Menu toggle

This commit is contained in:
Martin Fietz 2016-02-11 10:23:30 +01:00
parent 7436e9c030
commit 5690c99e48
2 changed files with 45 additions and 7 deletions

View File

@ -33,6 +33,9 @@ public class OpmlFeedChooserActivity extends ActionBarActivity {
private ListView feedlist; private ListView feedlist;
private ArrayAdapter<String> listAdapter; private ArrayAdapter<String> listAdapter;
private MenuItem selectAll;
private MenuItem deselectAll;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
setTheme(UserPreferences.getTheme()); setTheme(UserPreferences.getTheme());
@ -55,6 +58,20 @@ public class OpmlFeedChooserActivity extends ActionBarActivity {
public void onClick(View v) { public void onClick(View v) {
setResult(RESULT_CANCELED); setResult(RESULT_CANCELED);
finish(); finish();
feedlist.setOnItemClickListener((parent, view, position, id) -> {
SparseBooleanArray checked = feedlist.getCheckedItemPositions();
int checkedCount = 0;
for (int i = 0; i < checked.size(); i++) {
if (checked.valueAt(i)) {
checkedCount++;
}
}
if(checkedCount == listAdapter.getCount()) {
selectAll.setVisible(false);
deselectAll.setVisible(true);
} else {
deselectAll.setVisible(false);
selectAll.setVisible(true);
} }
}); });
@ -101,13 +118,11 @@ public class OpmlFeedChooserActivity extends ActionBarActivity {
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu); super.onCreateOptionsMenu(menu);
MenuItemCompat.setShowAsAction(menu.add(Menu.NONE, R.id.select_all_item, Menu.NONE, MenuInflater inflater = getMenuInflater();
R.string.select_all_label), inflater.inflate(R.menu.opml_selection_options, menu);
MenuItemCompat.SHOW_AS_ACTION_IF_ROOM); selectAll = menu.findItem(R.id.select_all_item);
deselectAll = menu.findItem(R.id.deselect_all_item);
MenuItemCompat.setShowAsAction(menu.add(Menu.NONE, R.id.deselect_all_item, Menu.NONE, deselectAll.setVisible(false);
R.string.deselect_all_label),
MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
return true; return true;
} }
@ -115,10 +130,14 @@ public class OpmlFeedChooserActivity extends ActionBarActivity {
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.select_all_item: case R.id.select_all_item:
selectAll.setVisible(false);
selectAllItems(true); selectAllItems(true);
deselectAll.setVisible(true);
return true; return true;
case R.id.deselect_all_item: case R.id.deselect_all_item:
deselectAll.setVisible(false);
selectAllItems(false); selectAllItems(false);
selectAll.setVisible(true);
return true; return true;
default: default:
return false; return false;

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto">
<item
android:id="@id/select_all_item"
android:icon="?attr/ic_check_box_outline"
android:title="@string/select_all_label"
custom:showAsAction="ifRoom">
</item>
<item
android:id="@id/deselect_all_item"
android:icon="?attr/ic_check_box"
android:title="@string/deselect_all_label"
custom:showAsAction="ifRoom">
</item>
</menu>