question

njsokalski avatar image
0 Votes"
njsokalski asked ·

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.

dotnet-android
10 |1000 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 ·

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.


· 3 ·
10 |1000 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);
recyclerView.SetLayoutManager(mLayoutManager);


Check the doc: https://android-developers.googleblog.com/2017/02/build-flexible-layouts-with.html

0 Votes 0 ·
njsokalski avatar image njsokalski JarvanZhang-MSFT ·

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

0 Votes 0 ·