利用RecyclerView实现无限循环滚动
一、先上效果图
二、GitHub地址
GitHub地址 https://github.com/MNXP/AutomaticRolling 三、思路
RecycleView实现,内部实现Runnable滚动
四、实现
1)内部实现Runnable
static class AutoPollTask implements Runnable {
private final WeakReference mReference; /** * 使用弱引用持有外部类引用,防止内存泄漏 */ private AutoPollTask(AutoPollRecyclerView reference) {
this.mReference = new WeakReference<>(reference); } @Override public void run() {
AutoPollRecyclerView recyclerView = mReference.get(); if (recyclerView != null && recyclerView.running) {
recyclerView.scrollBy(2, 2);//每次滚动距离 /** * 判断是否为无限循环 */ if (recyclerView.canRun){
/** * 判断是否触底 */ if (isSlideToBottom(recyclerView)) {
/**跳至顶部*/ recyclerView.smoothScrollToPosition(0); } } recyclerView.postDelayed(recyclerView.autoPollTask,TIME_AUTO_POLL); } } } /**判断Recycler是否滑动至最底部 是返回true 不是返回false*/ public static boolean isSlideToBottom(RecyclerView recyclerView) {
if (recyclerView == nul