invalidateOptionMenu() disables the clicking functionality of toolbar menu

不羁岁月 提交于 2019-12-12 03:39:15

问题


In my eCommerce app, I am using cart basket with count textview. Here for updates the cart count textview I used invalidateOptionMenu(); after that the clicking of cart navigates next fragment is not working. Even I tried in many ways,but did not worked.Please help me

This is my code:

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


        badgeLayout = (RelativeLayout) menu.findItem(R.id.badge).getActionView();           /*-------Cart Basket with Counter---------*/
        basketCount = (TextView) badgeLayout.findViewById(R.id.counter);
        CommonUtil.dbUtil.open();
        try {
            MainActivity.basketCount.setText(String.valueOf(CommonUtil.dbUtil.getCartItem().getCount()));
        } catch (Exception e) {
            MainActivity.basketCount.setText(String.valueOf("0"));
        }

        badgeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Toast.makeText(context, "Cart Clicked", Toast.LENGTH_SHORT).show();
                /*
                * menuItemClicked represents ToolBar OverFlow (...) menu is clicked.
                * */
                Config.menuItemClicked = true;
                SharedPreferences.Editor editor = CommonUtil.pref.edit();
                editor.putBoolean("cart_Clicked", Config.menuItemClicked);
                editor.apply();

                Intent next = new Intent(context, ProductActivity.class);
                startActivity(next);

            }
        });

        invalidateOptionsMenu();
        return super.onCreateOptionsMenu(menu);
    }

回答1:


instead of calling invalidateOptionMenu() inside your onCreateOptionsMenu() you should call it when you are updating the count value that you set in basketCount TextView.

Calling invalidateOptionsMenu() is of no avail there, so when you add item to your cart at that you should call invalidateOptionsMenu().



来源:https://stackoverflow.com/questions/37541136/invalidateoptionmenu-disables-the-clicking-functionality-of-toolbar-menu

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