每七个项目点击Recyclerview效果中的项目?

时间:2017-12-26 13:29:49

标签: android xamarin android-recyclerview android-adapter

我有一个Recyclerview,在每个项目中,我放置了2个名为IncementBtn nad DecrementBtn的按钮来增加和减少ListOrders中的项目。还有一个显示项目编号的Textview .Recyclerview在viewpager中,每页寻呼机相关到ViewPager选项卡。当我单击incrementBtn以再次添加数量时,第七个位置也会更改并将数量显示为单击的项目。 当我点击第七项也受影响的项目时,一切都很好。

 public class SimpleStringRecyclerViewAdapter : RecyclerView.Adapter
            {
                private readonly TypedValue mTypedValue = new TypedValue();
                private int mBackground;
                private List<Food> mValues;
                Resources mResource;
                private Dictionary<int, int> mCalculatedSizes;

                public SimpleStringRecyclerViewAdapter(Context context, List<Food> items, Resources res)
                {
                    context.Theme.ResolveAttribute(Resource.Attribute.selectableItemBackground, mTypedValue, true);
                    mBackground = mTypedValue.ResourceId;
                    mValues = items;
                    mResource = res;

                    mCalculatedSizes = new Dictionary<int, int>();
                }

                public override int ItemCount
                {
                    get { return mValues.Count; }
                }

                public override async void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
                {
                    var simpleHolder = holder as SimpleViewHolder;

                    //simpleHolder.mBoundString = mValues[position].;
                    simpleHolder.mFoodNameTxt.Text = mValues[position].Name;
                    simpleHolder.mPriceTxt.Text = mValues[position].Price.ToString();
                    simpleHolder.foodId = mValues[position].FoodId;
                    simpleHolder.position = position;

                }

                public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
                {

                    View view = LayoutInflater.From(parent.Context)
                        .Inflate(Resource.Layout.food_list_item, parent, false);
                    view.SetBackgroundResource(mBackground);

                    return new SimpleViewHolder(view);
                }
            }

            public class SimpleViewHolder : RecyclerView.ViewHolder
            {
                public string mBoundString;
                public  View mView;
                public  ImageView mImageView;
                public  TextView mFoodNameTxt;
                public  TextView mPriceTxt;
                public  Button mIncrementBtn;
                public  Button mDcrementBtn;
                public  TextView _quantitiyTxt;
                public int foodId;
                private static int quntitySum = 0;
                public int position;
                public SimpleViewHolder(View view) : base(view)
                {
                    mView = view;


                    //mImageView = view.FindViewById<ImageView>(Resource.Id.avatar);
                    mFoodNameTxt = view.FindViewById<TextView>(Resource.Id.lbl_food_name);
                    mPriceTxt = view.FindViewById<TextView>(Resource.Id.lbl_price);

                    mIncrementBtn = view.FindViewById<Button>(Resource.Id.increment_btn);
                    _quantitiyTxt = view.FindViewById<TextView>(Resource.Id.quantity_txt);

                    mDcrementBtn = view.FindViewById<Button>(Resource.Id.decrement_btn);

                    mDcrementBtn.Click += delegate
                    {

                        //Find order and decrement
                        var orderItem = orderList.Find(p => p.FoodId == foodId);
                        orderItem.Quantity--;
                        _quantitiyTxt.Text = orderItem.Quantity.ToString();
                        _fabCart.SetImageBitmap(ConvertTextToBitmap.TextAsBitmap((--quntitySum).ToString(), 36, Color.White));

                        if (orderItem.Quantity == 0)
                        {
                            mDcrementBtn.Visibility = ViewStates.Invisible;
                            _quantitiyTxt.Visibility = ViewStates.Invisible;
                            _quantitiyTxt.Text = "";
                            orderList.Remove(orderItem);
                        }
                        if (orderList.Count == 0)
                        {
                            _fabCart.Visibility = ViewStates.Gone;
                        }

                    };

                    mIncrementBtn.Click += delegate
                    {

                        _fabCart.Show();
                        mDcrementBtn.Visibility = ViewStates.Visible;
                        _quantitiyTxt.Visibility = ViewStates.Visible;
                        if (orderList.Any(p=>p.FoodId==foodId))
                        {
                            //Find order and increment
                            var orderItem = orderList.Find(p => p.FoodId == foodId);
                            orderItem.Quantity++;
                            _quantitiyTxt.Text = orderItem.Quantity.ToString();

                        }
                        else
                        {

                            orDetail = new Order
                            {
                                FoodId = foodId,
                                FoodName = mFoodNameTxt.Text,
                                Price = Convert.ToInt32(mPriceTxt.Text)
                            };
                            orDetail.Quantity++;
                            orderList.Add(orDetail);
                            _quantitiyTxt.Text = orDetail.Quantity.ToString();

                        }

                        _fabCart.SetImageBitmap(ConvertTextToBitmap.TextAsBitmap((++quntitySum).ToString(), 36, Color.White));

                    };


                }


                public override string ToString()
                {
                    return base.ToString() + " '" + mFoodNameTxt.Text;
                }
            }

我错了什么?     请帮助解决这个问题。

我有一张有趣的照片来展示这个问题。 enter image description here

2 个答案:

答案 0 :(得分:2)

在设置recyclerView时尝试为recyclerView设置ItemCachedSize。

YourRecyclverView.setAdapter(matchItemAdapter);
YourRecyclverView.setNestedScrollingEnabled(false);
YourRecyclverView.setLayoutManager(layoutManager);
YourRecyclverView.setItemViewCacheSize(100);

我收到了这个错误,并通过将项目缓存视图设置为100来解决。

答案 1 :(得分:1)

正如@ jon-douglas所说,我将此代码添加到OnBindViewHolder来修复错误。

 simpleHolder.mDcrementBtn.Visibility = ViewStates.Invisible;
 simpleHolder._quantitiyTxt.Visibility = ViewStates.Invisible;
 simpleHolder._quantitiyTxt.Text = "";

 var item = orderList.Find(p => p.FoodId == mValues[position].FoodId);
 if (item != null && item.ClickFlag)
 {
   simpleHolder.mDcrementBtn.Visibility = ViewStates.Visible;
   simpleHolder._quantitiyTxt.Visibility = ViewStates.Visible;
   simpleHolder._quantitiyTxt.Text = item.Quantity.ToString();
 }