Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the RecycleView of automatic polling by Android

Shulou Source: shulou.com Published: 2022-06-03 09:38:40 09月27日 Update

This article will explain in detail how to achieve automatic polling RecycleView on Android. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Demand: similar to hospitals or shopping malls, large screen unlimited rotation item (advertising words / advertising pictures …) For your reference, the details 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 the private boolean canRun;// mark can be automatically polled, and whether false public AutoPollRecyclerView (Context context, @ Nullable AttributeSet attrs) {super (context, attrs) {super (context, attrs); autoPollTask = new AutoPollTask (this);} static class AutoPollTask implements Runnable {private final WeakReference mReference is not needed. / / use weak references to hold external class references-> prevent memory leaks public AutoPollTask (AutoPollRecyclerView reference) {this.mReference = new WeakReference (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) } / / enable: if running, stop-> then 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);}}

Enable: if it is running, stop-> and then enable it.

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 contentfield data);} @ Override public int getItemCount () {return Integer.MAX_VALUE;}

Code in Activity

MRecyclerView.setAdapter (adapter); if (true) / / ensure that the total number of itemCount width exceeds the screen width-> handle mRecyclerView.start ()

This is the end of this article on "how to achieve automatic polling RecycleView in Android". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Moving wheel code being article content width advertisement more reference running good practical number memory hospital available in shopping mall big screen big screen screen Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Huawei Docker Shulou Technology Xiaomi