这篇文章主要讲解了“如何解决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); } }
感谢各位的阅读,以上就是“如何解决Android ViewPager自定义轮播图播放冲突”的内容了,经过本文的学习后,相信大家对如何解决Android ViewPager自定义轮播图播放冲突这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。