问题
am inflating menu item inside fragments onCreateOptionsMenu()
.
Point 1 :
But here onCreateOptionsMenu
is calling multiple time, am no where calling invalidateOptionMenu()
inside fragment and am not getting why it is like this.
Point 2:
I am updating cart count (badge count) please look my code below,
mCartCountTextView
is member that hold the TextView,that holds count value. Am setting mCartCountTextView
in some where of fragment by calling refreshCartCount()
,but the value of mCartCountTextView
is not updating even though SharedPreference
has updated value.
Code Snippet:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.menu_base, menu);
MenuItem cartItem = menu.findItem(R.id.action_cart);
cartItem.setActionView(R.layout.base_menu_custom_layout);
cartItem.getActionView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mMenuClickListener.onCartClick();
}
});
mCartCountTextView = ((TextView) cartItem.getActionView().findViewById(R.id.cart_count));
refreshCartCount();
}
public void refreshCartCount() {
if (PreferenceManager.getInstance(getActivity()).getCustomerCartCount() == 0)
mCartCountTextView.setVisibility(View.GONE);
else {
mCartCountTextView.setVisibility(View.VISIBLE);
mCartCountTextView.setText(String.valueOf(PreferenceManager.getInstance(getActivity()).getCustomerCartCount()));
}
}
How do I fix it?
Here am calling invalidateOptionsMenu()
inside same fragment.
@Override
public void setCartResponse(CartListDTO response) {
hideProgressBar();
showToast(response.message);
if (response.status == 1) {
mCartBtn.setText(R.string.go_to_cart);
getActivity().invalidateOptionsMenu();
}
}
来源:https://stackoverflow.com/questions/37134559/text-on-an-actionbar-icon-is-not-updating-on-new-count