问题描述
我正在尝试开发电子商务类型的应用程序,用户可以在其中添加商品到购物车.但是我无法从适配器更新购物车徽章计数器.这是我的应用程序的屏幕截图.
I am trying to develop e-commerce type of app in which user can add item to cart. But I am unable to update the cart badge counter from adapter.Here is screenshot of my app.
我正在片段内使用Recycler视图.我指的是下面的链接,用于菜单项计数.
I am using Recycler view inside fragment. And I am referring this below link for menu item count.
https://mobikul. com/在菜单项上添加徽章计数类似购物车通知-etc/
我的问题是我无法从适配器访问菜单项.
My problem is I cannot access menu item from adapter.
这是我的片段代码.
public class PriceListFragment extends Fragment {
public ArrayList<Design> designList;
private Single_DesignAdapter adapter;
private RecyclerView recyclerView;
public PriceListFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
setHasOptionsMenu(true);
View v = inflater.inflate(R.layout.fragment_price_list, container, false);
recyclerView = (RecyclerView)v.findViewById(R.id.pl_recycler_view);
designList = new ArrayList<Design>();
designList = list();
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setHasFixedSize(true);
adapter = new Single_DesignAdapter(getContext(), designList);
recyclerView.setAdapter(adapter);
return v;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.cart_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
MenuItem itemCart = menu.findItem(R.id.menu_cart);
LayerDrawable icon = (LayerDrawable) itemCart.getIcon();
setBadgeCount(getContext(), icon, 0);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_cart:
Toast.makeText(getActivity(), "cart", Toast.LENGTH_SHORT). show();
return false;
default:
break;
}
return false;
}
public ArrayList<Design> list() {
ArrayList<Design> arrayList = new ArrayList<Design>();
Design design = new Design();
design.name = "Black";
design.image = "http://website/Demo/images/1.jpg";
design.designName = "11001";
design.qualityName = "Cotton";
design.amount = "1000";
design.discPercent = "5";
Design design2 = new Design();
design2.name = "Green";
design2.image = "http://website/Demo/images/2.jpg";
design2.designName = "11001";
design2.qualityName = "Cotton";
design2.amount = "900";
design2.discPercent = "9";
arrayList.add(design);
arrayList.add(design2);
return arrayList;
}
public static void setBadgeCount(Context context, LayerDrawable icon, int count) {
BadgeDrawable badge;
// Reuse drawable if possible
Drawable reuse = icon.findDrawableByLayerId(R.id.ic_badge);
if (reuse != null && reuse instanceof BadgeDrawable) {
badge = (BadgeDrawable) reuse;
} else {
badge = new BadgeDrawable(context);
}
badge.setCount(count);
icon.mutate();
icon.setDrawableByLayerId(R.id.ic_badge, badge);
}
}
设计是我的模型课
public class Design implements Serializable{
public String id;
public String qualityId;
public String qualityName;
public String designId;
public String designName;
public String image;
public String name; //use as shade name
public String discPercent;
public String amount;
public String pcs;
public String qty;
}
public class Single_DesignAdapter extends RecyclerView.Adapter<Single_DesignAdapter.ViewHolder> {
public ArrayList<Design> designList;
private Context context;
private LayoutInflater layoutInflater;
public Single_DesignAdapter(Context context, ArrayList<Design> designList) {
this.designList = designList;
this.context = context;
this.layoutInflater = LayoutInflater.from(context);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = layoutInflater.inflate(R.layout.single_design, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
Design design = designList.get(position);
holder.tvQuality.setText(design.qualityName);
holder.tvDesign.setText(design.designName);
holder.tvShade.setText(design.name);
if (!design.discPercent.equals("0")) {
holder.tvDisPer.setText("-"+design.discPercent+"%");
holder.tvAmount.setPaintFlags(holder.tvAmount.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
int a = Integer.parseInt(design.amount);
float b = Float.parseFloat(design.discPercent) / 100;
holder.tvAmount.setText("₹ " + design.amount);
int c = (int) (a * b);
int d = a-c;
holder.tvDiscAmt.setText("₹ " +d);
Picasso.with(context).load(design.image).into(holder.ivDesign);
} else {
holder.tvAmount.setText("₹ " + design.amount);
Picasso.with(context).load(design.image).into(holder.ivDesign);
holder.tvDisPer.setVisibility(View.INVISIBLE);
holder.tvDiscAmt.setVisibility(View.INVISIBLE);
}
holder.btAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.llAdd.setVisibility(View.GONE);
holder.llPlusMinus.setVisibility(View.VISIBLE);
}
});
holder.btPlus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (Integer.parseInt(holder.btPcsCount.getText().toString()) >= 1) {
int a = Integer.parseInt(holder.btPcsCount.getText().toString());
int x = a++;
holder.btPcsCount.setText(Integer.toString(a));
}
}
});
holder.btMinus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (Integer.parseInt(holder.btPcsCount.getText().toString()) <= 1) {
holder.llAdd.setVisibility(View.VISIBLE);
holder.llPlusMinus.setVisibility(View.GONE);
} else {
int a = Integer.parseInt(holder.btPcsCount.getText().toString());
a--;
holder.btPcsCount.setText(Integer.toString(a));
}
}
});
}
@Override
public int getItemCount() {
return designList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public LinearLayout llPer, llAdd, llPlusMinus;
public Button btPlus, btMinus, btPcsCount, btAdd;
public ImageView ivDesign;
public TextView tvDesign, tvQuality, tvShade, tvAmount, tvDiscAmt, tvDisPer;
public ViewHolder(View itemView) {
super(itemView);
btPlus = (Button) itemView.findViewById(R.id.btPlus);
//other find view by ids
}
}
}
现在,当用户将商品添加到购物车时,它应该增加计数器.我将在ArrayList<Design>
中添加所有购物车项目,然后将其保存到共享首选项中.我将管理所有事务,但我无法理解如何从适配器更新购物车的计数器.我无法访问适配器中的菜单.如果还有其他方法,请帮助.
Now when user add item to cart it should increment counter. I will add every cart items in ArrayList<Design>
and then I will save this list into shared preference. I will manage everything, but I am unable understand that How can I update counter of cart from adapter. I cannot access menu in adapter. If there is other way to do this, Please help.
推荐答案
由于您的Adapter
中已经有一个Context
.您可以将其强制转换为Activity
.这样您就可以访问菜单.
Since you already have a Context
in your Adapter
. You can cast that to Activity
. This way you have access to the menu.
//when item is added or removed
((Activity)this.context).invalidateOptionsMenu()
在您的Fragment.onCreateOptionsMenu
中,您可以继续设置项目计数
In your Fragment.onCreateOptionsMenu
you can go ahead and set the item count
这篇关于Android:从Recycler Adapter更改购物车菜单标志计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!