ViewPager—02图片无限轮播

时间:2022-07-10 15:15:43

布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" > <RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp" > <android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent" /> <LinearLayout
android:id="@+id/ll_point"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:layout_marginBottom="20dp"
android:orientation="horizontal" />
</RelativeLayout> </RelativeLayout>

相关代码

 public class MainActivity extends Activity {

     private ViewPager mViewPager;
private LinearLayout mLlPoint;
private MyAdapter mAdapter; private int[] ids = {R.drawable.a1,R.drawable.a2,R.drawable.a3};
private List<ImageView> mImages;
private int lastPos;
private boolean isRunning; private Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch(msg.what){
case 0: mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1); if (isRunning) {
mHandler.sendEmptyMessageDelayed(0,3000);
} break; default:
break;
}
};
}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); initView();
initData(); mAdapter = new MyAdapter();
mViewPager.setAdapter(mAdapter);
mViewPager.addOnPageChangeListener(new PagerChnageListener());
mViewPager.setCurrentItem((mImages.size()) * 1000); isRunning = true;
mHandler.sendEmptyMessageDelayed(0,3000);
} private void initView() {
mViewPager = (ViewPager)findViewById(R.id.vp);
mLlPoint = (LinearLayout)findViewById(R.id.ll_point);
} private void initData() { mImages = new ArrayList<ImageView>(); for (int i = 0;i < ids.length;i++) {
ImageView iv = new ImageView(this);
iv.setBackgroundResource(ids[i]);
mImages.add(iv); View view = new View(this);
view.setBackgroundResource(R.drawable.point_selector);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(Utils.dip2px(this,10),
Utils.dip2px(this,10));
if (i != 0) {
params.leftMargin = 20;
view.setEnabled(false);
}else {
view.setEnabled(true);
}
view.setLayoutParams(params);
mLlPoint.addView(view);
}
} class MyAdapter extends PagerAdapter { @Override
public int getCount() {
return Integer.MAX_VALUE;
} @Override
public boolean isViewFromObject(View arg0,Object arg1) {
return arg0 == arg1;
} @Override
public Object instantiateItem(ViewGroup container,int position) {
ImageView view = mImages.get(position % mImages.size()); // 增加异常扑捉防止空白页
try {
((ViewPager)container).addView(view,0);
}catch(Exception e) {
e.printStackTrace();
}
return view;
} @Override
public void destroyItem(ViewGroup container,int position,Object object) {
// 防止2/3个出现BUG
// container.removeView(mImages.get(position % mImages.size()));
}
} class PagerChnageListener implements OnPageChangeListener { /* state: 0空闲,1是滑行中,2加载完毕 */
@Override
public void onPageScrollStateChanged(int state) {
switch(state){
case 0:
isRunning = true;
break;
case 1:
isRunning = false;
break;
case 2:
isRunning = true;
break; default:
break;
}
} @Override
public void onPageScrolled(int position,float positionOffset,int positionOffsetPixels) { } @Override
public void onPageSelected(int position) {
int pos = position % mImages.size();
mLlPoint.getChildAt(lastPos).setEnabled(false);
mLlPoint.getChildAt(pos).setEnabled(true);
lastPos = pos;
}
} @Override
protected void onDestroy() {
super.onDestroy();
isRunning = false;
mHandler.removeCallbacksAndMessages(null);
}
}

 

 效果图

ViewPager—02图片无限轮播