Android : Change cart menu badge counter from Recycler Adapter

不羁岁月 提交于 2019-12-21 06:25:25

问题


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.

I am using Recycler view inside fragment. And I am referring this below link for menu item count.

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

My problem is I cannot access menu item from adapter.

Here is my fragment code.

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 is my model class

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

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.


回答1:


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()

In your Fragment.onCreateOptionsMenu you can go ahead and set the item count




回答2:


In your fragment click event:

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

Your activity contains this method:

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



回答3:


You can use Interface to communicate between the Fragment and the Adapter.

for example:

public interface CallbackInterface {
    void onCall();
}

and in your PriceListFragment in onCreateView do that:

CallbackInterface callback = new CallbackInterface{
 @Override
  public void onCall() {
     if(itemCart!=null){
       //code to increment badge
     }
  }
};

and pass callback to the adapter's constructor and then use it there.




回答4:


You can use badge style in TextView now, All you need to do is adjust your textview position to make it look like badge.

        <TextView
                android:id="@+id/fabCounter"
                style="@style/Widget.Design.FloatingActionButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_centerVertical="true"
                android:layout_marginEnd="10dp"
                android:padding="5dp"
                android:text="-9"
                android:textColor="@android:color/black"
                android:textSize="14sp" />



来源:https://stackoverflow.com/questions/43015908/android-change-cart-menu-badge-counter-from-recycler-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!