njsokalski avatar image
0 Votes"
njsokalski asked RobCaplan edited

Wrapping Items in a RecyclerView

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered njsokalski commented


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.

<LinearLayout ...

        android:id="@+id/recyclerView1" />

<LinearLayout xmlns:android=""
    <!--the views-->

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)

        recyclerView = FindViewById<RecyclerView>(Resource.Id.recyclerView1);
        mLayoutManager = new GridLayoutManager(this, 3);

        list = xxx;//add the data
        adapter = new CustomAdapter(list, this);

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.

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

In your code, you use a GridLayoutManager, in which you specify a constant spanCount of 3. I do not want a constant here, because the number of columns will be different depending on how many items I have as well as their widths.

0 Votes 0 ·

What you need may be a flexible layout, google api proivdes the FlexboxLayout which could help you. Try install the XamarinLibrary.Xamarin.AndroidX.FlexBox packaget use the function.

using Com.Google.Android.Flexbox;

mLayoutManager = new FlexboxLayoutManager(this, 0, 1);

Check the doc:

0 Votes 0 ·

That is exactly what I want! Thank you so much!

0 Votes 0 ·