Himavan
Himavan

Reputation: 397

How to set custom positions in recyclerview gridlayout manager?

I have twelve items. I use recyclerview along with GridLayoutmanager to display items. My current code

  RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this,2, GridLayoutManager.HORIZONTAL, false);
        recyclerViewSlots.setLayoutManager(layoutManager);

has give following formatList of items

I want to display items in the following format.

List of items

Upvotes: 1

Views: 751

Answers (1)

Milszym
Milszym

Reputation: 376

You can do it by reversing both: grid layout manager and list of objects that you are passing to the grid adapter.

So if your full code with adapter assignment looks like this:

List<Integer> numbers = new ArrayList();
for (int i = 1; i <= 12; i++) {
    numbers.add(i);
}
YourGridAdapter adapter = new YourGridAdapter(numbers);

RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this,2, GridLayoutManager.HORIZONTAL, false);

RecyclerView recyclerView = findViewById(R.id.recyclerViewSlots);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(layoutManager);

Then you can just change it into:

List<Integer> numbers = new ArrayList();
for (int i = 1; i <= 12; i++) {
    numbers.add(i);
}
Collections.reverse(numbers);
YourGridAdapter adapter = new YourGridAdapter(numbers);

RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this,2, GridLayoutManager.HORIZONTAL, true);

RecyclerView recyclerView = findViewById(R.id.recyclerViewSlots);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(layoutManager);

If that does not satisfy you and you have static number of items you could also consider using GridLayout and putting inside of it all 12 views with manual positioning in xml.

Upvotes: 1

Related Questions