-
Notifications
You must be signed in to change notification settings - Fork 294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for recyclerview-selection #324
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.xwray.groupie; | ||
|
||
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 GroupieLookup extends ItemDetailsLookup<Long> { | ||
|
||
public final static String GROUPIE_SELECTION_ID = "groupie-selection"; | ||
private RecyclerView recyclerView; | ||
|
||
public GroupieLookup(RecyclerView recyclerView) { | ||
this.recyclerView = recyclerView; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public ItemDetails<Long> getItemDetails(@NonNull MotionEvent e) { | ||
View view = recyclerView.findChildViewUnder(e.getX(), e.getY()); | ||
if (view != null) { | ||
RecyclerView.ViewHolder viewHolder = recyclerView.getChildViewHolder(view); | ||
if (viewHolder instanceof GroupieViewHolder) { | ||
return ((GroupieViewHolder) viewHolder).getItemDetails(); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
import androidx.recyclerview.selection.ItemDetailsLookup; | ||
import androidx.recyclerview.widget.RecyclerView; | ||
import android.view.View; | ||
|
||
|
@@ -94,4 +95,19 @@ public Item getItem() { | |
public View getRoot() { | ||
return itemView; | ||
} | ||
|
||
public ItemDetailsLookup.ItemDetails<Long> getItemDetails() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure whether this is the best place for this. Should this instead be on the item so that users can overwrite it? I can imagine us wanting to provide users with a custom selection key. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Regarding the type of the custom selection key I would like to suggest Long. |
||
return new ItemDetailsLookup.ItemDetails<Long>(){ | ||
@Override | ||
public int getPosition() { | ||
return getAdapterPosition(); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Long getSelectionKey() { | ||
return getItemId(); | ||
} | ||
}; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding this tracker to the whole recyclerview in the example breaks it. You should only add selection on the items that can actually be selected. From the looks of it, we should probably change
GroupieLookup
to only returns items if they can be selected