谢谢
@Override public void onScrolled(RecyclerView recyclerView, int dx, int dy)
{
super.onScrolled(recyclerView, dx, dy);
StaggeredGridLayoutManager manager =
(StaggeredGridLayoutManager) recyclerView.getLayoutManager();
int visibleItemCount = manager.getChildCount();
int totalItemCount = manager.getItemCount();
int[] firstVisibleItems = manager.findFirstVisibleItemPositions(null);
if (firstVisibleItems != null && firstVisibleItems.length > 0) {
}
if (visibleItemCount + pastVisibleItems >= totalItemCount) {
}
}
答案 0 :(得分:0)
尝试一下:
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy)
{
super.onScrolled(recyclerView, dx, dy);
StaggeredGridLayoutManager manager = (StaggeredGridLayoutManager) recyclerView.getLayoutManager();
if(manager.findLastVisibleItemPosition() == manager.getItemCount()-1)
{
// We have reached the end of the recycler view.
}
}
答案 1 :(得分:-1)
检查此开源库,例如: https://github.com/Malinskiy/SuperRecyclerView
它具有setupMoreLoading方法,当X
元素要滚动时,它会附加侦听器和调用方法。
这是在这里完成的方式:
...
mInternalOnScrollListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
processOnMore();
if (mExternalOnScrollListener != null)
mExternalOnScrollListener.onScrolled(recyclerView, dx, dy);
if (mSwipeDismissScrollListener != null)
mSwipeDismissScrollListener.onScrolled(recyclerView, dx, dy);
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (mExternalOnScrollListener != null)
mExternalOnScrollListener.onScrollStateChanged(recyclerView, newState);
if (mSwipeDismissScrollListener != null)
mSwipeDismissScrollListener.onScrollStateChanged(recyclerView, newState);
}
};
mRecycler.addOnScrollListener(mInternalOnScrollListener);
....
private void processOnMore() {
RecyclerView.LayoutManager layoutManager = mRecycler.getLayoutManager();
int lastVisibleItemPosition = getLastVisibleItemPosition(layoutManager);
int visibleItemCount = layoutManager.getChildCount();
int totalItemCount = layoutManager.getItemCount();
if (((totalItemCount - lastVisibleItemPosition) <= ITEM_LEFT_TO_LOAD_MORE ||
(totalItemCount - lastVisibleItemPosition) == 0 && totalItemCount > visibleItemCount)
&& !isLoadingMore) {
isLoadingMore = true;
if (mOnMoreListener != null) {
mMoreProgress.setVisibility(View.VISIBLE);
mOnMoreListener.onMoreAsked(mRecycler.getAdapter().getItemCount(), ITEM_LEFT_TO_LOAD_MORE, lastVisibleItemPosition);
}
}
}