简体   繁体   English

列表内的网格-适配器布局mvxexception

[英]Grid inside list - adapter layout mvxexception

I try to show a GridView in a ListView. 我尝试在ListView中显示GridView。 I want to show images grouped by some properties. 我想显示按某些属性分组的图像。

My Listview (Info: I removed all layout-options in following snippets (better readable): 我的Listview(信息:我删除了以下代码段中的所有layout-options(更好地可读):

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

Then the list_albumdetail layout file: 然后是list_albumdetail布局文件:

<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. 上面的MvxGridView单独使用时(在没有ListView作为父级的情况下)可以完美地工作MvxGridView只是用来显示标题。

Here is my Adapter to show the Listview: 这是我的适配器以显示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" 在MvxAdapter创建期间,bindingContext为null-仅当在堆栈上放置了特定的绑定上下文时,才应创建适配器。”

I have no idea whats going wrong. 我不知道怎么了。 Without the inner GridView it works perfectly, so the BindingContext can't be null. 没有内部GridView,它会完美地工作,因此BindingContext不能为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: 使用MvvmCross我需要使用BindingContext来做到这一点:

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

This BindingContext is a IMvxAndroidBindingContext and is set by the constructor. 此BindingContext是IMvxAndroidBindingContext ,由构造方法设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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