Text on an ActionBar Icon is not updating on new count

£可爱£侵袭症+ 提交于 2020-01-05 03:48:09

问题


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

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