欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2022-03-12 11:28:24
本文实例为大家分享了android viewpager自定义轮播图,并解决播放冲突,供大家参考首先介绍一下这篇小代码:注释全面,简单易学,适用初学者,图片自拟!!!一定要将arraylist集合&am...

本文实例为大家分享了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);
 }
}

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

相关标签: ViewPager 轮播图