android – 增量操作栏使用片段查看按钮单击时的Badger

我正在进行电子商务任务,我想在按钮上添加项目按钮点击添加到购物车,当我点击按钮项目需要添加到购物车和视图獾需要增加.

所以我想在片段中执行此操作,如果你不理解我的问题,那么看看快照,你会明白我想问的是什么?

如果购物车中没有物品 –
android  – 增量操作栏使用片段查看按钮单击时的Badger

当我点击按钮购物车项目添加到购物车,它显示如下..
android  – 增量操作栏使用片段查看按钮单击时的Badger

那么如何使用Fragments执行此操作并增加视图badger.Thanks

解决方法:

请尝试使用以下解决方案,如果您仍有问题,请告诉我,我会向您发送示例.

private int count = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button increaseButton = (Button) findViewById(R.id.increaseButton);
    increaseButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            doIncrease();
        }
    });
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);

    MenuItem menuItem = menu.findItem(R.id.testAction);
    menuItem.setIcon(buildCounterDrawable(count, R.drawable.ic_menu_gallery));

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

private Drawable buildCounterDrawable(int count, int backgroundImageId) {
    LayoutInflater inflater = LayoutInflater.from(this);
    View view = inflater.inflate(R.layout.counter_menuitem_layout, null);
    view.setBackgroundResource(backgroundImageId);

    if (count == 0) {
        View counterTextPanel = view.findViewById(R.id.counterValuePanel);
        counterTextPanel.setVisibility(View.GONE);
    } else {
        TextView textView = (TextView) view.findViewById(R.id.count);
        textView.setText("" + count);
    }

    view.measure(
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

    view.setDrawingCacheEnabled(true);
    view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
    Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
    view.setDrawingCacheEnabled(false);

    return new BitmapDrawable(getResources(), bitmap);
}

private void doIncrease() {
    count++;
    invalidateOptionsMenu();
}
上一篇:php – 在Woocommerce中添加到购物车之前清空购物车


下一篇:小程序实现购物车