gpt4 book ai didi

安卓 : Change cart menu badge counter from Recycler Adapter

转载 作者:行者123 更新时间:2023-11-29 19:17:51 25 4
gpt4 key购买 nike

我正在尝试开发电子商务类型的应用程序,用户可以在其中将商品添加到购物车。但我无法从适配器更新购物车徽章计数器。这是我的应用程序的屏幕截图。

enter image description here

我在 fragment 中使用 Recycler View 。我指的是以下链接的菜单项计数。

https://mobikul.com/adding-badge-count-on-menu-items-like-cart-notification-etc/

我的问题是我无法从适配器访问菜单项。

这是我的 fragment 代码。

PriceListFragment.java

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);
}
}

设计是我的模特课

Design.java

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;
}

Single_DesignAdapter.java

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> 中添加每个购物车项目然后我会将此列表保存到共享首选项中。我会管理一切,但我无法理解如何从适配器更新购物车计数器。我无法访问适配器中的菜单。如果有其他方法可以做到这一点,请帮助。

最佳答案

在您的 fragment 点击事件中:

((MainActivity) getActivity()).createCartBadge("value");

您的 Activity 包含此方法:

public void createCartBadge(int i) {
MenuItem cartItem = mToolbarMenu.findItem(R.id.cart);
LayerDrawable localLayerDrawable = (LayerDrawable) cartItem.getIcon();
Drawable cartBadgeDrawable = localLayerDrawable
.findDrawableByLayerId(R.id.ic_badge);
BadgeDrawable badgeDrawable;
if ((cartBadgeDrawable != null)
&& ((cartBadgeDrawable instanceof BadgeDrawable))
&& (i < 10)) {
badgeDrawable = (BadgeDrawable) cartBadgeDrawable;
} else {
badgeDrawable = new BadgeDrawable(MainActivity.this);
}
badgeDrawable.setCount(i);
localLayerDrawable.mutate();
localLayerDrawable.setDrawableByLayerId(R.id.ic_badge, badgeDrawable);
cartItem.setIcon(localLayerDrawable);
}

关于安卓 : Change cart menu badge counter from Recycler Adapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43015908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com