Android

关注公众号 jb51net

关闭
首页 > 软件编程 > Android > ViewPager自定义轮播图并解决播放冲突

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

作者:_万能的博哥

这篇文章主要为大家详细介绍了Android ViewPager自定义轮播图并解决播放冲突,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

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

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

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

layout布局内写法:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
 
    <android.support.v4.view.ViewPager
        android:id="@+id/pager01"
        android:layout_width="match_parent"
        android:layout_height="300dp" />
 
    <RadioGroup
        android:id="@+id/radioGroup"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
    </RadioGroup>
 
</LinearLayout>

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<ImageView> list = new ArrayList<ImageView>();
  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<ImageView> list;
 private Handler handler;
 //构造
 public MyPagerAdapter(ArrayList<ImageView> 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);
 }
}

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

您可能感兴趣的文章:
阅读全文