Android如何實(shí)現(xiàn)自動(dòng)輪詢RecycleView?

這篇文章將為大家詳細(xì)講解有關(guān)Android如何實(shí)現(xiàn)自動(dòng)輪詢RecycleView?,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

創(chuàng)新互聯(lián)建站專注于企業(yè)成都全網(wǎng)營銷、網(wǎng)站重做改版、江海網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、HTML5、成都做商城網(wǎng)站、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為江海等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。

代碼如下

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

public class AutoPollRecyclerView extends RecyclerView {
 private static final long TIME_AUTO_POLL = 32;
 AutoPollTask autoPollTask;
 private boolean running; //標(biāo)示是否正在自動(dòng)輪詢
 private boolean canRun;//標(biāo)示是否可以自動(dòng)輪詢,可在不需要的是否置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;
 //使用弱引用持有外部類引用->防止內(nèi)存泄漏
 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);
 }
 }
 }
 //開啟:如果正在運(yùn)行,先停止->再開啟
 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);
 }
}

開啟:如果正在運(yùn)行,先停止->再開啟

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中的代碼如下

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

Activity中的代碼

mRecyclerView.setAdapter(adapter);
 if (true) //保證itemCount的總個(gè)數(shù)寬度超過屏幕寬度->自己處理
 mRecyclerView.start();

關(guān)于Android如何實(shí)現(xiàn)自動(dòng)輪詢RecycleView?就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

分享題目:Android如何實(shí)現(xiàn)自動(dòng)輪詢RecycleView?
瀏覽路徑:http://muchs.cn/article10/ghsogo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、響應(yīng)式網(wǎng)站、網(wǎng)站營銷App開發(fā)、網(wǎng)站制作、網(wǎng)站內(nèi)鏈

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司