为RecyclerView添加下拉刷新(PullToRefresh)功能
在之前的文章中,我們實(shí)現(xiàn)了帶有header和footer功能的WrapRecyclerView。
實(shí)現(xiàn)帶header和footer功能的RecyclerView
實(shí)現(xiàn)帶header和footer功能的RecyclerView——完善篇
現(xiàn)今App中列表的下拉刷新和上拉加載已經(jīng)是一種習(xí)慣了,這兩個(gè)操作也確實(shí)方便很多。
為RecyclerView添加這個(gè)功能可以通過(guò)多種方法,這里我選用了一種簡(jiǎn)單的做法。
因?yàn)樵谖覀兊腁pp中我們一直在使用com.loopeer.android.thirdparty:pulltorefresh:<版本>這個(gè)庫(kù),所以這次也是基于這個(gè)庫(kù)來(lái)實(shí)現(xiàn)。
首先要為WrapRecyclerView添加兩個(gè)方法,如下:
public int getFirstVisiblePosition(){int firstPosition = 0;LayoutManager layoutManager = getLayoutManager();if(layoutManager instanceof LinearLayoutManager){firstPosition = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();}if(layoutManager instanceof GridLayoutManager){firstPosition = ((GridLayoutManager) layoutManager).findFirstVisibleItemPosition();}if(layoutManager instanceof StaggeredGridLayoutManager){int[] positions = ((StaggeredGridLayoutManager) layoutManager).findFirstVisibleItemPositions(null);firstPosition = positions[0];}return firstPosition;}public int getLastVisiblePosition(){int lastPosition = 0;LayoutManager layoutManager = getLayoutManager();if(layoutManager instanceof LinearLayoutManager){lastPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();}if(layoutManager instanceof GridLayoutManager){lastPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();}if(layoutManager instanceof StaggeredGridLayoutManager){int[] positions = ((StaggeredGridLayoutManager) layoutManager).findLastVisibleItemPositions(null);lastPosition = positions[positions.length - 1];}return lastPosition;}
這兩個(gè)方法用于輔助判斷滑動(dòng)時(shí)是否到頂或到底,下面會(huì)用到。注意對(duì)于不同的LayoutManager使用不同的方式來(lái)獲取。
新建一個(gè)PullToRefreshRecyclerView,繼承PullToRefreshBase
public class PullToRefreshRecyclerView extends PullToRefreshBase<WrapRecyclerView>{
需要重寫(xiě)幾個(gè)方法來(lái)實(shí)現(xiàn)功能,如
這兩個(gè)方法會(huì)在滑動(dòng)的時(shí)候被調(diào)用,判斷是否已經(jīng)到列表頂部或底部,如果到頂部或底部就會(huì)執(zhí)行下拉/上拉的操作了。
邏輯比較簡(jiǎn)單,判斷是否顯示了第一個(gè)/最后一個(gè)item,并且它的top/bottom也顯示了(說(shuō)明這個(gè)item完整顯示出來(lái)了)。
還需要重寫(xiě)另外一個(gè)方法
@Overrideprotected WrapRecyclerView createRefreshableView(Context context, AttributeSet attrs) {WrapRecyclerView recyclerView = new WrapRecyclerView(context, attrs);recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {@Overridepublic void onScrollStateChanged(RecyclerView recyclerView, int newState) {super.onScrollStateChanged(recyclerView, newState);}@Overridepublic void onScrolled(RecyclerView recyclerView, int dx, int dy) {super.onScrolled(recyclerView, dx, dy);if(isReadyForPullStart()){recyclerView.clearFocus();}}});recyclerView.setId(R.id.pulltorefresh_recyclerview);return recyclerView;}
這個(gè)方法就是創(chuàng)建一個(gè)WrapRecyclerView,注意不要忘了setId,否則在Fragment中使用會(huì)出現(xiàn)一些問(wèn)題(回收重建的時(shí)候)。
由于基于pulltorefresh庫(kù),所有功能庫(kù)中都實(shí)現(xiàn)了,所以重寫(xiě)這幾個(gè)方法就能實(shí)現(xiàn)下拉刷新功能了。實(shí)現(xiàn)效果如下
如果想改變顯示或風(fēng)格,可以通過(guò)pulltorefresh庫(kù)的api來(lái)實(shí)現(xiàn),關(guān)于pulltorefresh庫(kù)的使用大家可以自行查閱相關(guān)文檔,如果有時(shí)間我坑會(huì)整理一篇關(guān)于這個(gè)庫(kù)的文章。
通過(guò)三篇文章我們對(duì)對(duì)RecyclerView功能進(jìn)行擴(kuò)展,目前基本可以滿足大部分需求了。所以這部分就到此告一段落了,如果需要其他功能我們以后再來(lái)補(bǔ)充。謝謝大家!!
?源碼
完整源碼請(qǐng)關(guān)注公眾號(hào):BennuCTech,發(fā)送“WrapRecyclerView”獲取。
總結(jié)
以上是生活随笔為你收集整理的为RecyclerView添加下拉刷新(PullToRefresh)功能的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 实现带header和footer功能的R
- 下一篇: 剖析Fragment的Pause生命周期