问题
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