RecyclerView系列(六)加载更多的RecyclerView封装

下拉刷新完成之后,接下来当然是加载更多,加载更多也是在下拉刷新的基础上封装的。具体逻辑差不多,下面直接上代码

一、RecyclerView封装类

public class LoadRefreshRecyclerView extends RefreshRecyclerView {
// 上拉加载更多的辅助类
private LoadViewCreator mLoadCreator;
// 上拉加载更多头部的高度
private int mLoadViewHeight = 0;
// 上拉加载更多的头部View
private View mLoadView;
// 手指按下的Y位置
private int mFingerDownY;
// 当前是否正在拖动
private boolean mCurrentDrag = false;
// 当前的状态
private int mCurrentLoadStatus;
// 默认状态
public int LOAD_STATUS_NORMAL = 0x0011;
// 上拉加载更多状态
public static int LOAD_STATUS_PULL_DOWN_REFRESH = 0x0022;
// 松开加载更多状态
public static int LOAD_STATUS_LOOSEN_LOADING = 0x0033;
// 正在加载更多状态
public int LOAD_STATUS_LOADING = 0x0044;

public LoadRefreshRecyclerView(Context context) {
    super(context);
}

public LoadRefreshRecyclerView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
}

public LoadRefreshRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

// 先处理上拉加载更多,同时考虑加载列表的不同风格样式,确保这个项目还是下一个项目都能用
// 所以我们不能直接添加View,需要利用辅助类
public void addLoadViewCreator(LoadViewCreator loadCreator) {
    this.mLoadCreator = loadCreator;
    addRefreshView();
}

@Override
public void setAdapter(Adapter adapter) {
    super.setAdapter(adapter);
    addRefreshView();
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            // 记录手指按下的位置 ,之所以写在dispatchTouchEvent那是因为如果我们处理了条目点击事件,
            // 那么就不会进入onTouchEvent里面,所以只能在这里获取
            mFingerDownY = (int) ev.getRawY();
            break;

        case MotionEvent.ACTION_UP:
            if (mCurrentDrag) {
                restoreLoadView();
            }
            break;
    }
    return super.dispatchTouchEvent(ev);
}

/**
 * 重置当前加载更多状态
 */
private void restoreLoadView() {
    int currentBottomMargin = ((MarginLayoutParams) mLoadView.getLayoutParams()).bottomMargin;
    int finalBottomMargin = 0;
    if (mCurrentLoadStatus == LOAD_STATUS_LOOSEN_LOADING) {
        mCurrentLoadStatus = LOAD_STATUS_LOADING;
        if (mLoadCreator != null) {
            mLoadCreator.onLoading();
        }
        if (mListener != null) {
            mListener.onLoad();
        }
    }

    int distance = currentBottomMargin - finalBottomMargin;

    // 回弹到指定位置
    ValueAnimator animator = ObjectAnimator.ofFloat(currentBottomMargin, finalBottomMargin).setDuration(distance);
    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            float currentTopMargin = (float) animation.getAnimatedValue();
            setLoadViewMarginBottom((int) currentTopMargin);
        }
    });
    animator.start();
    mCurrentDrag = false;
}


@Override
public boolean onTouchEvent(MotionEvent e) {
    switch (e.getAction()) {
        case MotionEvent.ACTION_MOVE:
            // 如果是在最底部才处理,否则不需要处理
            if (canScrollDown() || mCurrentLoadStatus == LOAD_STATUS_LOADING
                    || mLoadCreator == null || mLoadView == null) {
                // 如果没有到达最顶端,也就是说还可以向上滚动就什么都不处理
                return super.onTouchEvent(e);
            }

            if (mLoadCreator != null) {
                mLoadViewHeight = mLoadView.getMeasuredHeight();
            }
            // 解决上拉加载更多自动滚动问题
            if (mCurrentDrag) {
                scrollToPosition(getAdapter().getItemCount() - 1);
            }

            // 获取手指触摸拖拽的距离
            int distanceY = (int) ((e.getRawY() - mFingerDownY) * mDragIndex);
            // 如果是已经到达头部,并且不断的向下拉,那么不断的改变refreshView的marginTop的值
            if (distanceY < 0) {
                setLoadViewMarginBottom(-distanceY);
                updateLoadStatus(-distanceY);
                mCurrentDrag = true;
                return true;
            }
            break;
    }

    return super.onTouchEvent(e);
}

/**
 * 更新加载的状态
 */
