交错的相似回收者视图
Staggered Alike Recycler View
我想实现项目列表,如下所示。
- 如果项目总数 = 1(显示完整项目)
- 如果项目总数 = 2(在 2 列中显示 2 个项目)
- 如果项目总数 = 3(显示 3 个项目,第 1 列中有 2 个,第 2 列中有 1 个)
- 等等...
如果有 5 项和 6 项,该部分应如下所示滚动
I want to implement this, as one of my section in my application. What is the correct way of implementing this in Android, I need a starting point. Is this possible with a recycler view? Please share any library or github project if available.
可以使用自定义布局管理。我们可以根据需要修改布局管理器。
我使用了 vlayout git 库中的 DelegateAdapter,它真的很棒。
查看下方 link 了解更多详情。
是的,您可以使用 StaggeredLayoutManager 实现此目的,其中您需要根据您的数据设置 span count & Orientation尺寸如下代码:
enter code private void setSpanValue() {
StaggeredGridLayoutManager staggeredGridLayoutManager;
if (modelArrayList.size() == 1) {
staggeredGridLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.HORIZONTAL);
recyclerView.setLayoutManager(staggeredGridLayoutManager);
} else if (modelArrayList.size() == 2) {
staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(staggeredGridLayoutManager);
} else if (modelArrayList.size() == 3) {
staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(staggeredGridLayoutManager);
} else if (modelArrayList.size() == 4) {
staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(staggeredGridLayoutManager);
} else {
staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.HORIZONTAL);
recyclerView.setLayoutManager(staggeredGridLayoutManager);
}
recyclerView.setLayoutManager(staggeredGridLayoutManager);
recyclerView.hasFixedSize();
Adapter adapter = new Adapter(this, modelArrayList, checkEvenOrOdd(modelArrayList.size()));
recyclerView.setAdapter(adapter);
}
对于超过 4 个项目,您需要检查数据是偶数还是奇数,如下代码所示:
private boolean checkEvenOrOdd(int n) {
if ((n % 2) == 0) {
// number is even
return true;
} else {
// number is odd
return false;
}
}
查看我的示例项目,它将解决您的问题
https://github.com/DanishAmjad12/HetrogenousLayout
我想实现项目列表,如下所示。
- 如果项目总数 = 1(显示完整项目)
- 如果项目总数 = 2(在 2 列中显示 2 个项目)
- 如果项目总数 = 3(显示 3 个项目,第 1 列中有 2 个,第 2 列中有 1 个)
- 等等...
如果有 5 项和 6 项,该部分应如下所示滚动
I want to implement this, as one of my section in my application. What is the correct way of implementing this in Android, I need a starting point. Is this possible with a recycler view? Please share any library or github project if available.
可以使用自定义布局管理。我们可以根据需要修改布局管理器。
我使用了 vlayout git 库中的 DelegateAdapter,它真的很棒。 查看下方 link 了解更多详情。
是的,您可以使用 StaggeredLayoutManager 实现此目的,其中您需要根据您的数据设置 span count & Orientation尺寸如下代码:
enter code private void setSpanValue() {
StaggeredGridLayoutManager staggeredGridLayoutManager;
if (modelArrayList.size() == 1) {
staggeredGridLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.HORIZONTAL);
recyclerView.setLayoutManager(staggeredGridLayoutManager);
} else if (modelArrayList.size() == 2) {
staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(staggeredGridLayoutManager);
} else if (modelArrayList.size() == 3) {
staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(staggeredGridLayoutManager);
} else if (modelArrayList.size() == 4) {
staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(staggeredGridLayoutManager);
} else {
staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.HORIZONTAL);
recyclerView.setLayoutManager(staggeredGridLayoutManager);
}
recyclerView.setLayoutManager(staggeredGridLayoutManager);
recyclerView.hasFixedSize();
Adapter adapter = new Adapter(this, modelArrayList, checkEvenOrOdd(modelArrayList.size()));
recyclerView.setAdapter(adapter);
}
对于超过 4 个项目,您需要检查数据是偶数还是奇数,如下代码所示:
private boolean checkEvenOrOdd(int n) {
if ((n % 2) == 0) {
// number is even
return true;
} else {
// number is odd
return false;
}
}
查看我的示例项目,它将解决您的问题 https://github.com/DanishAmjad12/HetrogenousLayout