如何创建一个圆形(无休止的)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);
}
最后,将当前 item 设置为中间的某个东西(否则,它只会向下无限).
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屋!