android中RecycleView添加下滑到底部的監(jiān)聽示例
我們的日常開發(fā)中經常用到下拉刷新,而網上評價最好的開源下拉刷新組件當然還是android-Ultra-Pull-To-Refresh 此組件可以給任何的控件添加下拉刷新功能。當然也包括recycleview了。
可惜android-Ultra-Pull-To-Refresh只是提供了下拉刷新的功能,但是對于列表類的組件,我們日常開發(fā)中更多的會用到其上拉加載或者滑到底部自動加載的功能,當然目前來看用戶更喜歡滑到底部自動加載的功能。就比如今天說的recycleview我們只能自己給其添加滑到底部加載更多的功能了。
那它的實現(xiàn)原理是神馬呢 非常簡單:
RecycleView內部有一個滑動監(jiān)聽的抽象類OnScrollListener來接收滾動事件,此類里面有兩個實現(xiàn)的方法
public abstract static class OnScrollListener {
/**
* Callback method to be invoked when RecyclerView's scroll state changes.
*
* @param recyclerView The RecyclerView whose scroll state has changed.
* @param newState The updated scroll state. One of {@link #SCROLL_STATE_IDLE},
* {@link #SCROLL_STATE_DRAGGING} or {@link #SCROLL_STATE_SETTLING}.
*/
public void onScrollStateChanged(RecyclerView recyclerView, int newState){}
/**
* Callback method to be invoked when the RecyclerView has been scrolled. This will be
* called after the scroll has completed.
* <p>
* This callback will also be called if visible item range changes after a layout
* calculation. In that case, dx and dy will be 0.
*
* @param recyclerView The RecyclerView which scrolled.
* @param dx The amount of horizontal scroll.
* @param dy The amount of vertical scroll.
*/
public void onScrolled(RecyclerView recyclerView, int dx, int dy){}
}
通多源碼的注釋可以了解到
onScrollStateChanged 當recyclerview的滾動狀態(tài)發(fā)生變化的時候調用。
onScrolled 在布局可見和recycleview滾動的時候調用。
那么思路就是:
(1)在onScrollStateChanged 方法中判斷當前的滾動狀態(tài)是停止?jié)L動的狀態(tài)。
(2)然后根據api中的方法獲得最后可見的位置。
(3)判斷當前可見的recycleview中item的條數(shù)大于0
(4)判斷最后可見的位置大于數(shù)大于item總數(shù)減一
(5)并且item的總數(shù)大于可見的item 這樣可以保證超過一個界面的時候才執(zhí)行。
當滿足讓面的要求的時候我們就可以通過接口回調執(zhí)行我們的耗時邏輯 ,并顯示出加載的dialog。
因為RecyclerView可以通過layoutManager靈活的轉換成列表,表格,和瀑布流。尤其是瀑布流的時候,它的最后可見的位置是不一樣的,所以我們必須根據其不同的layoutManager狀態(tài)獲取相對應的最后可見位置。
代碼:
@Override
public void onScrollStateChanged(int state) {
if (state == RecyclerView.SCROLL_STATE_IDLE && mLoadingListener != null) {
LayoutManager layoutManager = getLayoutManager();
int lastVisibleItemPosition;
if (layoutManager instanceof GridLayoutManager) {
lastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
int[] into = new int[((StaggeredGridLayoutManager) layoutManager).getSpanCount()];
((StaggeredGridLayoutManager) layoutManager).findLastVisibleItemPositions(into);
lastVisibleItemPosition = findMax(into);
} else {
lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
}
if (layoutManager.getChildCount() > 0
&& lastVisibleItemPosition >= layoutManager.getItemCount() - 1 && layoutManager.getItemCount() > layoutManager.getChildCount()) {
View footView = mFootViews.get(0);
footView.setVisibility(View.VISIBLE);
mLoadingListener.onLoadMore();
}
}
}
我們可以通過api獲取瀑布流的所有的列 ,通過下面的方法找出最下面的一列。將加載的dialog顯示在此列的下面。
private int findMax(int[] lastPositions) {
int max = lastPositions[0];
for (int value : lastPositions) {
if (value > max) {
max = value;
}
}
return max;
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Android ExpandableListView實現(xiàn)下拉刷新和加載更多效果
- Android實踐之帶加載效果的下拉刷新上拉加載更多
- android使用PullToRefresh框架實現(xiàn)ListView下拉刷新上拉加載更多
- Android中使用ScrollView實現(xiàn)滑動到底部顯示加載更多
- Android RecyclerView添加上拉加載更多效果
- Android中RecycleView與ViewPager沖突的解決方法及原理
- Android RecycleView使用(CheckBox全選、反選、單選)
- Android 使用RecycleView列表實現(xiàn)加載更多的示例代碼
相關文章
Android開源框架的SlidingFragment的使用示例
今天小編就為大家分享一篇關于Android開源框架的SlidingFragment的使用示例,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03
Android開發(fā)中Eclipse報錯及對應處理方法總結
這篇文章主要介紹了Android開發(fā)中Eclipse報錯及對應處理方法,實例匯總了使用eclipse開發(fā)Android項目過程中常見的錯誤提示及對應的處理技巧,需要的朋友可以參考下2015-12-12
Android Fragment實現(xiàn)列表和內容聯(lián)動
這篇文章主要為大家詳細介紹了Android Fragment實現(xiàn)列表和內容聯(lián)動效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-01-01

