我正在使用 PagerSlidingTabStrip 来创建选项卡,并且在每个选项卡中我必须Recyclerview
填充相关的选项卡项目。例如,我们有 3 个名称为 Toyota 、 Benz 和 Ford 的选项卡和一个显示所有项目的选项卡,因此在 Toyota 选项卡中我们有它的车辆等等。Recyclerview
项目有一个添加到购物车和从购物车中删除的按钮,以及一个Textview
显示订单数量的按钮。Textview
删除按钮默认隐藏,当用户将项目添加到购物车时,删除按钮和Textview
可见性将为 true。问题是,如果用户将商品添加到购物车,则删除按钮和Textview
相关商品在所有选项卡中不可见?!但是在所有选项卡中,当用户将项目添加到购物车时,它会反映到相关选项卡中的项目。我认为问题是适配器Recylerview
,它保持视图而不刷新所有项目的选项卡,也许。我使用NotifyDataSetChanged
适配器Recyclerview
但不起作用。
button
添加到购物车和删除:单击可见Textview
和减量按钮时的mDecremtBtn 和 mIncrementBtn。
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 = "";
}
}
完成https://forums.xamarin.com/discussion/120389/recycleviewer-adapter-notifydatasetchanged-not-work/p1?new=1中的代码