RecyclerView在长列表中导致“未找到资源”异常

时间:2017-02-14 00:40:35

标签: android xamarin

我有这个OnBindViewHolder:

public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
        {
            PlayerBalanceRecycleView view = holder as PlayerBalanceRecycleView;
            var item = mItems[position];

            view.Date.Text = item.CreationDate.ToString("dd/MM");
            view.Result.Text = item.Amount.ToString("N0", new CultureInfo("en-US"));
            view.Total.Text = item.Balance.ToString("N0", new CultureInfo("en-US"));



            /* Remove SessionDetailed */
            //var oldFrag = mFragmentManager.FindFragmentByTag("session_" + view.SessionDetailed.Id);
            //if (oldFrag != null) mFragmentManager.BeginTransaction().Remove(oldFrag).Commit();

            /* Create new ID and Tag */
            //var newId = Utils.GenerateViewId();
            var tag = "session_" + view.SessionDetailed.Id.ToString();
            //view.SessionDetailed.Id = newId;

            if (item.Type == PlayerBalanceItemTypes.Payment)
            {
                view.Icon.SetImageDrawable(mActivity.Resources.GetDrawable(Resource.Drawable.ic_money_medium_blue));
                view.Result.SetTextColor(mActivity.Resources.GetColor(Resource.Color.blue));
            }
            else if (item.Type == PlayerBalanceItemTypes.Session)
            {
                view.Icon.SetImageDrawable(mActivity.Resources.GetDrawable(Resource.Drawable.ic_chips_medium));
                view.Result.SetTextColor(mActivity.Resources.GetColor(Resource.Color.orange));

                var frag = new BalancePlayerSession(item);

                var trans = mFragmentManager.BeginTransaction();
                trans.Replace(view.SessionDetailed.Id, frag, tag);
                trans.Commit();

            }
            if (item.Type == PlayerBalanceItemTypes.InitialDebt)
                view.Date.Visibility = ViewStates.Invisible;

            view.SessionDetailed.Visibility = ViewStates.Gone;
        }

所有ID都是唯一的,并在OnCreateViewHolder中设置。在较短的列表(不超出屏幕的列表)工作正常。但是,当列表超出屏幕时,它会导致“未找到资源”异常并引用我设置的唯一ID。

似乎没有创建实际的视图(因此您可以“按需”滚动)。

OnCreateViewHolder:

public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
        {
            View row = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.balance_player_row, parent, false);
            LinearLayout mRoot = row.FindViewById<LinearLayout>(Resource.Id.layoutPlayerBalance);
            ImageView mIcon = row.FindViewById<ImageView>(Resource.Id.imageViewPlayerBalanceType);
            TextView mDate = row.FindViewById<TextView>(Resource.Id.textBalancePlayerDate);
            TextView mResult = row.FindViewById<TextView>(Resource.Id.textBalancePlayerResult);
            TextView mTotal = row.FindViewById<TextView>(Resource.Id.textBalancePlayerTotal);
            LinearLayout mSessionDetailed = row.FindViewById<LinearLayout>(Resource.Id.layoutBalancePlayerSessionDetailed);
            mSessionDetailed.Id = Utils.GenerateViewId();

            PlayerBalanceRecycleView view = new PlayerBalanceRecycleView(row)
            {
                View = row,
                Root = mRoot,
                Icon = mIcon,
                Date = mDate,
                Result = mResult,
                Total = mTotal,
                SessionDetailed = mSessionDetailed
            };

            view.SetClickListener(OnClick);

            return view;
        }

视频解释行为:https://sendvid.com/qi1efjwd

如何确保Fragment正确替换View?

0 个答案:

没有答案