在线直播系统源码,循环滚动RecyclerView的实现
public class AutoRecyclerView extends RecyclerView {
private static final long TIME_AUTO_POLL = 16;
private final AutoPollTask autoPollTask;
private boolean running; //表示是否正在自动轮询
private boolean canRun;//表示是否可以自动轮询
public AutoRecyclerView(@NonNull Context context) {
super(context);
autoPollTask = new AutoPollTask(this);
}
public AutoRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
autoPollTask = new AutoPollTask(this);
}
public AutoRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
autoPollTask = new AutoPollTask(this);
}
private static class AutoPollTask implements Runnable {
private final WeakReference<AutoRecyclerView> mReference;
//使用弱引用持有外部类引用->防止内存泄漏
public AutoPollTask(AutoRecyclerView reference) {
this.mReference = new WeakReference<>(reference);
}
@Override
public void run() {
AutoRecyclerView recyclerView = mReference.get();
if (recyclerView != null && recyclerView.running && recyclerView.canRun) {
//水平移动
recyclerView.scrollBy(2, 0);
//竖直移动
//recyclerView.scrollBy(0, 2);
recyclerView.postOnAnimationDelayed(recyclerView.autoPollTask, TIME_AUTO_POLL);
}
}
}
//开启:如果正在运行,先停止->再开启
private void start() {
if (running)
stop();
canRun = true;
running = true;
postDelayed(autoPollTask, TIME_AUTO_POLL);
}
private void stop() {
running = false;
removeCallbacks(autoPollTask);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
return true;
}
public void startAutoScrolling(){
if (running){
return;
}
start();
}
public void stopAutoScrolling(){
stop();
}
public boolean isAutoScrolling(){
return running;
}
}
以上就是在线直播系统源码,循环滚动RecyclerView的实现, 更多内容欢迎关注之后的文章
标签:autoPollTask,RecyclerView,private,public,running,直播,context,recyclerView,源码 From: https://www.cnblogs.com/yunbaomengnan/p/16993728.html