Dynamically Updating GridLayoutManager's SpanCount Property

Nathan Sokalski 4,111 Reputation points
2021-12-11T20:44:40.2+00:00

I have a RecyclerView that uses a GridLayoutManager, and I need to dynamically calculate the SpanCount value. I am able to calculate this value in the LayoutChange event, but if I attempt to edit the GridLayoutManager by setting the SpanCount property or calling SetLayoutManager with a new GridLayoutManager the RecyclerView is not displayed. How/where/when should I update the SpanCount? Note: I will only need to recalculate the SpanCount during OnResume, because the width of the area where my RecyclerView is located does not change

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

Accepted answer
  1. Anonymous
    2021-12-13T06:00:15.587+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    I put the RecycleView in the Linearlayout, then I set the LayoutChange event for Linearlayout, I use set SpanCount like following code, it could Updating GridLayoutManager's SpanCount dynamically.

       PhotoAlbumAdapter mAdapter = new PhotoAlbumAdapter(photoAlbum);  
                   mRecyclerView.SetAdapter(mAdapter);  
         
                   GridLayoutManager mLayoutManager = new GridLayoutManager(this,2);  
                
                   mRecyclerView.SetLayoutManager(mLayoutManager);  
                   linearLayout1.LayoutChange += (o, e) =>  
                   {  
                       mLayoutManager.SpanCount = 3;  
                   };  
    

    If you still cannot work, please share your part of code or demo that could reproduce this issue.

    Best Regards,

    Leon Lu


    If the answer is the right solution, 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 comments No comments

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.