Open YangGui101 opened 4 years ago
同问
同问
同问
private void initRefreshLayout() {
binding.refreshLayout.setOnRefreshListener(refreshLayout -> {
loadData(0);
});
binding.refreshLayout.setOnLoadMoreListener(refreshLayout -> {
loadData(adapter.getCurrentPage() + 1);
});
binding.recycleView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
GridLayoutManager lm = (GridLayoutManager) recyclerView.getLayoutManager();
if (lastVisibleItemPosition != lm.findLastVisibleItemPosition()) {
lastVisibleItemPosition = lm.findLastVisibleItemPosition();
Log.d(TAG, "onScrolled: lastVisibleItemPosition=" + lastVisibleItemPosition);
if (adapter.getItemCount() - lastVisibleItemPosition < 10) {
// 提前预加载
loadData(adapter.getCurrentPage() + 1);
}
}
}
});
}
private int oldLoadPage = 0;
private void loadData(int page) {
// 防止重复加载同一页数据
if (page != 0 && page == oldLoadPage) return;
oldLoadPage = page;
// 访问网络拉取数据
}
自动加载好像是到最底才触发的,请问有自己设置的预加载吗? 例如10条数据,我看到了视图第5条数据就触发加载更多。