我有一个包含 20 项的列表。我希望能够像这样加载它们
1 11
2 12
3 13
4 14
5 15
6 16
7 17
8 18
9 19
10 20
我正在使用 RecyclerView
和 GridLayoutManager
使用 GridLayoutManager(context, 2)
,它加载项目如下
1 2
3 4
5 6
7 8
....
这不是我想要的输出
最佳答案
实现此目的的最简单方法是根据所需的顺序对 ArrayList 进行排序,将数组列表传递给 rearrangeTheArrayList 函数,获取新的 ArrayList 并将其传递给回收器 View 适配器以实现上述目的订单。
public ArrayList<Integer> rearrangeTheArrayList(ArrayList<Integer> integerArrayList) {
ArrayList<Integer> resultArrayList = new ArrayList<>();
int halfLength = 0;
if (integerArrayList.size() % 2 == 1) {
halfLength = (integerArrayList.size() / 2) + 1;
} else {
halfLength = integerArrayList.size() / 2;
}
for (int i = 0; i < halfLength; i++) {
resultArrayList.add(integerArrayList.get(i));
if ( (i + halfLength)<(integerArrayList.size() )) {
resultArrayList.add(integerArrayList.get(i + halfLength));
}
}
return resultArrayList;
}
关于Android - GridLayoutManager - 垂直加载列表中的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54880217/