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

首先我是使用 这篇文章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>


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值