Ciąg dalszy uspójniania aplikacji ze standardami, czyli przerzucam przycisk usuwania do toolbara Na początek xml – menu z przyciskiem usuwania:
Przy tworzeniu ustawiam żeby był niewidoczny, bo chcę żeby pojawił się razem z checkboxami po tym jak użytkownik przytrzyma element na liście. ShowAsAction=”ifRoom” sprawi, że jeżeli będzie miejsce to przycisk pozostanie przyciskiem, a nie elementem w wysuwanym menu.
W IngredientActivity muszę teraz dorzucić dwie metody: jedną, która przetworzy (inflate) mi tego xml-a i drugą, która obsłuży kliknięcie – czyli usunie zaznaczone elementy:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
getMenuInflater().inflate(R.menu.menu_with_delete_icon, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
switch (item.getItemId()) { | |
case R.id.delete_menu_button: | |
ArrayList<Long> ingredientIds = new ArrayList<Long>(); | |
for (int i = 0; i < adapter.getCount(); i++) { | |
Ingredient ingredient = adapter.getItem(i); | |
if (ingredient != null && ingredient.isChecked()) { | |
ingredientIds.add(ingredient.getId()); | |
} | |
} | |
toggleCheckboxesAndToolbar(); | |
ingredientContract.delete(ingredientIds.toArray(new Long[ingredientIds.size()]), helper); | |
adapter.updateResults(ingredientContract.getAllIngredientsArray(helper)); | |
return true; | |
default: | |
return super.onOptionsItemSelected(item); | |
} | |
} |
Przy usuwaniu przyda się powrócić do stanu początkowego, czyli schować checkboxy i przycisk usuwania. Od razu można ubrać to w funkcję, bo pokazywanie i chowanie elementów to różnią się niewiele.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void toggleCheckboxesAndToolbar() { | |
int colorId = adapter.showCheckboxes ? R.color.colorPrimary : android.R.color.darker_gray; | |
myToolbar.setBackgroundColor(ContextCompat.getColor(this, colorId)); | |
MenuItem deleteMenuButton = myToolbar.getMenu().findItem(R.id.delete_menu_button); | |
deleteMenuButton.setVisible(!deleteMenuButton.isVisible()); | |
adapter.showCheckboxes = !adapter.showCheckboxes; | |
adapter.notifyDataSetChanged(); | |
} |
Dla dodania akcentu przy okazji zmieniam też kolor toolbara na szary.
Dzięki dodaniu tej funkcji moja akcja podpięta pod przytrzymanie na liście jest prosta:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { | |
@Override | |
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { | |
toggleCheckboxesAndToolbar(); | |
return true; | |
} | |
}); |
A jak to wygląda w praktyce:
I to tyle na dziś