Я пытаюсь создать меню с фильтрами на карте, идея состоит в том, чтобы выбрать несколько элементов (например, снимок экрана).Но проблема в том, что каждый раз, когда я нажимаю опцию, меню автоматически скрывается.
Поэтому я могу установить только один флажок каждый раз, когда открываю меню.Такое поведение не является лучшим, потому что пользователь должен открыть N раз меню, чтобы выбрать N фильтров.
Любое предложение или совет о том, как решить эту проблему?Спасибо!
![introducir la descripción de la imagen aquí](https://i.stack.imgur.com/ejKAe.png)
И это мой код (сокращен, чтобы быть более читабельным):
map_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_filtro"
app:showAsAction="always" >
<menu>
<group android:checkableBehavior="all">
<item
android:id="@+id/nav_ciencia"
android:icon="@drawable/ciencia"
android:title="@string/filtro_ciencia"
app:showAsAction="never"
/>
<item
android:id="@+id/nav_comercio"
android:icon="@drawable/comercio"
android:title="@string/filtro_comercio"
app:showAsAction="never"
/>
<item
android:id="@+id/nav_cultura"
android:icon="@drawable/cultura"
android:title="@string/filtro_cultura"
app:showAsAction="never"
/>
<item
android:id="@+id/nav_deporte"
android:icon="@drawable/deporte"
android:title="@string/filtro_deportes"
app:showAsAction="never"
/>
</group>
</menu>
</item>
</menu>
Активность
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.map_drawer, menu);
SharedPreferences prefs = getSharedPreferences(Constants.pref_name, Context.MODE_PRIVATE);
MenuItem item_ciencia = menu.findItem(R.id.nav_ciencia);
item_ciencia.setChecked(prefs.getBoolean(Constants.pref_nav_ciencia, false));
MenuItem item_comercio = menu.findItem(R.id.nav_comercio);
item_comercio.setChecked(prefs.getBoolean(Constants.pref_nav_comercio, false));
MenuItem item_cultura = menu.findItem(R.id.nav_cultura);
item_cultura.setChecked(prefs.getBoolean(Constants.pref_nav_cultura, false));
MenuItem item_deporte = menu.findItem(R.id.nav_deporte);
item_deporte.setChecked(prefs.getBoolean(Constants.pref_nav_deporte, false));
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
SharedPreferences prefs = getSharedPreferences(Constants.pref_name, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
if (id == R.id.action_settings) {
return true;
}else if (id == R.id.nav_ciencia) {
item.setChecked(!item.isChecked());
editor.putBoolean(Constants.pref_nav_ciencia, item.isChecked());
editor.commit();
return true;
} else if (id == R.id.nav_comercio) {
item.setChecked(!item.isChecked());
editor.putBoolean(Constants.pref_nav_comercio, item.isChecked());
editor.commit();
return true;
} else if (id == R.id.nav_cultura) {
item.setChecked(!item.isChecked());
editor.putBoolean(Constants.pref_nav_cultura, item.isChecked());
editor.commit();
return true;
} else if (id == R.id.nav_deporte) {
item.setChecked(!item.isChecked());
editor.putBoolean(Constants.pref_nav_deporte, item.isChecked());
editor.commit();
return true;
}
return super.onOptionsItemSelected(item);
}