GridLayoutManager Using Wrong Orientation

Nathan Sokalski 4,111 Reputation points
2022-02-09T04:55:59.24+00:00

I have a RecyclerView that uses a GridLayoutManager as follows:

this.rvWinnings.SetLayoutManager(new GridLayoutManager(this, maxbtns, GridLayoutManager.Horizontal, false));

My expectations would be that the first item would be in the upper left, the second item to the right of that, the third to the right, up to maxbtns columns, then start the next row, as follows:

0 1 2 3 4 5
6 7 8 9 10 11

However, it is giving me the following:

0 6
1 7
2 8
3 9
4 10
5 11

If I switch the orientation, then it switches it here, so it's not hard to get what I want, but why does GridLayoutManager interpret the orientations differently than LinearLayoutManager? I want to make sure I am not missing something so I don't end up messing something up later.

Developer technologies .NET Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. JarvanZhang 23,971 Reputation points
    2022-02-10T05:17:20.583+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    The second parameter of the GridLayoutManager's constructor method is spanCount. It represents represents the number of rows or columns in this direction. When the third parameter is GridLayoutManager.Horizontal, 'spanCount' represent the rows. So please set the third parameter to GridLayoutManager.Vertical instead.

       adapter = new CustomAdapter(this, data);  
       var layoutManager = new GridLayoutManager(this, number, GridLayoutManager.Vertical, false);  
       recyclerView.SetAdapter(adapter);  
       recyclerView.SetLayoutManager(layoutManager);  
    

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.