支持下拉刷新、上拉加载的RecyclerView,基于PullToRefresh
2015-12-23 09:27
471 查看
首先我是使用 这篇文章http://blog.csdn.net/Mr_WangGang/article/details/46707441 提供的方法来实现的,感谢提供思路。
但是使用过程中,发现一个bug:上拉加载完成后,必须要向下滑一下再向上滑才能向下滚动,不然就一直判断成是上拉加载状态。
所以在此基础上,参考listview的方法判断是否应该上拉加载,重写了一下isReadyForPullEnd()这个方法。
但是使用过程中,发现一个bug:上拉加载完成后,必须要向下滑一下再向上滑才能向下滚动,不然就一直判断成是上拉加载状态。
所以在此基础上,参考listview的方法判断是否应该上拉加载,重写了一下isReadyForPullEnd()这个方法。
<span style="font-size:14px;">public class PullToRefreshRecycleView extends PullToRefreshBase<RecyclerView> { private RecyclerView mRefreshableView; public PullToRefreshRecycleView(Context context) { super(context); } public PullToRefreshRecycleView(Context context, AttributeSet attrs) { super(context, attrs); } public PullToRefreshRecycleView(Context context, Mode mode) { super(context, mode); } public PullToRefreshRecycleView(Context context, Mode mode, AnimationStyle animStyle) { super(context, mode, animStyle); } //重写4个方法 //1 滑动方向 @Override public Orientation getPullToRefreshScrollDirection() { return Orientation.VERTICAL; } //滑动的View @Override protected RecyclerView createRefreshableView(Context context, AttributeSet attrs) { mRefreshableView = new RecyclerView(context, attrs); return mRefreshableView; } //是否滑动到底部 @Override protected boolean isReadyForPullEnd() { return isLastItemVisible(); } //是否滑动到顶部 @Override protected boolean isReadyForPullStart() { View view = mRefreshableView.getChildAt(0); if (view != null) { return view.getTop() >= mRefreshableView.getTop(); } return false; } private boolean isLastItemVisible() { final RecyclerView.Adapter adapter = mRefreshableView.getAdapter(); if (null == adapter) { return true; } else { LinearLayoutManager layoutManager = (LinearLayoutManager) mRefreshableView.getLayoutManager(); final int lastItemPosition = mRefreshableView.getAdapter().getItemCount() -1; final int lastVisiblePosition = layoutManager.findLastVisibleItemPosition(); /** * This check should really just be: lastVisiblePosition == * lastItemPosition, but PtRListView internally uses a FooterView * which messes the positions up. For me we'll just subtract one to * account for it and rely on the inner condition which checks * getBottom(). */ if (lastVisiblePosition >= lastItemPosition - 1) { final int childIndex = lastVisiblePosition - layoutManager.findFirstVisibleItemPosition(); final View lastVisibleChild = mRefreshableView.getChildAt(childIndex); if (lastVisibleChild != null) { return lastVisibleChild.getBottom() <= mRefreshableView.getBottom(); } } } return false; } }</span>
相关文章推荐
- MATLAB数据导入导出
- Android手机Shell
- char&nbsp;*与NSString*的相互转化
- oc中在NSdate中获取年月日的方法
- 向其他线程创建的控件中添加内容
- 向其他线程创建的控件中添加内容
- IOS中各种动画特效的实现
- cocos2d-x&nbsp;中的字体
- 在cocos2d-x中使用sqlite
- cocos2d-x&nbsp;获取系统当前时间
- C++得到当前进程所占用的内…
- C++遍历文件夹下的所有文件
- mbstowcs,wcstombs,WideChar…
- 获取当前目录下所有文件的文件名
- 宽字符与字符串的相互转换
- 宽字符转字符串
- cocos2dx绘制实心圆
- VC2008获取指定的窗口句柄
- VC中通过程序名称找到窗口句…
- mouse_event函数