Created
July 14, 2021 08:13
-
-
Save puke3615/f15a14fbd49f96bbae202bb43e2901df to your computer and use it in GitHub Desktop.
RecyclerViewLoadMoreHelper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.support.annotation.NonNull; | |
import android.support.v7.widget.LinearLayoutManager; | |
import android.support.v7.widget.RecyclerView; | |
import android.support.v7.widget.StaggeredGridLayoutManager; | |
/** | |
* 封装加载更多通用辅助类 | |
* | |
* @author puke | |
* @version 2020-07-24 | |
*/ | |
public class RecyclerViewLoadMoreHelper { | |
private static final int DEFAULT_LOAD_MORE_WHEN_SHOW_LAST_COUNT = 10; | |
private final RecyclerView recyclerView; | |
private final int loadMoreWhenShowLastCount; | |
private OnLoadMoreListener loadMoreListener; | |
private boolean locked; | |
private boolean enabled = true; | |
public interface OnLoadMoreListener { | |
void onLoadMore(); | |
} | |
public RecyclerViewLoadMoreHelper(RecyclerView recyclerView) { | |
this(recyclerView, DEFAULT_LOAD_MORE_WHEN_SHOW_LAST_COUNT); | |
} | |
public RecyclerViewLoadMoreHelper(RecyclerView recyclerView, int loadMoreWhenShowLastCount) { | |
this.recyclerView = recyclerView; | |
this.loadMoreWhenShowLastCount = loadMoreWhenShowLastCount; | |
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { | |
@Override | |
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { | |
super.onScrolled(recyclerView, dx, dy); | |
doCheck(); | |
} | |
}); | |
} | |
/** | |
* 注册加载更多回调 | |
*/ | |
public void setOnLoadMoreListener(OnLoadMoreListener loadMoreListener) { | |
this.loadMoreListener = loadMoreListener; | |
} | |
public void setEnabled(boolean enabled) { | |
this.enabled = enabled; | |
} | |
/** | |
* 锁定加载更多, 调用之后, 不再回调加载更多事件 | |
*/ | |
public void lock() { | |
locked = true; | |
} | |
/** | |
* 解锁加载更多, 调用之后, 继续回调加载更多事件 | |
*/ | |
public void unlock() { | |
locked = false; | |
} | |
public void doCheck() { | |
doCheck(false); | |
} | |
/** | |
* 手动触发加载更多检测逻辑 | |
* | |
* @param force 是否强制检测 true: 忽略lock逻辑 | |
*/ | |
public void doCheck(boolean force) { | |
if (!enabled) { | |
return; | |
} | |
if (locked && !force) { | |
return; | |
} | |
RecyclerView.Adapter adapter = recyclerView.getAdapter(); | |
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager(); | |
if (adapter == null) { | |
return; | |
} | |
if (layoutManager instanceof LinearLayoutManager) { | |
int lastVisibleItemPosition = | |
((LinearLayoutManager) layoutManager).findLastVisibleItemPosition(); | |
if (lastVisibleItemPosition >= adapter.getItemCount() - loadMoreWhenShowLastCount) { | |
if (loadMoreListener != null) { | |
loadMoreListener.onLoadMore(); | |
} | |
} | |
} else if (layoutManager instanceof StaggeredGridLayoutManager) { | |
StaggeredGridLayoutManager gridLayoutManager = (StaggeredGridLayoutManager) layoutManager; | |
int[] lastVisibleItemPositions = gridLayoutManager.findLastVisibleItemPositions(null); | |
for (int lastVisibleItemPosition : lastVisibleItemPositions) { | |
if (lastVisibleItemPosition >= adapter.getItemCount() - loadMoreWhenShowLastCount) { | |
if (loadMoreListener != null) { | |
loadMoreListener.onLoadMore(); | |
} | |
break; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment