Wrapping Items in a RecyclerView

Nathan Sokalski 4,126 Reputation points
2021-03-17T21:32:13.153+00:00

I have a RecyclerView for which I want to wrap the items. The width of the items is unknown (and may change or not even be the same for all items). Is there a LayoutManager (or other technique) that I can use that will wrap the items when necessary? Measuring the items at runtime to calculate the number of columns for a GridLayoutManager or StaggeredGridLayoutManager could be expensive, not to mention a lot of extra code. Any ideas? Thanks.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,325 questions
0 comments No comments
{count} votes

Accepted answer
  1. JarvanZhang 23,951 Reputation points
    2021-03-18T06:38:35.413+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    To specify a dynamic size for the recyclerView, please set both the android:layout_width of the recyclerView and the android:layout_width of item's layout to wrap_content.

       //main_layout.xaml  
       <LinearLayout ...  
           android:orientation="vertical"  
           android:layout_width="match_parent"  
           android:layout_height="match_parent">  
         
           <androidx.recyclerview.widget.RecyclerView  
               android:minWidth="25px"  
               android:minHeight="25px"  
               android:layout_width="wrap_content"  
               android:layout_height="wrap_content"  
               android:id="@+id/recyclerView1" />  
       </LinearLayout>  
         
       //item_ayout.xaml  
       <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
           android:orientation="vertical"  
           android:layout_width="wrap_content"  
           android:layout_height="wrap_content">  
           <!--the views-->  
       </LinearLayout>  
    

    The related code about populating data for the recyclerView.

       public class TestActivity : Activity  
       {  
           RecyclerView recyclerView;  
           RecyclerView.LayoutManager mLayoutManager;  
           List<model> list;  
           CustomAdapter adapter;  
         
           protected override void OnCreate(Bundle savedInstanceState)  
           {  
               base.OnCreate(savedInstanceState);  
               SetContentView(Resource.Layout.layout2);  
         
               recyclerView = FindViewById<RecyclerView>(Resource.Id.recyclerView1);  
               mLayoutManager = new GridLayoutManager(this, 3);  
               recyclerView.SetLayoutManager(mLayoutManager);  
         
               list = xxx;//add the data  
               adapter = new CustomAdapter(list, this);  
               recyclerView.SetAdapter(adapter);  
           }  
       }  
    

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    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