From 6b8222f3c578b60f8e76be1ae6afa45131c3a0d7 Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 25 Aug 2018 15:55:43 +0000 Subject: [PATCH] Recyclerview selection experiment --- .idea/caches/build_file_checksums.ser | Bin 535 -> 535 bytes app/build.gradle | 1 + .../eu/faircode/email/AdapterMessage.java | 21 +++++- .../eu/faircode/email/FragmentMessages.java | 62 +++++++++++++++++- .../email/MyActionModeController.java | 38 +++++++++++ .../java/eu/faircode/email/MyItemDetail.java | 30 +++++++++ .../eu/faircode/email/MyItemKeyProvider.java | 37 +++++++++++ .../java/eu/faircode/email/MyItemLookup.java | 35 ++++++++++ 8 files changed, 221 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/eu/faircode/email/MyActionModeController.java create mode 100644 app/src/main/java/eu/faircode/email/MyItemDetail.java create mode 100644 app/src/main/java/eu/faircode/email/MyItemKeyProvider.java create mode 100644 app/src/main/java/eu/faircode/email/MyItemLookup.java diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index 7da81e0eb63bda3a207eac3696dc3a9a2ca82ad7..1cd885f082fae894f1b99cde5e370a41d8b4dae0 100644 GIT binary patch delta 32 ocmbQvGM#0@Oy;hW-!{(4ViYXzYGHJovtO{_wYc%`GOsli0OTVMEC2ui delta 32 ocmbQvGM#0@OlEcreateSelectAnything()) + .withOnDragInitiatedListener(new OnDragInitiatedListener() { + @Override + public boolean onDragInitiated(@NonNull MotionEvent e) { + Log.i(Helper.TAG, "onDragInitiated"); + return true; + } + }).build(); + + + adapter.setSelectionTracker(selectionTracker); + + selectionTracker.addObserver(new SelectionTracker.SelectionObserver() { + @Override + public void onItemStateChanged(@NonNull Object key, boolean selected) { + Log.i(Helper.TAG, "onItemStateChanged"); + super.onItemStateChanged(key, selected); + } + + @Override + public void onSelectionRefresh() { + Log.i(Helper.TAG, "onSelectionRefresh"); + super.onSelectionRefresh(); + } + + @Override + public void onSelectionChanged() { + Log.i(Helper.TAG, "onSelectionChanged"); + super.onSelectionChanged(); + } + + @Override + public void onSelectionRestored() { + Log.i(Helper.TAG, "onSelectionRestored"); + super.onSelectionRestored(); + } + }); + + if (savedInstanceState != null) + selectionTracker.onRestoreInstanceState(savedInstanceState); + pbWait.setVisibility(View.GONE); grpReady.setVisibility(View.VISIBLE); diff --git a/app/src/main/java/eu/faircode/email/MyActionModeController.java b/app/src/main/java/eu/faircode/email/MyActionModeController.java new file mode 100644 index 0000000000..49ef39db9f --- /dev/null +++ b/app/src/main/java/eu/faircode/email/MyActionModeController.java @@ -0,0 +1,38 @@ +package eu.faircode.email; + +import android.content.Context; +import android.view.Menu; +import android.view.MenuItem; + +import androidx.appcompat.view.ActionMode; +import androidx.recyclerview.selection.SelectionTracker; + +public class MyActionModeController implements ActionMode.Callback { + private final Context context; + private final SelectionTracker selectionTracker; + + public MyActionModeController(Context context, SelectionTracker selectionTracker) { + this.context = context; + this.selectionTracker = selectionTracker; + } + + @Override + public boolean onCreateActionMode(ActionMode actionMode, Menu menu) { + return false; + } + + @Override + public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) { + return false; + } + + @Override + public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) { + return false; + } + + @Override + public void onDestroyActionMode(ActionMode actionMode) { + selectionTracker.clearSelection(); + } +} \ No newline at end of file diff --git a/app/src/main/java/eu/faircode/email/MyItemDetail.java b/app/src/main/java/eu/faircode/email/MyItemDetail.java new file mode 100644 index 0000000000..1bff9392f3 --- /dev/null +++ b/app/src/main/java/eu/faircode/email/MyItemDetail.java @@ -0,0 +1,30 @@ +package eu.faircode.email; + +import android.util.Log; + +import androidx.annotation.Nullable; +import androidx.recyclerview.selection.ItemDetailsLookup; + +public class MyItemDetail extends ItemDetailsLookup.ItemDetails { + private final int adapterPosition; + private final Long selectionKey; + + public MyItemDetail(int adapterPosition, Long selectionKey) { + Log.i(Helper.TAG, "MyItemDetail"); + this.adapterPosition = adapterPosition; + this.selectionKey = selectionKey; + } + + @Override + public int getPosition() { + Log.i(Helper.TAG, "MyItemDetail.getPosition=" + adapterPosition); + return adapterPosition; + } + + @Nullable + @Override + public Long getSelectionKey() { + Log.i(Helper.TAG, "MyItemDetail.getSelectionKey=" + selectionKey); + return selectionKey; + } +} diff --git a/app/src/main/java/eu/faircode/email/MyItemKeyProvider.java b/app/src/main/java/eu/faircode/email/MyItemKeyProvider.java new file mode 100644 index 0000000000..4ca91582c8 --- /dev/null +++ b/app/src/main/java/eu/faircode/email/MyItemKeyProvider.java @@ -0,0 +1,37 @@ +package eu.faircode.email; + +import android.util.Log; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.paging.PagedList; +import androidx.recyclerview.selection.ItemKeyProvider; +import androidx.recyclerview.widget.RecyclerView; + +public class MyItemKeyProvider extends ItemKeyProvider { + private final PagedList messages; + + public MyItemKeyProvider(PagedList messages) { + super(ItemKeyProvider.SCOPE_MAPPED); + this.messages = messages; + Log.i(Helper.TAG, "MyItemKeyProvider"); + } + + @Nullable + @Override + public Long getKey(int position) { + Log.i(Helper.TAG, "MyItemKeyProvider.getKey pos=" + position + " key=" + messages.get(position).id); + return messages.get(position).id; + } + + @Override + public int getPosition(@NonNull Long key) { + Log.i(Helper.TAG, "MyItemKeyProvider.getPosition key=" + key); + int pos = RecyclerView.NO_POSITION; + for (int i = 0; i < messages.size(); i++) + if (messages.get(i).id.equals(key)) + pos = i; + Log.i(Helper.TAG, "MyItemKeyProvider.getPosition key=" + key + " pos=" + pos); + return pos; + } +} \ No newline at end of file diff --git a/app/src/main/java/eu/faircode/email/MyItemLookup.java b/app/src/main/java/eu/faircode/email/MyItemLookup.java new file mode 100644 index 0000000000..8bca612960 --- /dev/null +++ b/app/src/main/java/eu/faircode/email/MyItemLookup.java @@ -0,0 +1,35 @@ +package eu.faircode.email; + +import android.util.Log; +import android.view.MotionEvent; +import android.view.View; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.recyclerview.selection.ItemDetailsLookup; +import androidx.recyclerview.widget.RecyclerView; + +public class MyItemLookup extends ItemDetailsLookup { + + private final RecyclerView recyclerView; + + public MyItemLookup(RecyclerView recyclerView) { + Log.i(Helper.TAG, "MyItemLookup"); + this.recyclerView = recyclerView; + } + + @Nullable + @Override + public ItemDetails getItemDetails(@NonNull MotionEvent e) { + Log.i(Helper.TAG, "MyItemLookup.getItemDetails"); + View view = recyclerView.findChildViewUnder(e.getX(), e.getY()); + if (view != null) { + RecyclerView.ViewHolder viewHolder = recyclerView.getChildViewHolder(view); + if (viewHolder instanceof AdapterMessage.ViewHolder) { + return ((AdapterMessage.ViewHolder) viewHolder).getItemDetails(); + } + } + + return null; + } +}