Android ViewPager自定义轮播图并解决播放冲突

本文实例为大家分享了Android ViewPager自定义轮播图,并解决播放冲突,供大家参考

首先介绍一下这篇小代码:

注释全面,简单易学,适用初学者,图片自拟!!!

一定要将ArrayList集合&Handler机制传到适配器,否则无法完成展示,也解决不了滑动冲突,代码有点多,但是它通俗易懂啊

layout布局内写法:



 
    
 
    
    
 

Activity内写法:

public class Frag_01 extends Fragment {
 private RadioGroup radioGroup;
 private ViewPager pager;
 
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
   Bundle savedInstanceState) {
  View view = inflater.inflate(R.layout.frag_01, container, false);
  // 找Viewpager控件
  radioGroup = (RadioGroup) view.findViewById(R.id.radioGroup);
  pager = (ViewPager) view.findViewById(R.id.pager01);
  
  // 图片数据自拟-->drawable文件夹下
  int arr[] = { R.drawable.a1, R.drawable.a2, R.drawable.a3,
    R.drawable.a4, R.drawable.a5 };
  final ArrayList list = new ArrayList();
  for (int i = 0; i < arr.length; i++) {
   ImageView imageView = new ImageView(getActivity());
   imageView.setImageResource(arr[i]);
   list.add(imageView);
   // 生成小圆点
   RadioButton radioButton = new RadioButton(getActivity());
   radioGroup.addView(radioButton);
  }
  
  // 默认选中第一个
  radioGroup.check(radioGroup.getChildAt(0).getId());
    
  // 适配器
  pager.setAdapter(new MyPagerAdapter(list, handler));
  
  // 页面切换监听
  pager.setOnPageChangeListener(new OnPageChangeListener() {
 
   @Override
   public void onPageSelected(int arg0) {
    //根据页面索引控制小圆点切换
    arg0 %= list.size();
    radioGroup.check(radioGroup.getChildAt(arg0).getId());
 
   }
 
   @Override
   public void onPageScrolled(int arg0, float arg1, int arg2) {
    // TODO Auto-generated method stub
 
   }
 
   @Override
   public void onPageScrollStateChanged(int arg0) {
    // TODO Auto-generated method stub
 
   }
  });
  //handler发送消息
  handler.sendEmptyMessageDelayed(0, 3000);
  return view;
 }
 
        //Handler机制-----------------------------------------
 
 Handler handler = new Handler() {
  public void handleMessage(android.os.Message msg) {
   switch (msg.what) {
   case 0:
    //获取到下一页的条目--->赋值给当前条目
    int currentItem = pager.getCurrentItem() + 1;
    pager.setCurrentItem(currentItem);
    handler.sendEmptyMessageDelayed(0, 3000);
    break;
 
   default:
    break;
   }
  };
 };
}

PagerAdapter适配器的写法:(内含滑动冲突解决方案)

public class MyPagerAdapter extends PagerAdapter {
 
 private ArrayList list;
 private Handler handler;
 //构造
 public MyPagerAdapter(ArrayList list, Handler handler) {
  super();
  this.list = list;
  this.handler = handler;
 }
 
 @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) {
  position %= list.size();
  ImageView imageView = list.get(position);
  imageView.setOnTouchListener(new OnTouchListener() {
 
   @Override
   public boolean onTouch(View v, MotionEvent event) {
    // 获取事件
    int action = event.getAction();
    switch (action) {
    case MotionEvent.ACTION_MOVE:// 滑动
     handler.removeCallbacksAndMessages(null);
     break;
    case MotionEvent.ACTION_DOWN:// 按下
     handler.removeCallbacksAndMessages(null);
     break;
    case MotionEvent.ACTION_CANCEL:// 非人为操作
     handler.sendEmptyMessageDelayed(0, 3000);
     break;
    case MotionEvent.ACTION_UP:// 抬起
     handler.sendEmptyMessageDelayed(0, 3000);
     break;
    }
    return true;
   }
  });
  //获取的图片存进容器
  container.addView(imageView);
  return imageView;
 }
 
 // 销毁视图
 @Override
 public void destroyItem(ViewGroup container, int position, Object object) {
  container.removeView((View) object);
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

你可能感兴趣的:(Android ViewPager自定义轮播图并解决播放冲突)