支持下拉刷新、上拉加载的RecyclerView,基于PullToRefresh

来源:互联网 发布:淘宝网买卖中心 编辑:程序博客网 时间:2024/05/18 08:22

首先我是使用 这篇文章http://blog.csdn.net/Mr_WangGang/article/details/46707441 提供的方法来实现的,感谢提供思路。

但是使用过程中,发现一个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>


0 0
原创粉丝点击