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