Mvvmcross Android-Recyclerview自定义适配器在滚动时引发错误

时间:2018-06-25 09:32:11

标签: c# xamarin.android mvvmcross

我正在为xamarin android应用程序使用mvvmcross。在此应用中,我正在使用MvxRecyclerView并为此创建了自定义适配器。

在“活动”中,我将此适配器绑定到回收站视图,如下所示-

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);
    recyclerview = (MvxRecyclerView)FindViewById(Resource.Id.mvxRVList);
    GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
    recyclerview.SetLayoutManager(isLandscape ? (LinearLayoutManager)linearLayoutManager : gridLayoutManager);
    globalRetailItemAdapter = new CustomAdapter(isLandscape, this, (IMvxAndroidBindingContext)BindingContext);
    recyclerview.NestedScrollingEnabled = true;
}

适配器:

public class CustomAdapter : MvxRecyclerAdapter //,ISectionIndexer
{
    Context context;
    bool isLandscape;
    public CustomAdapter(bool isLandscape, Context context, IMvxAndroidBindingContext bindingContext)
        : base(bindingContext)
    {
        this.context = context;
        this.isLandscape = isLandscape;
    }

    public override ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
    {
        View itemView;

        if (viewType == 0)
        {
            itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.sectionHeaderTemplate,parent,false);
            return new ItemViewHolderForHeader(itemView, this.BindingContext);
        }
        else
        {
            itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.productlistviewitem, parent, false);
            return new ItemViewHolder(itemView, this.BindingContext);
        }
    }
    public override int GetItemViewType(int position)
    {
        var item = GetItem(position);
        if (item is RetailItemSectionHeader)
            return 0;
        return 1;
    }
    public override void OnBindViewHolder(ViewHolder holder, int position)
    {
        try
        {
            int type = GetItemViewType(position);
            var item = GetItem(position);
            if (type == 0)
            {
                RetailItemSectionHeader header = (RetailItemSectionHeader)item;//items[position]
                ItemViewHolderForHeader viewHolder = (ItemViewHolderForHeader)holder;
                viewHolder.tvItemPrice.Text = header.headerTitle;                   
            }
            else
            {
                RetailItem retailItem = (RetailItem)item;//items[position]
                ItemViewHolder viewHolder = (ItemViewHolder)holder;                    
                viewHolder.tvProductItemName.Text = retailItem.Title;
                viewHolder.tvItemQuantity.Text = Convert.ToString(retailItem.quantity);
                viewHolder.tvItemPrice.Text = Convert.ToString(retailItem.Price);                    
            }
        }
        catch(Exception e)
        {

        }
    }
}

观看者

public class ItemViewHolder : MvxRecyclerViewHolder
{
    public TextView tvProductItemName, tvItemPrice, tvItemQuantity;
    public LinearLayout llTemplate;
    public RelativeLayout rlTemplate;

    public ItemViewHolder(View itemView, IMvxAndroidBindingContext context)
        : base(itemView, context)
    {
        tvProductItemName = (TextView)itemView.FindViewById(Resource.Id.tvProductItemName);
        tvItemPrice = (TextView)itemView.FindViewById(Resource.Id.tvItemPrice);
        tvItemQuantity = (TextView)itemView.FindViewById(Resource.Id.tvItemQuantity);
        llTemplate = (LinearLayout)itemView.FindViewById(Resource.Id.lvItemTemplate);
        rlTemplate = (RelativeLayout)itemView.FindViewById(Resource.Id.rlItemTemplate);
    }
}
public class ItemViewHolderForHeader : MvxRecyclerViewHolder
{
    public TextView tvItemPrice;

    public ItemViewHolderForHeader(View itemView, IMvxAndroidBindingContext context)
        : base(itemView, context)
    {
        tvItemPrice = (TextView)itemView.FindViewById(Resource.Id.tvItemPrice);
    }
}

我的视图正确呈现,但是尝试滚动列表时出现错误。以下是我得到的错误描述-

  

未处理的异常:

     

Java.Lang.IndexOutOfBoundsException:检测到不一致。     无效的项目位置14(偏移量:14)。状态:166 mvvmcross.droid.support.v7.recyclerview.MvxRecyclerView {2799f2df VFED.V ............... 0,0-434,530#7f0900f9 app:id / mvxRVList},适配器:md569c8f30ad557d7c02673e279425d47a7.CustomAdapter@29fb04f0,布局:android.support.v7.widget.GridLayoutManager@111878a2,上下文:md5a2c5b2e78f64efe650e82df8fe55ec57.HomeView@2c470d>

布局文件中的Recyclerview-

<MvvmCross.Droid.Support.V7.RecyclerView.MvxRecyclerView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingLeft="10dp"
      android:background="@android:color/transparent"
      android:paddingTop="15dp"
      android:scrollbars="vertical"
      android:scrollbarSize="20dp"
      android:scrollbarStyle="outsideInset"
      local:MvxBind="ItemsSource ItemsWithHeader;ItemClick ItemSelectedCommand"                    
      local:MvxTemplateSelector="RMH.POS.Mobile.Android.ProductListTemplateSelector,RMH.POS.Mobile.Android"
      android:id="@+id/mvxRV" />

0 个答案:

没有答案