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

Android RecyclerView打造自动循环效果

程序员文章站 2024-02-26 21:40:04
先看效果图 主要处理的地方: 1、recyclerview中adapter的item个人可以无限轮询. 2、recyclerview自动滑动 3、手指按下时滑动...

先看效果图

Android RecyclerView打造自动循环效果

主要处理的地方:
1、recyclerview中adapter的item个人可以无限轮询.
2、recyclerview自动滑动
3、手指按下时滑动停止,手指抬起后继续自动滑动

public class autopollrecyclerview extends recyclerview {
 private static final long time_auto_poll = 16;
 autopolltask autopolltask;
 private boolean running; //标示是否正在自动轮询
 private boolean canrun;//标示是否可以自动轮询,可在不需要的是否置false
 public autopollrecyclerview(context context, @nullable attributeset attrs) {
  super(context, attrs);
  autopolltask = new autopolltask(this);
 }
 static class autopolltask implements runnable {
  private final weakreference<autopollrecyclerview> mreference;
  //使用弱引用持有外部类引用->防止内存泄漏
  public autopolltask(autopollrecyclerview reference) {
   this.mreference = new weakreference<autopollrecyclerview>(reference);
  }
  @override
  public void run() {
   autopollrecyclerview recyclerview = mreference.get();
   if (recyclerview != null && recyclerview.running &&recyclerview.canrun) {
    recyclerview.scrollby(2, 2);
    recyclerview.postdelayed(recyclerview.autopolltask,recyclerview.time_auto_poll);
   }
  }
 }
 //开启:如果正在运行,先停止->再开启
 public void start() {
  if (running)
   stop();
  canrun = true;
  running = true;
  postdelayed(autopolltask,time_auto_poll);
 }
 public void stop(){
  running = false;
  removecallbacks(autopolltask);
 }
 @override
 public boolean ontouchevent(motionevent e) {
  switch (e.getaction()){
   case motionevent.action_down:
    if (running)
     stop();
    break;
   case motionevent.action_up:
   case motionevent.action_cancel:
   case motionevent.action_outside:
    if (canrun)
     start();
    break;
  }
  return super.ontouchevent(e);
 }
}

adapter处理:主要处理getitemcount()和数据填充的onbindviewholder()方法

public class autopolladapter extends recyclerview.adapter<baseviewholder> {
 private final context mcontext;
 private final list<string> mdata;
 public autopolladapter(context context, list<string> list) {
  this.mcontext = context;
  this.mdata = list;
 }
 @override
 public baseviewholder oncreateviewholder(viewgroup parent, int viewtype) {
  view view = layoutinflater.from(mcontext).inflate(r.layout.item_auto_poll, parent, false);
  baseviewholder holder = new baseviewholder(view);
  return holder;
 }
 @override
 public void onbindviewholder(baseviewholder holder, int position) {
  string data = mdata.get(position%mdata.size());
  holder.settext(r.id.tv_content,data);
 }
 @override
 public int getitemcount() {
  return integer.max_value;
 }
}

最后附上activity调用的代码

public class autopollrecycleractivity extends baseactivity {
 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_auto_poll);
  inittoolbar();
  initview();
 }
 private void initview() {
  autopollrecyclerview mrecyclerview = (autopollrecyclerview) findviewbyid(r.id.rv_recycleview);
  list<string> list = new arraylist<>();
  for (int i = 0; i < 5; ) {
   list.add(" item: " + ++i);
  }
  autopolladapter adapter = new autopolladapter(this, list);
  mrecyclerview.setlayoutmanager(new linearlayoutmanager(this, linearlayoutmanager.horizontal, false));
  mrecyclerview.additemdecoration(new divideritemdecoration(this, divideritemdecoration.horizontal_list));
  mrecyclerview.setadapter(adapter);
  if (true) //保证itemcount的总个数宽度超过屏幕宽度->自己处理
   mrecyclerview.start();
 }
}

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