简体   繁体   中英

Grid inside list - adapter layout mvxexception

I try to show a GridView in a ListView. I want to show images grouped by some properties.

My Listview (Info: I removed all layout-options in following snippets (better readable):

<Mvx.MvxListView
    android:id="@+id/albumdetail_imagelist"
    local:MvxBind="ItemsSource Data.GroupedPictures;"
    local:MvxItemTemplate="@layout/list_albumdetail" />

Then the list_albumdetail layout file:

<LinearLayout>
    <TextView android:id="@+id/albumdetailitem_header" />
    <Mvx.MvxGridView
        android:id="@+id/albumdetailitem_imagegrid"
        android:numColumns="3"
        android:stretchMode="columnWidth"
        local:MvxItemTemplate="@layout/list_phonepicture" />
</LinearLayout>

This MvxGridView above works perfectly when it stands alone (without a ListView as parent. The ListView is just to show a header.

Here is my Adapter to show the Listview:

public class AlbumDetailAdapter : BasePictureSectionAdapter
{
    private readonly Activity _context;

    public AlbumDetailAdapter(Activity context, IMvxAndroidBindingContext bindingContext, bool hideheader = false)
        : base(context, bindingContext, hideheader)
    {
        _context = context;
    }

    protected override View GetView(int position, View convertView, ViewGroup parent, int templateId)
    {
        var keyitem = GetRawItem(position) as KeyedList<string, PictureDetailDataModel>;
        if (keyitem == null) return base.GetView(position, convertView, parent, templateId);

        AlbumDetailViewHoler holder = null;

        if (convertView == null)
        {
            // HERE CRASHS THE APP
            convertView = _context.LayoutInflater.Inflate(templateId, parent, false);
        }
        else
        {
            holder = (AlbumDetailViewHoler)convertView.Tag;
        }

        if (holder == null)
        {
            holder = new AlbumDetailViewHoler
            {
                Header = convertView.FindViewById<TextView>(Resource.Id.albumdetailitem_header),
                GridView = convertView.FindViewById<SpecialGridView>(Resource.Id.albumdetailitem_imagegrid)
            };

            holder.GridView.FastScrollEnabled = false;
            holder.GridView.Adapter = new PhonePictureAdapter(_context, BindingContext, 120);

            convertView.Tag = holder;
        }

        // Set header text
        holder.Header.Text = keyitem.Key;

        // Set itemsource
        holder.GridView.ItemsSource = keyitem.Values;

        return convertView;
    }


    private class AlbumDetailViewHoler : BaseSectionViewHolder
    {
        public SpecialGridView GridView { get; set; }
    }
}

The app crashs with the following exception:

bindingContext is null during MvxAdapter creation - Adapter's should only be created when a specific binding context has been placed on the stack"

I have no idea whats going wrong. Without the inner GridView it works perfectly, so the BindingContext can't be null. Is there a better way to achive this? (without an external library)? Or whats going wrong? Thanks

Just for others with same problems, I answer my own question.

The problem was, that I tried to infalte the layout from the normal context. With MvvmCross I need to do that with the BindingContext:

convertView = BindingContext.BindingInflate(templateId, parent, false);

This BindingContext is a IMvxAndroidBindingContext and is set by the constructor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM