Android Studio: invalidateOptionsMenu() causes the always visible items to stop working

只谈情不闲聊 提交于 2019-11-28 12:48:05

问题


I am trying to hide/show an always visible item (app:showAsAction="always") of a menu after a Switch is off/on.

The issue is:

if I use invalidateOptionsMenu() in onPrepareOptionsMenu(), my searchView and savebutton menu.findItem(R.id.save_product_option) stop working. They are visible but do not answer to the click.

If I do not use invalidateOptionsMenu(), the items work as expected. But the savebutton only gets hiden (menu.findItem(R.id.save_product_option).setVisible(false)) when I click on the Menu itself (three dots).

I want to hide/show the savebutton (arrow like item on the figure at the bottom) as soon as I set off/on the Switch while keeping their functions.

Only the app:showAsAction="never" items work normally.

That's how I set isSwitchChecked in the onCreate to determine whether the savebutton will be hiden/shown.

    mAddProductField.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                mProductFormField.setVisibility(View.VISIBLE);
                Log.i("debinf product", "is switch checked true "+isChecked);
                isSwitchChecked = true;
            } else {
                mProductFormField.setVisibility(View.GONE);
                Log.i("debinf product", "is switch checked false "+isChecked);
                isSwitchChecked = false;
            }
        }
    });

Below is how I implemented the menu items.

@Override
public boolean onCreateOptionsMenu(final Menu menu) {
    super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.options_product,menu);
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    searchView = (SearchView) menu.findItem(R.id.search_product_option).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setQueryHint("Pesquisar...");

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

        @Override
        public boolean onQueryTextSubmit(String s) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String s) {
            if (s.isEmpty()) {
                //mProductListAdapter
                /*adapter.filter("");
                listView.clearTextFilter();*/
                Log.i("debinf product", "search field is empty");
                menu.findItem(R.id.save_product_option).setVisible(true);

            } else {
                //adapter.filter(newText);
                //menu.findItem(R.id.save_product_option).setVisible(false);
                searchView.requestFocus();
                Log.i("debinf product", "search field is NOT empty = "+s.toString());
            }
            return true;
        }
    });
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    super.onOptionsItemSelected(item);

    if (item.getItemId() == R.id.save_product_option) {
        Toast.makeText(this, "Save Product", Toast.LENGTH_SHORT).show();
        saveProduct();
        return true;
    }
    if (item.getItemId() == R.id.import_product_option) {
        Toast.makeText(this, "Import Product", Toast.LENGTH_SHORT).show();
        return true;
    }
    if (item.getItemId() == R.id.cleanfields_product_option) {
        Toast.makeText(this, "Clean Fields", Toast.LENGTH_SHORT).show();
        return true;
    }

    return false;
}

@Override
public boolean onPrepareOptionsMenu(final Menu menu) {

    if (isSwitchChecked) {
        //invalidateOptionsMenu();//update menu options
        menu.findItem(R.id.save_product_option).setVisible(true);

    } else {
        //invalidateOptionsMenu();//update menu options
        menu.findItem(R.id.save_product_option).setVisible(false);

    }
    return super.onPrepareOptionsMenu(menu);
}

The screenshot below shows how the Activity is initialized. But searchView do not respond to click.

When I set on the Switch, the saveButton reappears, but the searchView and saveButton do not respond to click.

]3

Thanks in advance!

来源:https://stackoverflow.com/questions/54756799/android-studio-invalidateoptionsmenu-causes-the-always-visible-items-to-stop

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