diff options
author | Matthew Wong <wongma@protonmail.ch> | 2015-07-01 19:31:11 -0400 |
---|---|---|
committer | Matthew Wong <wongma@protonmail.ch> | 2015-07-01 19:31:11 -0400 |
commit | 8c885882dc0642f2dbf313980441911edd9f9cc7 (patch) | |
tree | 3163df426ac347377e2b45e07e9916d0c2d1330a | |
parent | 6d0abdcad2288ff751c6b5ad195a314c812491c0 (diff) |
Make selectedItems Set instead of SparseBooleanArray
-rw-r--r-- | app/src/main/java/com/zeapo/pwdstore/utils/PasswordRecyclerAdapter.java | 32 | ||||
-rw-r--r-- | app/src/main/res/drawable/selected_rectangle.xml | 2 |
2 files changed, 14 insertions, 20 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRecyclerAdapter.java b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRecyclerAdapter.java index 5ad1b890..857c8276 100644 --- a/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRecyclerAdapter.java +++ b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRecyclerAdapter.java @@ -1,12 +1,9 @@ package com.zeapo.pwdstore.utils; import android.graphics.Color; -import android.support.v7.app.AppCompatActivity; import android.support.v7.view.ActionMode; -import android.support.v7.widget.PopupMenu; import android.support.v7.widget.RecyclerView; import android.util.Log; -import android.util.SparseBooleanArray; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; @@ -19,12 +16,14 @@ import com.zeapo.pwdstore.PasswordStore; import com.zeapo.pwdstore.R; import java.util.ArrayList; +import java.util.Set; +import java.util.TreeSet; public class PasswordRecyclerAdapter extends RecyclerView.Adapter<PasswordRecyclerAdapter.ViewHolder> { private final PasswordStore activity; private final ArrayList<PasswordItem> values; private final PasswordFragment.OnFragmentInteractionListener listener; - private final SparseBooleanArray selectedItems; + private final Set<Integer> selectedItems; private ActionMode mActionMode; // Provide a reference to the views for each data item @@ -50,7 +49,7 @@ public class PasswordRecyclerAdapter extends RecyclerView.Adapter<PasswordRecycl this.values = values; this.activity = activity; this.listener = listener; - selectedItems = new SparseBooleanArray(); + selectedItems = new TreeSet<>(); } // Create new views (invoked by the layout manager) @@ -98,7 +97,7 @@ public class PasswordRecyclerAdapter extends RecyclerView.Adapter<PasswordRecycl public void onClick(View v) { if (mActionMode != null) { toggleSelection(holder.position); - if (selectedItems.size() == 0) { + if (selectedItems.isEmpty()) { mActionMode.finish(); } } else { @@ -114,13 +113,13 @@ public class PasswordRecyclerAdapter extends RecyclerView.Adapter<PasswordRecycl return false; } toggleSelection(holder.position); - // Start the CAB using the ActionMode.Callback defined above + // Start the CAB using the ActionMode.Callback mActionMode = activity.startSupportActionMode(mActionModeCallback); return true; } }); - holder.view.setSelected(selectedItems.get(position)); + holder.view.setSelected(selectedItems.contains(position)); } @@ -146,10 +145,8 @@ public class PasswordRecyclerAdapter extends RecyclerView.Adapter<PasswordRecycl public boolean onActionItemClicked(ActionMode mode, MenuItem item) { switch (item.getItemId()) { case R.id.menu_delete_password: - for (int i = 0; i < selectedItems.size(); i++) { - if (selectedItems.valueAt(i)) { - activity.deletePassword(PasswordRecyclerAdapter.this, selectedItems.keyAt(i)); - } + for (int position : selectedItems) { + activity.deletePassword(PasswordRecyclerAdapter.this, position); } mode.finish(); // Action picked, so close the CAB return true; @@ -197,14 +194,11 @@ public class PasswordRecyclerAdapter extends RecyclerView.Adapter<PasswordRecycl this.notifyItemRemoved(position); } - public void toggleSelection(int pos) { - if (selectedItems.get(pos, false)) { - selectedItems.delete(pos); + public void toggleSelection(int position) { + if (!selectedItems.remove(position)) { + selectedItems.add(position); } - else { - selectedItems.put(pos, true); - } - notifyItemChanged(pos); + notifyItemChanged(position); } }
\ No newline at end of file diff --git a/app/src/main/res/drawable/selected_rectangle.xml b/app/src/main/res/drawable/selected_rectangle.xml index bd3c40ae..ec8d2ad8 100644 --- a/app/src/main/res/drawable/selected_rectangle.xml +++ b/app/src/main/res/drawable/selected_rectangle.xml @@ -13,7 +13,7 @@ <item android:bottom="2dp" android:left="1dp" android:right="1dp"> <shape android:shape="rectangle" android:dither="true"> <corners android:radius="2dp" /> - <solid android:color="@color/grey_100" /> + <solid android:color="@color/blue_grey_100" /> <padding android:bottom="2dp" android:left="1dp"/> </shape> |