recyclerview的数据刷新(下拉刷新和自动加载更多)以及添加提示语(例如:“数据已加载完毕”)
下拉加載更多的核心是SwipeRefreshLayout搭配Recyclerview進(jìn)行使用。布局為
<android.support.v4.widget.SwipeRefreshLayout android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </android.support.v4.widget.SwipeRefreshLayout> 因?yàn)閞ecyclerview自帶的是沒有刷新功能的。下拉刷新我們可以調(diào)用SwipeRefreshLayout的setOnRefreshListener()來完成。
在回調(diào)的方法中完成自己想要的邏輯。一般都是將page設(shè)置為1,然后initData()。這個(gè)initData()就是進(jìn)行數(shù)據(jù)請求的方法。
swipelayout_command.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {@Override public void onRefresh() {page=1; recyclerview.setNoMore(false);//可以加載更多 getRequestData();//請求數(shù)據(jù)請求 swipelayout_command.setRefreshing(false);//刷新圖標(biāo)消失 } }); 這里,我推薦一個(gè)在github上找到的一個(gè)自定義的recyclerview控件,因?yàn)槠涔δ鼙容^完善。附上鏈接:https://github.com/jdsjlzx/LRecyclerView這個(gè)自定義控件包含兩種recyclerview,一個(gè)LRecyclerview,一個(gè)LuRecyclerview。相對而言,LRecyclerview功能更加完善。
我這里用的是LuRecyclerview,搭配swipeRefreshLayout。
<android.support.v4.widget.SwipeRefreshLayout android:layout_below="@id/command_toolbar" android:id="@+id/swipe_command" android:layout_width="match_parent" android:layout_height="match_parent"> <com.github.jdsjlzx.recyclerview.LuRecyclerView android:id="@+id/recyclerview_command" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="50dp"> </com.github.jdsjlzx.recyclerview.LuRecyclerView> </android.support.v4.widget.SwipeRefreshLayout>上拉刷新作者已經(jīng)寫好了API,我們可以直接調(diào)用——
recyclerview.setOnLoadMoreListener(new OnLoadMoreListener() {@Override public void onLoadMore() {/** * 判斷現(xiàn)在加載的數(shù)據(jù)條數(shù)是否等于服務(wù)器數(shù)據(jù)總條數(shù) * 如果小于,則page加1,加載新一頁的數(shù)據(jù) * 如果等于,設(shè)置不再加載更多。 */ if (mCurrentCounter < TOTAL_COUNTER) {Log.i("sub", "onLoadMore: click"); Log.i("curr", "onResponse: if "+mCurrentCounter); page++;//page+1,也就是下一頁 Log.i("sub", "onCreateView: page"+page); getRequestData(); } else {//the end recyclerview.setNoMore(true);//不再加載更多 }} });? 回調(diào)方法里面的邏輯:這里我設(shè)置了幾個(gè)常量
/**服務(wù)器端一共多少條數(shù)據(jù)*/ private static final int TOTAL_COUNTER = 10000; /**每一頁展示多少條數(shù)據(jù)*/ private static final int REQUEST_COUNT = 10; /**已經(jīng)獲取到多少條數(shù)據(jù)了*/ private static int mCurrentCounter = 0; 設(shè)置這幾個(gè)常量的作用有兩個(gè),一個(gè)是確定數(shù)據(jù)加載完成,顯示提示語。另一個(gè)是數(shù)據(jù)加載未完成,加載下一頁(這個(gè)邏輯放在自動(dòng)加載更多的回調(diào)方法里面)。這幾個(gè)常量都可以隨自己的需求進(jìn)行定義。
在自動(dòng)加載更多的監(jiān)聽事件后設(shè)置提示語——
//設(shè)置底部加載文字提示 recyclerview.setFooterViewHint("拼命加載中","———— 已經(jīng)全部為你呈現(xiàn)了 ————","網(wǎng)絡(luò)不給力啊,點(diǎn)擊再試一次吧"); 接下來看看請求數(shù)據(jù)的方法的邏輯—— @Override public void onResponse(Call<CommandDetail> call, Response<CommandDetail> response) {if (page==1){list.removeAll(list); }List<CommandDetail.DataBean> list_count = response.body().getData(); if (REQUEST_COUNT>list_count.size()) {Log.i("curr", "onResponse: count_list size 2"+list_count.size()); recyclerview.setNoMore(true); }list.addAll(response.body().getData()); recyclerview.refreshComplete(REQUEST_COUNT);//完成刷新 //先清空數(shù)據(jù),再加載,避免數(shù)據(jù)重復(fù)的問題 adapter.notifyDataSetChanged(); mCurrentCounter+=list_count.size(); }這里我先進(jìn)行判斷,page是否為1,如果為1,那么先請求數(shù)據(jù)集合,這么做的目的是為了下拉刷新的時(shí)候數(shù)據(jù)不會重復(fù)出現(xiàn)。
然后判斷自定義顯示的數(shù)據(jù)條數(shù)是否大于加載得到的數(shù)據(jù)集合的大小,如果是,那么說明數(shù)據(jù)已經(jīng)加載完畢,設(shè)置setNoMore()為true,這樣就會顯示提示語。
再將數(shù)據(jù)添加到集合中;完成刷新;刷新適配器。最后將自定義的當(dāng)前顯示的數(shù)據(jù)條數(shù)的值進(jìn)行累加。
然后就是綁定適配器。
LinearLayoutManager manager = new LinearLayoutManager(this); recyclerview.setLayoutManager(manager); SongCommandAdapter commandAdapter = new SongCommandAdapter(list, this); adapter = new LuRecyclerViewAdapter(commandAdapter); recyclerview.setAdapter(adapter); 這里可以看到,我是將自定義的recyclerviewAdaper作為參數(shù)傳進(jìn)了需要的LuRecyclerviewAdapter()中。這里主要說明的是自定義的適配器完全是按照正常的recyclerviewadaper的邏輯去寫。
好了,就這樣。關(guān)于LuRecyclerview以及LRecyclerview的大家可以點(diǎn)進(jìn)鏈接看看。有很多內(nèi)容,可以添加頭布局尾布局,添加分割線等等,自己去了解。這篇文章主要就是說一下關(guān)于數(shù)據(jù)請求的相關(guān)邏輯判斷。希望你們能看懂。。。。。。。
總結(jié)
以上是生活随笔為你收集整理的recyclerview的数据刷新(下拉刷新和自动加载更多)以及添加提示语(例如:“数据已加载完毕”)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: dump在计算机中的意义是什么
- 下一篇: Scrapped or attached