private ViewPagerAdapter mViewPagerAdapter; private List<ImageView> images; private List<View> dots; private int currentItem; private int oldPosition = 0; //记录上一次点的位置 private ScheduledExecutorService scheduledExecutorService;//线程池 private int[] imageIds = new int[]{ R.drawable.viewpager_one, R.drawable.viewpager_one, R.drawable.viewpager_one, }; /** * 接收子线程传递过来的数据 */ private Handler mHandler = new Handler(){ public void handleMessage(android.os.Message msg) { mHomeVp.setCurrentItem(currentItem); } };
/** * 轮播监听private ViewPager mHomeVp; ui控件*/mHomeVp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { dots.get(position).setBackgroundResource(R.drawable.dot_focused); dots.get(oldPosition).setBackgroundResource(R.drawable.dot_normal); oldPosition = position; currentItem = position; } @Override public void onPageScrollStateChanged(int state) { }});
private void initVariable() { //显示的图片 images = new ArrayList<ImageView>(); for(int i = 0; i < imageIds.length; i++){ ImageView imageView = new ImageView(getActivity()); imageView.setBackgroundResource(imageIds[i]); images.add(imageView); } //显示的小点 dots = new ArrayList<View>(); dots.add(mView.findViewById(R.id.dot_0)); dots.add(mView.findViewById(R.id.dot_1)); dots.add(mView.findViewById(R.id.dot_2)); mViewPagerAdapter = new ViewPagerAdapter(); mHomeVp.setAdapter(mViewPagerAdapter); }
private class ViewPagerAdapter extends PagerAdapter { @Override public int getCount() { return images.size(); } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(images.get(position)); // super.destroyItem(container, position, object); } @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(images.get(position)); return images.get(position); } } @Override public void onStart() { super.onStart(); scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); scheduledExecutorService.scheduleWithFixedDelay( new ViewPageTask(), 2, 3, TimeUnit.SECONDS); } private class ViewPageTask implements Runnable{ @Override public void run() { currentItem = (currentItem + 1) % imageIds.length; mHandler.sendEmptyMessage(0); } }
XML:
<FrameLayout android:layout_width="match_parent" android:layout_height="121dp"> <android.support.v4.view.ViewPager android:id="@+id/home_vp" android:layout_width="match_parent" android:layout_height="match_parent"/> <LinearLayout android:layout_width="match_parent" android:layout_height="35dip" android:layout_gravity="bottom" android:background="#33000000" android:gravity="center" android:orientation="vertical"> <TextView android:id="@+id/home_vp_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="图片标题" android:textColor="@android:color/white"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="3dip" android:orientation="horizontal"> <View android:id="@+id/dot_0" android:layout_width="5dip" android:layout_height="5dip" android:layout_marginLeft="2dip" android:layout_marginRight="2dip" android:background="@drawable/dot_focused"/> <View android:id="@+id/dot_1" android:layout_width="5dip" android:layout_height="5dip" android:layout_marginLeft="2dip" android:layout_marginRight="2dip" android:background="@drawable/dot_normal"/> <View android:id="@+id/dot_2" android:layout_width="5dip" android:layout_height="5dip" android:layout_marginLeft="2dip" android:layout_marginRight="2dip" android:background="@drawable/dot_normal"/> </LinearLayout> </LinearLayout> </FrameLayout>