先看效果图
主要处理的地方:
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(); }}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android RecyclerView打造自动循环效果全部内容,希望文章能够帮你解决Android RecyclerView打造自动循环效果所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)