如何实现使用StaggeredLayoutManager无尽的滚动 [英] How to implement Endless scrolling using StaggeredLayoutManager
问题描述
我已经尝试过实施LinearLayoutManager无尽的滚动,它是成功的,并试图复制LinearLayoutManager实施StaggeredGridLayoutManager,但它不工作。
I already tried to implement the endless scrolling for LinearLayoutManager and it is successful and tried to copy the LinearLayoutManager implementation to StaggeredGridLayoutManager but it doesn't work.
我只是想获得firstVisibleItem。
I just want to get the firstVisibleItem.
在LinearLayoutManager:
in LinearLayoutManager :
int firstVisibleItem = linearLayoutManager.findFirstVisibleItemPosition(int);
但在StaggeredGridLayoutManager是:
but in StaggeredGridLayoutManager is :
int firstVisibleItem = staggeredGridLayoutManager.findFirstVisibleItemPositions(int[])
如何使用获得firstVisibleItem(INT)不(INT [])?
How to get the firstVisibleItem using (int) not (int[])?
有没有什么好的方法/执行这个呢?
Is there any good approach/implementation about this?
在此先感谢。
推荐答案
我得到它的工作:
您可以使用在StaggeredGridLayoutManager两种方法之一:
You can use one of two methods in the StaggeredGridLayoutManager:
- mLayoutManager.findFirstVisibleItemPositions
- mLayoutManager.findFirstCompletelyVisibleItemPositions
传递,将得到与位置初始化一个空的int数组,并使用一个适合于你。
Pass an empty int array that will get initialized with the positions and use the one that makes sense for you.
private boolean loading = true;
private int pastVisibleItems, visibleItemCount, totalItemCount;
mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener({
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
visibleItemCount = mLayoutManager.getChildCount();
totalItemCount = mLayoutManager.getItemCount();
int[] firstVisibleItems = null;
firstVisibleItems = mLayoutManager.findFirstVisibleItemPositions(firstVisibleItems);
if(firstVisibleItems != null && firstVisibleItems.length > 0) {
pastVisibleItems = firstVisibleItems[0];
}
if (loading) {
if ((visibleItemCount + pastVisibleItems) >= totalItemCount) {
loading = false;
Log.d("tag", "LOAD NEXT ITEM");
}
}
}
});
这篇关于如何实现使用StaggeredLayoutManager无尽的滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!