private void updateLoadStatus(int distanceY) {
    if (distanceY <= 0) {
        mCurrentLoadStatus = LOAD_STATUS_NORMAL;
    } else if (distanceY < mLoadViewHeight) {
        mCurrentLoadStatus = LOAD_STATUS_PULL_DOWN_REFRESH;
    } else {
        mCurrentLoadStatus = LOAD_STATUS_LOOSEN_LOADING;
    }

    if (mLoadCreator != null) {
        mLoadCreator.onPull(distanceY, mLoadViewHeight, mCurrentLoadStatus);
    }
}

/**
 * 添加底部加载更多View
 */
private void addRefreshView() {
    Adapter adapter = getAdapter();
    if (adapter != null && mLoadCreator != null) {
        // 添加底部加载更多View
        View loadView = mLoadCreator.getLoadView(getContext(), this);
        if (loadView != null) {
            addFooterView(loadView);
            this.mLoadView = loadView;
        }
    }
}

/**
 * 设置加载View的marginBottom
 */
public void setLoadViewMarginBottom(int marginBottom) {
    MarginLayoutParams params = (MarginLayoutParams) mLoadView.getLayoutParams();
    if (marginBottom < 0) {
        marginBottom = 0;
    }
    params.bottomMargin = marginBottom;
    mLoadView.setLayoutParams(params);
}


/**
 * @return Whether it is possible for the child view of this layout to
 * scroll up. Override this if the child view is a custom view.
 * 判断是不是滚动到了最顶部,这个是从SwipeRefreshLayout里面copy过来的源代码
 */
public boolean canScrollDown() {
    return ViewCompat.canScrollVertically(this, 1);
}

/**
 * 停止加载更多
 */
public void onStopLoad() {
    mCurrentLoadStatus = LOAD_STATUS_NORMAL;
    restoreLoadView();
    if (mLoadCreator != null) {
        mLoadCreator.onStopLoad();
    }
}

// 处理加载更多回调监听
private OnLoadMoreListener mListener;

public void setOnLoadMoreListener(OnLoadMoreListener listener) {
    this.mListener = listener;
}

public interface OnLoadMoreListener {
    void onLoad();
}}

二、辅助类

 public abstract class LoadViewCreator {
/**
 * 获取上拉加载更多的View
 *
 * @param context 上下文
 * @param parent  RecyclerView
 */
public abstract View getLoadView(Context context, ViewGroup parent);

/**
 * 正在上拉
 *
 * @param currentDragHeight    当前拖动的高度
 * @param loadViewHeight    总的加载高度
 * @param currentLoadStatus 当前状态
 */
public abstract void onPull(int currentDragHeight, int loadViewHeight, int currentLoadStatus);

/**
 * 正在加载中
 */
public abstract void onLoading();

/**
 * 停止加载
 */
public abstract void onStopLoad();
}

使用和下来加载差不多

三、下拉刷新和上拉加载更多使用

    // 添加头部和底部刷新效果
    mRecyclerView.addRefreshViewCreator(new DefaultRefreshCreator());
    mRecyclerView.addLoadViewCreator(new DefaultLoadCreator());
    mRecyclerView.setOnRefreshListener(this);
    mRecyclerView.setOnLoadMoreListener(this);

 @Override
  public void onRefresh() {
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            mRecyclerView.onStopRefresh();
        }
    }, 2000);
}

@Override
public void onLoad() {
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            initData();
            mRecyclerView.onStopLoad();
            mAdapter.notifyDataSetChanged();
        }
    }, 2000);
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,866评论 25 708
  • 内容抽屉菜单ListViewWebViewSwitchButton按钮点赞按钮进度条TabLayout图标下拉刷新...
    皇小弟阅读 46,871评论 22 665
  • 今天本来很好的心情,去厂里陪伴她。她说要放假了,我听着多高兴啊。于是我就试探性的说我们不如回老家吧,结果又是钱呀神...
    坝周阅读 162评论 0 0
  • 我是我所有幻想的主角, 在你的剧情里苦涩的演了很久。 我被选择的用第一人称, 流着泪哭了很久很久。 你与所有人相衬...
    糜情阅读 190评论 0 1
  • 导读: 林拜得知德仁的现状,鼓励郑秋冬这正是转型的好时机,抛开情分,德仁的财务数据和公司业绩都是上升状态,不过是走...
    课后辅导陈老师阅读 1,026评论 0 1