RecyclerView实现加载更多
2016-06-21 18:11
218 查看
情景描述
我们使用列表显示数据时,通常都会遇到需要加载更多数据的情况,比如列表即将滚动到底部时,我们就需要进行新的数据的获取及填充。本篇博客介绍的是:自定义RecyclerView,添加即将滚动到底部的回调,方便实现加载更多功能。代码
/** * Add the callback of reaching bottom into RecyclerView * Support three LayoutManager(Linear、Grid、StaggeredGrid) * Created by wiky on 2016/3/26. */ public class RBCallbkRecyclerView extends RecyclerView { private OnReachBottomListener onReachBottomListener; private boolean isInTheBottom = false; /** * reachBottomRow = 1;(default) * mean : when the lastVisibleRow is lastRow , call the onReachBottom(); * reachBottomRow = 2; * mean : when the lastVisibleRow is Penultimate Row , call the onReachBottom(); * And so on */ private int reachBottomRow = 1; public RBCallbkRecyclerView(Context context) { super(context); } public RBCallbkRecyclerView(Context context, AttributeSet attrs) { super(context, attrs); } public RBCallbkRecyclerView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public void onScrolled(int dx, int dy) { super.onScrolled(dx, dy); if (onReachBottomListener != null) { LayoutManager layoutManager = getLayoutManager(); if (layoutManager == null) { //it maybe unnecessary throw new RuntimeException("LayoutManager is null,Please check it!"); } Adapter adapter = getAdapter(); if (adapter == null) { //it maybe unnecessary throw new RuntimeException("Adapter is null,Please check it!"); } boolean isReachBottom = false; //is GridLayoutManager if (layoutManager instanceof GridLayoutManager) { GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager; int rowCount = adapter.getItemCount() / gridLayoutManager.getSpanCount(); int lastVisibleRowPosition = gridLayoutManager.findLastVisibleItemPosition() / gridLayoutManager.getSpanCount(); isReachBottom = (lastVisibleRowPosition >= rowCount - reachBottomRow); } //is LinearLayoutManager else if (layoutManager instanceof LinearLayoutManager) { int lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition(); int rowCount = adapter.getItemCount(); if (reachBottomRow > rowCount) reachBottomRow = 1; isReachBottom = (lastVisibleItemPosition >= rowCount - reachBottomRow); } //is StaggeredGridLayoutManager else if (layoutManager instanceof StaggeredGridLayoutManager) { StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager; int spanCount = staggeredGridLayoutManager.getSpanCount(); int[] into = new int[spanCount]; int[] eachSpanListVisibleItemPosition = staggeredGridLayoutManager.findLastVisibleItemPositions(into); for (int i = 0; i < spanCount; i++) { if (eachSpanListVisibleItemPosition[i] > adapter.getItemCount() - reachBottomRow * spanCount) { isReachBottom = true; break; } } } if (!isReachBottom) { isInTheBottom = false; } else if (!isInTheBottom) { onReachBottomListener.onReachBottom(); isInTheBottom = true; Log.d("RBCallbkRecyclerView", "onReachBottom"); } } } public void setReachBottomRow(int reachBottomRow) { if (reachBottomRow < 1) reachBottomRow = 1; this.reachBottomRow = reachBottomRow; } public interface OnReachBottomListener { void onReachBottom(); } public void setOnReachBottomListener(OnReachBottomListener onReachBottomListener) { this.onReachBottomListener = onReachBottomListener; } }
使用
基础使用和RecyclerView无差别添加接口回调方法:
mRBCallbkRecyclerView.setOnReachBottomListener(new RBCallbkRecyclerView.OnReachBottomListener() { @Override public void onReachBottom() { //即将到达几部,进行加载更多操作 } });
同时支持设置还剩X行数据时,即进行回调
mRBCallbkRecyclerView.setReachBottomRow(1);//(默认)最后一行可见时,进行回调
相关文章推荐
- ThinkPHP的事务回滚了却还能插入数据到mysql数据库
- 欧拉函数的性质及应用
- 模板语言语法
- android studio 使用友盟多渠道打包
- 非条件判断
- 组策略应用——限制计算机接入
- STL之vector
- python 类的私有属性和方法
- 通过JavaScript实现漂浮
- Appium移动自动化测试(二)——安装Android开发环境
- block的使用
- JSP之邮箱检验
- 如何做好项目需求获取与分析(转载)
- nslookup命令
- 百度开放云物接入IoT--Java客户端
- Codeforces #354D (Div. 2) 暴力BFS
- 拦截器和过滤器的区别
- MySQL一些SQL技巧
- 微信网页开发实践与总结
- android 创建xml