为RecyclerView添加下拉刷新(PullToRefresh)功能
在之前的文章中,我們實現(xiàn)了帶有header和footer功能的WrapRecyclerView。
實現(xiàn)帶header和footer功能的RecyclerView
實現(xiàn)帶header和footer功能的RecyclerView——完善篇
現(xiàn)今App中列表的下拉刷新和上拉加載已經(jīng)是一種習慣了,這兩個操作也確實方便很多。
為RecyclerView添加這個功能可以通過多種方法,這里我選用了一種簡單的做法。
因為在我們的App中我們一直在使用com.loopeer.android.thirdparty:pulltorefresh:<版本>這個庫,所以這次也是基于這個庫來實現(xiàn)。
首先要為WrapRecyclerView添加兩個方法,如下:
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;}
這兩個方法用于輔助判斷滑動時是否到頂或到底,下面會用到。注意對于不同的LayoutManager使用不同的方式來獲取。
新建一個PullToRefreshRecyclerView,繼承PullToRefreshBase
public class PullToRefreshRecyclerView extends PullToRefreshBase<WrapRecyclerView>{
需要重寫幾個方法來實現(xiàn)功能,如
這兩個方法會在滑動的時候被調(diào)用,判斷是否已經(jīng)到列表頂部或底部,如果到頂部或底部就會執(zhí)行下拉/上拉的操作了。
邏輯比較簡單,判斷是否顯示了第一個/最后一個item,并且它的top/bottom也顯示了(說明這個item完整顯示出來了)。
還需要重寫另外一個方法
@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;}
這個方法就是創(chuàng)建一個WrapRecyclerView,注意不要忘了setId,否則在Fragment中使用會出現(xiàn)一些問題(回收重建的時候)。
由于基于pulltorefresh庫,所有功能庫中都實現(xiàn)了,所以重寫這幾個方法就能實現(xiàn)下拉刷新功能了。實現(xiàn)效果如下
如果想改變顯示或風格,可以通過pulltorefresh庫的api來實現(xiàn),關于pulltorefresh庫的使用大家可以自行查閱相關文檔,如果有時間我坑會整理一篇關于這個庫的文章。
通過三篇文章我們對對RecyclerView功能進行擴展,目前基本可以滿足大部分需求了。所以這部分就到此告一段落了,如果需要其他功能我們以后再來補充。謝謝大家!!
?源碼
完整源碼請關注公眾號:BennuCTech,發(fā)送“WrapRecyclerView”獲取。
總結
以上是生活随笔為你收集整理的为RecyclerView添加下拉刷新(PullToRefresh)功能的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 实现带header和footer功能的R
- 下一篇: 剖析Fragment的Pause生命周期