I have a list of let's say 20 items. I want to be able to load them like this
1 11
2 12
3 13
4 14
5 15
6 16
7 17
8 18
9 19
10 20
I'm using a RecyclerView
with a GridLayoutManager
Using GridLayoutManager(context, 2)
, it loads the items as follows
1 2
3 4
5 6
7 8
....
Which isn't my desired output
The simplest way to achieve this would be sort the ArrayList accordingly to the required order, you pass the your arraylist to rearrangeTheArrayList function get the new ArrayList and pass this to recycler view Adaptor to achieve above mentioned order .
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;
}