如何创建循环(无休止)的RecyclerView? [英] How do I create a circular (endless) RecyclerView?
问题描述
我正试图使RecyclerView
循环回到列表的开头.
I am trying to make my RecyclerView
loop back to the start of my list.
我已经在互联网上进行了搜索,并设法检测出何时到达列表末尾,但是我不确定从何处着手.
I have searched all over the internet and have managed to detect when I have reached the end of my list, however I am unsure where to proceed from here.
这是我目前用来检测列表结尾的内容(找到这里):
This is what I am currently using to detect the end of the list (found here):
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
visibleItemCount = mLayoutManager.getChildCount();
totalItemCount = mLayoutManager.getItemCount();
pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition();
if (loading) {
if ( (visibleItemCount+pastVisiblesItems) >= totalItemCount) {
loading = false;
Log.v("...", ""+visibleItemCount);
}
}
}
当滚动到末尾时,我希望视图是可见的,而从列表顶部显示数据时,或者当滚动到列表顶部时,我将从列表底部显示数据.
When scrolled to the end, I would like to views to be visible while the displaying data from the top of the list or when scrolled to the top of the list I would display data from the bottom of the list.
例如:
View1 View2 View3 View4 View5
View1 View2 View3 View4 View5
View5 View1 View2 View3 View4
View5 View1 View2 View3 View4
推荐答案
没有使其无限的方法,但是有一种使其看起来无限的方法.
There is no way of making it infinite, but there is a way to make it look like infinite.
-
适配器中的
-
覆盖
getCount()
以返回类似Integer.MAX_VALUE
的大内容:
in your adapter override
getCount()
to return something big likeInteger.MAX_VALUE
:
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
getItem()
和getView()
中的
模除(%)位置除以实际物料编号:
in getItem()
and getView()
modulo divide (%) position by real item number:
@Override
public Fragment getItem(int position) {
int positionInList = position % fragmentList.size();
return fragmentList.get(positionInList);
}
最后,将当前项目设置为中间的某个项目(否则,它将仅在向下方向无穷无尽).
at the end, set current item to something in the middle (or else, it would be endless only in downward direction).
// scroll to middle item
recyclerView.getLayoutManager().scrollToPosition(Integer.MAX_VALUE / 2);
这篇关于如何创建循环(无休止)的RecyclerView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!