Android RecyclerView打造自動(dòng)循環(huán)效果
先看效果圖
主要處理的地方:
1、RecyclerView中Adapter的item個(gè)人可以無(wú)限輪詢(xún).
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)輪詢(xún) private boolean canRun;//標(biāo)示是否可以自動(dòng)輪詢(xún),可在不需要的是否置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; //使用弱引用持有外部類(lèi)引用->防止內(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); } } } //開(kāi)啟:如果正在運(yùn)行,先停止->再開(kāi)啟 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ù)寬度超過(guò)屏幕寬度->自己處理 mRecyclerView.start(); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android中RecyclerView上拉下拉,分割線(xiàn),多條目的實(shí)例代碼
- Android RecyclerView詳解及簡(jiǎn)單實(shí)例
- Android開(kāi)發(fā)中RecyclerView模仿探探左右滑動(dòng)布局功能
- Android 中RecyclerView多種item布局的寫(xiě)法(頭布局+腳布局)
- Android基于RecyclerView實(shí)現(xiàn)高亮搜索列表
- Android RecyclerView下拉刷新和上拉加載更多
- Android中RecyclerView嵌套滑動(dòng)沖突解決的代碼片段
- Android中實(shí)現(xiàn)淘寶購(gòu)物車(chē)RecyclerView或LIstView的嵌套選擇的邏輯
- Android使用recyclerview打造真正的下拉刷新上拉加載效果
- Android 滑動(dòng)監(jiān)聽(tīng)RecyclerView線(xiàn)性流+左右劃刪除+上下移動(dòng)
- Android RecyclerView 上拉加載更多及下拉刷新功能的實(shí)現(xiàn)方法
- Android使用RecyclerView實(shí)現(xiàn)水平滾動(dòng)控件
- Android RecyclerView的Item點(diǎn)擊事件實(shí)現(xiàn)整理
相關(guān)文章
Android啟動(dòng)頁(yè)用戶(hù)相關(guān)政策彈框的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android啟動(dòng)頁(yè)用戶(hù)相關(guān)政策彈框的實(shí)現(xiàn)方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05Android開(kāi)發(fā)學(xué)習(xí)之WallPaper設(shè)置壁紙?jiān)敿?xì)介紹與實(shí)例
這篇文章主要介紹了Android開(kāi)發(fā)學(xué)習(xí)之WallPaper設(shè)置壁紙?jiān)敿?xì)介紹與實(shí)例,有需要的朋友可以參考一下2013-12-12Android項(xiàng)目實(shí)現(xiàn)視頻播放器
這篇文章主要為大家詳細(xì)介紹了Android項(xiàng)目實(shí)現(xiàn)視頻播放器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03Android編程設(shè)計(jì)模式之Builder模式實(shí)例詳解
這篇文章主要介紹了Android編程設(shè)計(jì)模式之Builder模式,結(jié)合實(shí)例形式詳細(xì)分析了Android設(shè)計(jì)模式之Builder模式概念、功能、使用場(chǎng)景、用法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-12-12簡(jiǎn)單仿寫(xiě)Android控件SlidingMenu的實(shí)例代碼
下面小編就為大家分享一篇簡(jiǎn)單仿寫(xiě)Android控件SlidingMenu的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01android中colors.xml顏色設(shè)置資源文件的方法
這篇文章主要介紹了android中colors.xml顏色設(shè)置資源文件,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03Android Mms之:深入理解對(duì)話(huà)列表管理
本篇文章是對(duì)Android中的對(duì)話(huà)列表管理進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05Android仿微信圖片選擇器ImageSelector使用詳解
這篇文章主要為大家詳細(xì)介紹了Android仿微信圖片選擇器ImageSelector的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12