这篇文章主要为大家详细介绍了android RecyclerView打造自动循环效果,非常实用的循环滚动效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
先看效果图:
主要处理的地方:
- 1、RecyclerView中Adapter的item个人可以无限轮询.
- 2、RecyclerView自动滑动
- 3、手指按下时滑动停止,手指抬起后继续自动滑动
我这里是在xml中添加的LinearLayoutManager,也可以在java代码中setLayoutManager
<com.zxj.AutoPollRecyclerView
android:id="@+id/recycleView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="LinearLayoutManager"/>
public class AutoPollRecyclerView extends RecyclerView {
private static final long TIME_AUTO_POLL = 16;
AutoPollTask autoPollTask;
private boolean running; //表示是否正在自动轮询
private boolean canRun;//表示是否可以自动轮询
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, 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<AutoPollAdapter.BaseViewHolder> {
private final List<String> mData;
public AutoPollAdapter(List<String> list) {
this.mData = list;
}
@Override
public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).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.tv.setText(data);
}
@Override
public int getItemCount() {
return Integer.MAX_VALUE;
}
class BaseViewHolder extends RecyclerView.ViewHolder{
TextView tv;
public BaseViewHolder(View itemView) {
super(itemView);
tv = itemView.findViewById(R.id.tv_content);
}
}
}
最后附上Activity调用的代码
public class MainActivity extends AppCompatActivity {
private AutoPollRecyclerView mRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
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(list);
mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
mRecyclerView.setAdapter(adapter);
mRecyclerView.start();
}
@Override
protected void onDestroy() {
super.onDestroy();
if(null != mRecyclerView){
mRecyclerView.stop();
}
}
}