Android RecyclerView打造自动循环效果
短信预约 -IT技能 免费直播动态提醒
先看效果图
主要处理的地方:
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详解及简单实例Android开发中RecyclerView模仿探探左右滑动布局功能Android 中RecyclerView多种item布局的写法(头布局+脚布局)Android基于RecyclerView实现高亮搜索列表Android RecyclerView下拉刷新和上拉加载更多Android中RecyclerView嵌套滑动冲突解决的代码片段Android中实现淘宝购物车RecyclerView或LIstView的嵌套选择的逻辑Android使用recyclerview打造真正的下拉刷新上拉加载效果Android 滑动监听RecyclerView线性流+左右划删除+上下移动Android RecyclerView 上拉加载更多及下拉刷新功能的实现方法Android使用RecyclerView实现水平滚动控件Android RecyclerView的Item点击事件实现整理
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341