栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android RecyclerView打造自动循环效果

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 mReference;
  //使用弱引用持有外部类引用->防止内存泄漏
  public AutoPollTask(AutoPollRecyclerView reference) {
   this.mReference = new WeakReference(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 {
 private final Context mContext;
 private final List mData;
 public AutoPollAdapter(Context context, List 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 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();
 }
}

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/160465.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号