Recycleviewer适配器NotifyDataSetChanged不起作用?

时间:2018-02-01 06:39:12

标签: android android-recyclerview xamarin.android android-viewpager pagertabstrip

我使用PagerSlidingTabStrip创建标签页,在每个标签页面中我都有Recyclerview来调整相关标签项。例如,我们有3个选项卡,其名称为丰田,奔驰和福特以及A tab tho显示所有项目,因此在丰田选项卡中我们有车辆等等。 Recyclerview项目有一个添加到购物车并从购物车中删除的按钮,Textview显示订单数量。默认情况下隐藏按钮和Textview,当用户添加项目时购物车,所以删除按钮和Textview visiblility将是真的。问题是,如果用户将一个项目添加到购物车,则“删除”按钮和相关项目的Textview在“全部”选项卡中不可见?但是在“全部”选项卡中,当用户将项目添加到购物车时,它会反映到相关标签中的项我认为问题是Recylerview的适配器,它保存视图而不刷新所有项目的选项卡,也许。我使用NotifyDataSetChanged Recyclerview适配器但不起作用。

button添加到购物车并删除:mDecremtBtn和mIncrementBtn,当点击可见Textview和减少按钮时。

mDcrementBtn.Click += delegate
                    {

                        if (orderList.Count == 0)
                        {
                            quntitySum = 0;
                        }

                        //Find order and decrement
                        var orderItem = orderList.Find(p => p.FoodId == foodId);
                        orderItem.Quantity--;
                        _quantitiyTxt.Text = orderItem.Quantity.ToString();

                        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;

                            //Update bageNotification On bottomNavigation Cart when no items in orderlist
                            _badgeNotification.Text = "";
                            _badgeNotification.Visibility = ViewStates.Gone;
                        }
                        _fabCart.SetImageBitmap(ConvertTextToBitmap.TextAsBitmap((--quntitySum).ToString(), 36,
                           Color.White));

                        //Update bageNotification On bottomNavigation Cart
                        _badgeNotification.Text = quntitySum.ToString();
                        _adpter.NotifyDataSetChanged();

                    };

                    mIncrementBtn.Click += delegate
                    {
                        if (orderList.Count == 0)
                        {
                            quntitySum = 0;
                        }
                        _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),
                                ClickFlag = true
                            };

                            orDetail.Quantity++;
                            orderList.Add(orDetail);
                            _quantitiyTxt.Text = orDetail.Quantity.ToString();
                        }

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

                        //Update bageNotification On bottomNavigation Cart
                        _badgeNotification.Visibility = ViewStates.Visible;
                        _badgeNotification.Text = quntitySum.ToString();

                        _adpter.NotifyDataSetChanged();

                    };

在SimpleStringRecyclerViewAdapter类的OnbindeViewHolder中:RecyclerView.Adapter项目已更新。

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

                    simpleHolder.mFoodNameTxt.Text = _foodItems[position].FoodName;
                    simpleHolder.mPriceTxt.Text = _foodItems[position].Price.ToString();
                    simpleHolder.foodId = _foodItems[position].Id;   

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

                    }
                    else
                    {
                        simpleHolder.mDcrementBtn.Visibility = ViewStates.Invisible;
                        simpleHolder._quantitiyTxt.Visibility = ViewStates.Invisible;
                        simpleHolder._quantitiyTxt.Text = "";
                    }

                }

Imagenary中的Senario: enter image description here

https://forums.xamarin.com/discussion/120389/recycleviewer-adapter-notifydatasetchanged-not-work/p1?new=1

中的Compelete代码

0 个答案:

没有答案