欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來(lái)到入門教程網(wǎng)!

Android

當(dāng)前位置:主頁(yè) > 軟件編程 > Android >

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

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:Android|點(diǎn)擊: 次

需求:類似 或者商場(chǎng),大屏幕無(wú)限輪播item (廣告詞/廣告圖…),供大家參考,具體內(nèi)容如下

代碼如下

/**
 * 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ù)寬度超過(guò)屏幕寬度->自己處理
   mRecyclerView.start();

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有