国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android RecyclerView打造自動(dòng)循環(huán)效果

2019-12-12 04:26:06
字體:
供稿:網(wǎng)友

先看效果圖

主要處理的地方:
1、RecyclerView中Adapter的item個(gè)人可以無限輪詢.
2、RecyclerView自動(dòng)滑動(dòng)
3、手指按下時(shí)滑動(dòng)停止,手指抬起后繼續(xù)自動(dòng)滑動(dòng)

public class AutoPollRecyclerView extends RecyclerView { private static final long TIME_AUTO_POLL = 16; 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); }}

Adapter處理:主要處理getItemCount()和數(shù)據(jù)填充的onBindViewHolder()方法

public class AutoPollAdapter extends RecyclerView.Adapter<BaseViewHolder> { private final Context mContext; private final List<String> mData; public AutoPollAdapter(Context context, List<String> list) {  this.mContext = context;  this.mData = list; } @Override public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {  View view = LayoutInflater.from(mContext).inflate(R.layout.item_auto_poll, parent, false);  BaseViewHolder holder = new BaseViewHolder(view);  return holder; } @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調(diào)用的代碼

public class AutoPollRecyclerActivity extends BaseActivity { @Override protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_auto_poll);  initToolBar();  initView(); } private void initView() {  AutoPollRecyclerView mRecyclerView = (AutoPollRecyclerView) findViewById(R.id.rv_recycleView);  List<String> list = new ArrayList<>();  for (int i = 0; i < 5; ) {   list.add(" Item: " + ++i);  }  AutoPollAdapter adapter = new AutoPollAdapter(this, list);  mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));  mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.HORIZONTAL_LIST));  mRecyclerView.setAdapter(adapter);  if (true) //保證itemCount的總個(gè)數(shù)寬度超過屏幕寬度->自己處理   mRecyclerView.start(); }}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 莱西市| 扶风县| 温宿县| 乌拉特中旗| 宁远县| 麻江县| 疏勒县| 藁城市| 娱乐| 隆安县| 大冶市| 马尔康县| 远安县| 通许县| 庆城县| 孝义市| 永修县| 旬邑县| 罗平县| 景谷| 兰西县| 榆树市| 泗洪县| 祁门县| 大余县| 桦南县| 尤溪县| 凉城县| 衡阳县| 河东区| 夏河县| 凭祥市| 乌兰浩特市| 太谷县| 民丰县| 长春市| 广灵县| 肥乡县| 广河县| 武宣县| 延寿县|