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 !=  && 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);
 }
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

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;
 }
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

最后附上Activity调用的代码

 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();
 }
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

嗯,就这些

http://www.jizhuomi.com/career/532.html 程序猿去赚钱,嘿嘿

                    <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/markdown_views-ea0013b516.css">
                        </div>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值