Search code examples
androidgridviewmvvmcross

MVVMCross - MvxGridView is not wrapping the content height


I'm developing Android app using Xamarin and MVVM Cross. And I'm trying to load two labels list to MvxGridView.

Control_List.axml

<?xml version="1.0" encoding="utf-8"?>
<MvxLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<MvxGridView
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:numColumns="auto_fit"
    android:verticalSpacing="5dp"
    android:horizontalSpacing="5dp"
    android:stretchMode="columnWidth"
    android:choiceMode="none"
    local:MvxBind="ItemsSource List"
    local:MvxItemTemplate="@layout/list_item" />
</MvxLinearLayout>

And List_item.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:gravity="center_vertical">
<TextView
    android:background="@drawable/roundedshape_calendar_blurb"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:textSize="16dp"
    android:textColor="@color/white"
    android:singleLine="false"
    local:MvxBind="Text Format('{0}: {1}', Key, Value)" />
<View
    android:layout_width="1dp"
    android:layout_height="36dp"
    local:MvxBind="BackgroundColor DarkerThemeColor" />
</LinearLayout>

And the view looks like this:

MvxGriView layout

And my question is : why it displays only one row?

Many thanks!


Solution

  • I Struggled with this for a bit.

    I found an easy solution by changing the grid height dynamically:

            var myGrid = _rootView.FindViewById<MvxGridView>(Resource.Id.grid);
            ViewGroup.LayoutParams layoutParams = myGrid .LayoutParameters;
            layoutParams.Height = (ViewModel.Items.Count / myGrid .NumColumns) * _gridRowHeight;
            myGrid.LayoutParameters = layoutParams;