Fix multiple selection with RecyclerView

This commit is contained in:
DSIW 2016-06-10 04:50:24 +02:00
parent 0e700ce21b
commit 7a25cbf1d6
4 changed files with 21 additions and 18 deletions

View file

@ -1,8 +1,8 @@
package com.zeapo.pwdstore.utils; package com.zeapo.pwdstore.utils;
import android.graphics.Color;
import android.os.Build; import android.os.Build;
import android.support.v7.view.ActionMode; import android.support.v7.view.ActionMode;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
@ -91,7 +91,8 @@ public class PasswordRecyclerAdapter extends RecyclerView.Adapter<PasswordRecycl
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (mActionMode != null) { if (mActionMode != null) {
toggleSelection(holder, holder.getAdapterPosition(), null, pass.getType()); toggleSelection(holder.getAdapterPosition());
mActionMode.setTitle("" + selectedItems.size());
if (selectedItems.isEmpty()) { if (selectedItems.isEmpty()) {
mActionMode.finish(); mActionMode.finish();
} else if (selectedItems.size() == 1 && !canEdit) { } else if (selectedItems.size() == 1 && !canEdit) {
@ -106,6 +107,7 @@ public class PasswordRecyclerAdapter extends RecyclerView.Adapter<PasswordRecycl
} else { } else {
listener.onFragmentInteraction(pass); listener.onFragmentInteraction(pass);
} }
notifyItemChanged(holder.getAdapterPosition());
} }
}); });
@ -115,17 +117,27 @@ public class PasswordRecyclerAdapter extends RecyclerView.Adapter<PasswordRecycl
if (mActionMode != null) { if (mActionMode != null) {
return false; return false;
} }
toggleSelection(holder, holder.getAdapterPosition(), null, pass.getType()); toggleSelection(holder.getAdapterPosition());
canEdit = pass.getType() == PasswordItem.TYPE_PASSWORD; canEdit = pass.getType() == PasswordItem.TYPE_PASSWORD;
// Start the CAB using the ActionMode.Callback // Start the CAB using the ActionMode.Callback
mActionMode = activity.startSupportActionMode(mActionModeCallback); mActionMode = activity.startSupportActionMode(mActionModeCallback);
mActionMode.setTitle("" + selectedItems.size());
mActionMode.invalidate(); mActionMode.invalidate();
notifyItemChanged(holder.getAdapterPosition());
return true; return true;
} }
}); });
// after removal, everything is rebound for some reason; views are shuffled? // after removal, everything is rebound for some reason; views are shuffled?
holder.view.setSelected(selectedItems.contains(position)); boolean selected = selectedItems.contains(position);
holder.view.setSelected(selected);
if (selected) {
holder.itemView.setBackgroundResource(R.color.orange_200);
holder.type.setTextColor(Color.BLACK);
} else {
holder.itemView.setBackgroundResource(Color.alpha(1));
holder.type.setTextColor(activity.getColor(R.color.grey_500));
}
} }
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() { private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
@ -213,20 +225,9 @@ public class PasswordRecyclerAdapter extends RecyclerView.Adapter<PasswordRecycl
updateSelectedItems(position, selectedItems); updateSelectedItems(position, selectedItems);
} }
public void toggleSelection(ViewHolder holder, int position, CardView card, char type) { public void toggleSelection(int position) {
if (!selectedItems.remove(position)) { if (!selectedItems.remove(position)) {
selectedItems.add(position); selectedItems.add(position);
if (type == PasswordItem.TYPE_CATEGORY) {
// card.setCardBackgroundColor(activity.getResources().getColor(R.color.blue_grey_100));
} else {
// card.setCardBackgroundColor(activity.getResources().getColor(R.color.blue_grey_100));
}
} else {
if (type == PasswordItem.TYPE_CATEGORY) {
// card.setCardBackgroundColor(activity.getResources().getColor(R.color.blue_grey_200));
} else {
// card.setCardBackgroundColor(activity.getResources().getColor(R.color.blue_grey_50));
}
} }
} }

View file

@ -21,7 +21,7 @@
android:layout_gravity="bottom|end" android:layout_gravity="bottom|end"
app:elevation="6dp" app:elevation="6dp"
app:pressedTranslationZ="12dp" app:pressedTranslationZ="12dp"
app:backgroundTint="@color/blue_grey_500" app:backgroundTint="@color/accent"
app:rippleColor="@color/blue_grey_50" app:rippleColor="@color/blue_grey_50"
app:borderWidth="0dp" app:borderWidth="0dp"
android:layout_margin="@dimen/fab_compat_margin" android:layout_margin="@dimen/fab_compat_margin"

View file

@ -3,7 +3,7 @@
xmlns:card_view="http://schemas.android.com/apk/res-auto" xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_height="wrap_content"> android:background="?android:attr/activatedBackgroundIndicator">
<RelativeLayout <RelativeLayout
android:layout_width="match_parent" android:layout_width="match_parent"

View file

@ -1,5 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<color name="accent">#ff7043</color>
<color name="red_50">#fde0dc</color> <color name="red_50">#fde0dc</color>
<color name="red_100">#f9bdbb</color> <color name="red_100">#f9bdbb</color>
<color name="red_200">#f69988</color> <color name="red_200">#f69988</color>