下拉刷新完成之后界逛,接下來(lái)當(dāng)然是加載更多,加載更多也是在下拉刷新的基礎(chǔ)上封裝的纺座。具體邏輯差不多仇奶,下面直接上代碼
一、RecyclerView封裝類(lèi)
public class LoadRefreshRecyclerView extends RefreshRecyclerView {
// 上拉加載更多的輔助類(lèi)
private LoadViewCreator mLoadCreator;
// 上拉加載更多頭部的高度
private int mLoadViewHeight = 0;
// 上拉加載更多的頭部View
private View mLoadView;
// 手指按下的Y位置
private int mFingerDownY;
// 當(dāng)前是否正在拖動(dòng)
private boolean mCurrentDrag = false;
// 當(dāng)前的狀態(tài)
private int mCurrentLoadStatus;
// 默認(rèn)狀態(tài)
public int LOAD_STATUS_NORMAL = 0x0011;
// 上拉加載更多狀態(tài)
public static int LOAD_STATUS_PULL_DOWN_REFRESH = 0x0022;
// 松開(kāi)加載更多狀態(tài)
public static int LOAD_STATUS_LOOSEN_LOADING = 0x0033;
// 正在加載更多狀態(tài)
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);
}
// 先處理上拉加載更多比驻,同時(shí)考慮加載列表的不同風(fēng)格樣式该溯,確保這個(gè)項(xiàng)目還是下一個(gè)項(xiàng)目都能用
// 所以我們不能直接添加View,需要利用輔助類(lèi)
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:
// 記錄手指按下的位置 ,之所以寫(xiě)在dispatchTouchEvent那是因?yàn)槿绻覀兲幚砹藯l目點(diǎn)擊事件别惦,
// 那么就不會(huì)進(jìn)入onTouchEvent里面狈茉,所以只能在這里獲取
mFingerDownY = (int) ev.getRawY();
break;
case MotionEvent.ACTION_UP:
if (mCurrentDrag) {
restoreLoadView();
}
break;
}
return super.dispatchTouchEvent(ev);
}
/**
* 重置當(dāng)前加載更多狀態(tài)
*/
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) {
// 如果沒(méi)有到達(dá)最頂端掸掸,也就是說(shuō)還可以向上滾動(dòng)就什么都不處理
return super.onTouchEvent(e);
}
if (mLoadCreator != null) {
mLoadViewHeight = mLoadView.getMeasuredHeight();
}
// 解決上拉加載更多自動(dòng)滾動(dòng)問(wèn)題
if (mCurrentDrag) {
scrollToPosition(getAdapter().getItemCount() - 1);
}
// 獲取手指觸摸拖拽的距離
int distanceY = (int) ((e.getRawY() - mFingerDownY) * mDragIndex);
// 如果是已經(jīng)到達(dá)頭部氯庆,并且不斷的向下拉,那么不斷的改變r(jià)efreshView的marginTop的值
if (distanceY < 0) {
setLoadViewMarginBottom(-distanceY);
updateLoadStatus(-distanceY);
mCurrentDrag = true;
return true;
}
break;
}
return super.onTouchEvent(e);
}
/**
* 更新加載的狀態(tài)
*/
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;
}
}
}
/**
* 設(shè)置加載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.
* 判斷是不是滾動(dòng)到了最頂部扰付,這個(gè)是從SwipeRefreshLayout里面copy過(guò)來(lái)的源代碼
*/
public boolean canScrollDown() {
return ViewCompat.canScrollVertically(this, 1);
}
/**
* 停止加載更多
*/
public void onStopLoad() {
mCurrentLoadStatus = LOAD_STATUS_NORMAL;
restoreLoadView();
if (mLoadCreator != null) {
mLoadCreator.onStopLoad();
}
}
// 處理加載更多回調(diào)監(jiān)聽(tīng)
private OnLoadMoreListener mListener;
public void setOnLoadMoreListener(OnLoadMoreListener listener) {
this.mListener = listener;
}
public interface OnLoadMoreListener {
void onLoad();
}}
二堤撵、輔助類(lèi)
public abstract class LoadViewCreator {
/**
* 獲取上拉加載更多的View
*
* @param context 上下文
* @param parent RecyclerView
*/
public abstract View getLoadView(Context context, ViewGroup parent);
/**
* 正在上拉
*
* @param currentDragHeight 當(dāng)前拖動(dòng)的高度
* @param loadViewHeight 總的加載高度
* @param currentLoadStatus 當(dāng)前狀態(tài)
*/
public abstract void onPull(int currentDragHeight, int loadViewHeight, int currentLoadStatus);
/**
* 正在加載中
*/
public abstract void onLoading();
/**
* 停止加載
*/
public abstract void onStopLoad();
}
使用和下來(lái)加載差不多
三、下拉刷新和上拉加載更多使用
// 添加頭部和底部刷新效果
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);
}