RecyclerView Width Based On Offscreen & Unrendered Items

Nathan Sokalski 4,111 Reputation points
2021-11-07T03:01:38.017+00:00

I have a RecyclerView which displays a list of buttons. I want all the buttons to be the same width, which should be the width required by the widest button. However, if the widest button is not visible when the RecyclerView is first created (it is offscreen because the user has not yet scrolled to it), it is given a width that is less than required. The xml layout for my items (which is just a Button) has a layout_width of match_parent. Here are screenshots from before & after the user scrolls (I gave the RecyclerView a blue background to make it recognizable in the screenshot):

146947-screenshot-1636251471.png

147020-screenshot-1636251481.png

Notice how in the first screenshot, the RecyclerView (and all the buttons in it, since they have a width of match_parent) is the width of the widest button. But in the second screenshot (which is how it looks after scrolling down), the RecyclerView and it's buttons are still the same size, which is less than what is required by what is now the widest button. The RecyclerView has a width of wrap_content. What I basically need it to either have the width calculated for all buttons (including ones that are not yet visible) or have the width dynamically change when they become visible (preferably #1). How can I force the RecyclerView to base it's width on all items rather than just the visible ones?

Developer technologies | .NET | Xamarin
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2021-11-09T08:29:56.023+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    Could you change the sequence of the List<String> when you display the RecycleView? If so, we can get the longest string in the List, then switch the place between longest string and the first item in the List<String>. then we set the datasource for RecycleView.

    Here is code

       List<string> strs = new List<string>();  
                   //add some strings  strs.Add("No Par");  
         
         
                   //get the longest item index  
                   int MaxTextIndex = 0;  
                   for (int i = 1; i < strs.Count; i++)  
                   {  
                       if (strs[i ].Length > strs[MaxTextIndex].Length)  
                       {  
                           MaxTextIndex = i;  
                       }  
                   }  
         
                   //switch the index.  
         
                   string longestStr = strs[MaxTextIndex];  
                   string FirstStr = strs[0];  
         
                   strs[0] = longestStr;  
                   strs[MaxTextIndex] = FirstStr;  
         
                   RecyclerView mRecyclerView = FindViewById<RecyclerView>(Resource.Id.recyclerView);  
                   LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);  
                   mRecyclerView.SetLayoutManager(linearLayoutManager);  
         
                   MyRecycleViewAdapter recycleViewAdapter =   new MyRecycleViewAdapter(this, strs);  
         
                   mRecyclerView.SetAdapter(recycleViewAdapter);  
    

    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.


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.