I'm trying to create a menu with filters on a map, the idea is to select multiple elements (like the screenshoot). But the problem is that every time I press an option, the menu is automatically hidden.
So I can only tap one checkbox everytime I open the menu. This behaviour is not the best because user has to open N times the menu to select N filters.
Any suggestion or advice about how to solve this? Thanks!
And this is my code (reduced to be more readable):
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>
Activity
@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);
}
Aucun commentaire:
Enregistrer un commentaire