recyclerview的数据刷新(下拉刷新和自动加载更多)以及添加提示语(例如:“数据已加载完毕”)
下拉加載更多的核心是SwipeRefreshLayout搭配Recyclerview進行使用。布局為
<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> 因為recyclerview自帶的是沒有刷新功能的。下拉刷新我們可以調用SwipeRefreshLayout的setOnRefreshListener()來完成。
在回調的方法中完成自己想要的邏輯。一般都是將page設置為1,然后initData()。這個initData()就是進行數據請求的方法。
swipelayout_command.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {@Override public void onRefresh() {page=1; recyclerview.setNoMore(false);//可以加載更多 getRequestData();//請求數據請求 swipelayout_command.setRefreshing(false);//刷新圖標消失 } }); 這里,我推薦一個在github上找到的一個自定義的recyclerview控件,因為其功能比較完善。附上鏈接:https://github.com/jdsjlzx/LRecyclerView這個自定義控件包含兩種recyclerview,一個LRecyclerview,一個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>上拉刷新作者已經寫好了API,我們可以直接調用——
recyclerview.setOnLoadMoreListener(new OnLoadMoreListener() {@Override public void onLoadMore() {/** * 判斷現在加載的數據條數是否等于服務器數據總條數 * 如果小于,則page加1,加載新一頁的數據 * 如果等于,設置不再加載更多。 */ 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);//不再加載更多 }} });? 回調方法里面的邏輯:這里我設置了幾個常量
/**服務器端一共多少條數據*/ private static final int TOTAL_COUNTER = 10000; /**每一頁展示多少條數據*/ private static final int REQUEST_COUNT = 10; /**已經獲取到多少條數據了*/ private static int mCurrentCounter = 0; 設置這幾個常量的作用有兩個,一個是確定數據加載完成,顯示提示語。另一個是數據加載未完成,加載下一頁(這個邏輯放在自動加載更多的回調方法里面)。這幾個常量都可以隨自己的需求進行定義。
在自動加載更多的監聽事件后設置提示語——
//設置底部加載文字提示 recyclerview.setFooterViewHint("拼命加載中","———— 已經全部為你呈現了 ————","網絡不給力啊,點擊再試一次吧"); 接下來看看請求數據的方法的邏輯—— @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);//完成刷新 //先清空數據,再加載,避免數據重復的問題 adapter.notifyDataSetChanged(); mCurrentCounter+=list_count.size(); }這里我先進行判斷,page是否為1,如果為1,那么先請求數據集合,這么做的目的是為了下拉刷新的時候數據不會重復出現。
然后判斷自定義顯示的數據條數是否大于加載得到的數據集合的大小,如果是,那么說明數據已經加載完畢,設置setNoMore()為true,這樣就會顯示提示語。
再將數據添加到集合中;完成刷新;刷新適配器。最后將自定義的當前顯示的數據條數的值進行累加。
然后就是綁定適配器。
LinearLayoutManager manager = new LinearLayoutManager(this); recyclerview.setLayoutManager(manager); SongCommandAdapter commandAdapter = new SongCommandAdapter(list, this); adapter = new LuRecyclerViewAdapter(commandAdapter); recyclerview.setAdapter(adapter); 這里可以看到,我是將自定義的recyclerviewAdaper作為參數傳進了需要的LuRecyclerviewAdapter()中。這里主要說明的是自定義的適配器完全是按照正常的recyclerviewadaper的邏輯去寫。
好了,就這樣。關于LuRecyclerview以及LRecyclerview的大家可以點進鏈接看看。有很多內容,可以添加頭布局尾布局,添加分割線等等,自己去了解。這篇文章主要就是說一下關于數據請求的相關邏輯判斷。希望你們能看懂。。。。。。。
總結
以上是生活随笔為你收集整理的recyclerview的数据刷新(下拉刷新和自动加载更多)以及添加提示语(例如:“数据已加载完毕”)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: dump在计算机中的意义是什么
- 下一篇: Scrapped or attached