Android Implementation of Automatic Polling RecycleView

  • 2021-11-13 18:05:19
  • OfStack

Requirements: Similar to hospitals or shopping malls, item (advertising words/advertising pictures …) is broadcast on the big screen for your reference. The specific contents are as follows

The code is as follows


/**
 * Created by Xia_ Yan  on 2017/8/20.
 */

public class AutoPollRecyclerView extends RecyclerView {
 private static final long TIME_AUTO_POLL = 32;
 AutoPollTask autoPollTask;
 private boolean running; // Indicates whether automatic polling is in progress 
 private boolean canRun;// Indicate whether automatic polling is available , Can be set in whether you don't need it or not 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;
 // Holding External Class References with Weak References -> Prevent memory leaks 
 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);
 }
 }
 }
 // Open : If you are running , Stop first -> Re-open 
 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);
 }
}

Open: If it is running, stop first- > Re-open


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);
 }
}

The code in Adapter is as follows


@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;
 }

Code in Activity


mRecyclerView.setAdapter(adapter);
 if (true) // Guarantee itemCount The total number width of exceeds the screen width -> Handle by oneself 
 mRecyclerView.start();

Related articles: