Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the RecycleView of automatic polling by Android

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report