Share via

Access Adapter Items In GridLayoutManager.SpanSizeLookup & GetSpanSize

Nathan Sokalski 4,111 Reputation points
2022-03-05T21:55:52.317+00:00

I have a RecyclerView that uses a GridLayoutManager, and I want to center the items in the last row. Based on what I have found, this is done by creating a GridLayoutManager.SpanSizeLookup & overriding GetSpanSize. I believe I understand how to do this, except for the fact that I need access to the Adapter (or at least the item count & the SpanCount set for the GridLayoutManager) in order to calculate the value. Is there a way to access this information from a SpanSizeLookup (without passing in extra variable)? How can I access the span size using only the position?

Developer technologies | .NET | Xamarin
0 comments No comments

1 answer

Sort by: Most helpful
  1. Anonymous
    2022-03-07T08:24:27.353+00:00

    Hello,​

    Is there a way to access this information from a SpanSizeLookup (without passing in extra variable)? How can I access the span size using only the position?

    I cannot find this Api that get adapter Items in the GridLayoutManager.SpanSizeLookup without passing variable and getSpanSize.

    We use myAdapter.GetItemViewType(position) to get items normally.

       internal class MySpanSizeLookup : GridLayoutManager.SpanSizeLookup  
           {  
               private MyAdapter myAdapter;  
         
               public MySpanSizeLookup(MyAdapter myAdapter)  
               {  
                   this.myAdapter = myAdapter;  
               }  
         
               public override int GetSpanSize(int position)  
               {  
                   switch (myAdapter.GetItemViewType(position))  
                   {  
                       case MyAdapter.HEADER:  
                           return 1;  
                       case MyAdapter.ITEM:  
                           return 2;  
                       default:  
                           return -1;  
                   }  
         
                   
               }  
           }  
    

    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.

    Was this answer helpful?

    0 comments No comments

Your answer

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