What I want to achieve: I want to have a view inside a scrollable layout (Recyclerview
with GridlayoutManager
) with tiles (Views) in it. Dragging and dropping an item inside of the RecyclerView
should adjust the position of the icon and swap with the other elements. When a drag starts, an icon above the RecyclerView
will change to a trash icon and dragging the view to this icon will delete it from this RecyclerView
.
I tried this excellent tutorial, but I didn't find a way how to handle dragging outside of the Recyclerview
as the ItemTouchHelper.Callback
uses only Recycler.ViewHolder
elements as possible targets.
The method interpolateOutOfBoundsScroll()
gives feedback if the view moves out of the boundaries, but will only give back the total size that is offscreen, but no coordinates. Also, trying to drag the view out of the Recyclerview
always results in cutting of the View where it passes the borders of the Recyclerview
.
Does anyone have an idea how I could achieve this effect?
Drag and Drop can be added in a RecyclerView using the ItemTouchHelper utility class. Following are the important methods in the ItemTouchHelper. Callback interface which needs to be implemented: isLongPressDragEnabled - return true here to enable long press on the RecyclerView rows for drag and drop.
Android Swipe To Delete. Swipe to delete feature is commonly used to delete rows from a RecyclerView. In order to implement Swipe to delete feature, we need to use the ItemTouchHelper utility class.
You can achieve this simply by set this attribute for the parent of the RecyclerView:
android:clipChildren="false"
Edit: thank Adam Katz, I don't know why but sometimes you have to add this to the RecyclerView to make it work:
android:clipToPadding="false"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With