需求:类似医院或者商场,大屏幕无限轮播item (广告词/广告图…),供大家参考,具体内容如下
代码如下
/** * Created by Xia_焱 on 2017/8/20. */public class AutopollRecyclerVIEw extends RecyclerVIEw { private static final long TIME_auto_PolL = 32; 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); }}
开启:如果正在运行,先停止->再开启
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中的代码如下
@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中的代码
mRecyclerVIEw.setAdapter(adapter); if (true) //保证itemCount的总个数宽度超过屏幕宽度->自己处理 mRecyclerVIEw.start();
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
总结以上是内存溢出为你收集整理的Android实现自动轮询的RecycleView全部内容,希望文章能够帮你解决Android实现自动轮询的RecycleView所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)