单击按钮时隐藏/显示选项菜单

时间:2017-04-11 12:45:52

标签: android android-fragments android-optionsmenu

我在片段中使用了一些按钮。当我检查这些按钮时,应显示选项菜单。当我取消选中它时,它应该隐藏选项菜单。我应该怎么做

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    mLocation = getArguments().getString(Beco.EXTRA_LOCATION);
    listMalls = temporaryModelCache.getDealData().getFacets().getArea();
    listCategories = temporaryModelCache.getDealData().getFacets().getCategories();
    listGender = temporaryModelCache.getDealData().getFacets().getAgeGroup();
    try {
        MainActivity activity = (MainActivity) getActivity();
        if (activity != null) activity.hideBottomBar();
    } catch (Exception ignored) {

    }
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    menu.clear();
    inflater.inflate(R.menu.forgot_password, menu);
    mResetButton = menu.findItem(R.id.action_reset);
    super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int itemId = item.getItemId();
    switch (itemId) {
        case  R.id.action_reset:
            Log.d(TAG, "onClick");
            resetFilter();
            return true;
    }

    return super.onOptionsItemSelected(item);
}

这是我需要隐藏/显示选项菜单的事件

private void checkSelected() {
    if (!mapMall.isEmpty() || !mapGender.isEmpty() || !mapCategory.isEmpty()) {
        footerTab.setVisibility(View.VISIBLE);

    } else {
        footerTab.setVisibility(View.GONE);
    }
}

if (!mapMall.isEmpty() || !mapGender.isEmpty() || !mapCategory.isEmpty())我需要显示操作菜单时,在else部分我需要隐藏选项菜单!我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:2)

为hide和show选项菜单调用invalidateOptionsMenu()

 Boolean Isreset= false;

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    menu.clear();
    inflater.inflate(R.menu.forgot_password, menu);
    mResetButton = menu.findItem(R.id.action_reset);
    if(!Isreset)
    {
        mResetButton.setVisibility(true);
    }else{
        mResetButton.setVisibility(false);
    }
    super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int itemId = item.getItemId();
    switch (itemId) {
        case  R.id.action_reset:
            Log.d(TAG, "onClick");
            resetFilter();
            Isreset= true;
            invalidateOptionsMenu();

            return true;
    }

    return super.onOptionsItemSelected(item);
}

答案 1 :(得分:0)

您可以保留Menu对象的实例,然后使用它来使选项菜单无效。

private Menu menu;


    @Override 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true); 
        mLocation = getArguments().getString(Beco.EXTRA_LOCATION); 
        listMalls = temporaryModelCache.getDealData().getFacets().getArea(); 
        listCategories = temporaryModelCache.getDealData().getFacets().getCategories(); 
        listGender = temporaryModelCache.getDealData().getFacets().getAgeGroup(); 
        try { 
            MainActivity activity = (MainActivity) getActivity();
            if (activity != null) activity.hideBottomBar();
        } catch (Exception ignored) {

        } 
    } 

    @Override 
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        menu.clear();
        inflater.inflate(R.menu.forgot_password, menu);
        mResetButton = menu.findItem(R.id.action_reset);
        this.menu = menu;
        super.onCreateOptionsMenu(menu, inflater);
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        switch (itemId) {
            case  R.id.action_reset:
                Log.d(TAG, "onClick");
                resetFilter(); 
                return true; 
        } 

        return super.onOptionsItemSelected(item);
    } 

使用menu对象,然后切换选项菜单。

private void checkSelected() { 
    if (!mapMall.isEmpty() || !mapGender.isEmpty() || !mapCategory.isEmpty()) { 
        footerTab.setVisibility(View.VISIBLE);
        menu.findItem(R.id.action_reset).setVisibility(View.VISIBLE);

    } else { 
        footerTab.setVisibility(View.GONE);
        menu.findItem(R.id.action_reset).setVisibility(View.GONE);
    } 
}