mirror of
https://github.com/AdrianKuta/Unbound-Drag-Drop.git
synced 2025-04-20 15:09:02 +02:00
Compare commits
No commits in common. "main" and "v.0.0.4" have entirely different histories.
@ -27,7 +27,7 @@ To include Unbound Drag & Drop in your project, add the following dependency to
|
|||||||
file:
|
file:
|
||||||
|
|
||||||
```kotlin
|
```kotlin
|
||||||
implementation("dev.adriankuta:unbound-drag-drop:0.1.0")
|
implementation("dev.adriankuta:unbound-drag-drop:0.0.2")
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
@ -7,7 +7,7 @@ plugins {
|
|||||||
|
|
||||||
android {
|
android {
|
||||||
namespace = "dev.adriankuta.unbounddragdrop"
|
namespace = "dev.adriankuta.unbounddragdrop"
|
||||||
version = "0.1.0"
|
version = "0.0.4"
|
||||||
|
|
||||||
kotlinOptions {
|
kotlinOptions {
|
||||||
jvmTarget = JavaVersion.VERSION_17.toString()
|
jvmTarget = JavaVersion.VERSION_17.toString()
|
||||||
|
@ -6,15 +6,15 @@ import android.view.View
|
|||||||
import androidx.recyclerview.widget.RecyclerView
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
import androidx.recyclerview.widget.RecyclerView.DRAG_FLAG_OPAQUE
|
import androidx.recyclerview.widget.RecyclerView.DRAG_FLAG_OPAQUE
|
||||||
import androidx.recyclerview.widget.RecyclerView.ViewHolder
|
import androidx.recyclerview.widget.RecyclerView.ViewHolder
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper class to handle drag and drop functionality in multiple RecyclerViews.
|
* Helper class to handle drag and drop functionality in a RecyclerView.
|
||||||
*
|
*
|
||||||
* @param callback A Callback object to handle the drag and drop events.
|
* @param callback A Callback object to handle the drag and drop events.
|
||||||
*/
|
*/
|
||||||
class DragDropHelper(private val callback: Callback) : RecyclerView.OnChildAttachStateChangeListener {
|
class DragDropHelper(callback: Callback) :
|
||||||
private val recyclerViews = mutableListOf<RecyclerView>()
|
RecyclerView.OnChildAttachStateChangeListener {
|
||||||
private val recyclerItemClickListeners = mutableMapOf<RecyclerView, RecyclerItemClickListener>()
|
private var mRecyclerView: RecyclerView? = null
|
||||||
|
private var recyclerItemClickListener: RecyclerItemClickListener? = null
|
||||||
private val dropListener = DropListener(callback)
|
private val dropListener = DropListener(callback)
|
||||||
private val onItemLongClickListener: RecyclerItemClickListener.OnItemLongClickListener by lazy {
|
private val onItemLongClickListener: RecyclerItemClickListener.OnItemLongClickListener by lazy {
|
||||||
object : RecyclerItemClickListener.OnItemLongClickListener {
|
object : RecyclerItemClickListener.OnItemLongClickListener {
|
||||||
@ -39,42 +39,30 @@ class DragDropHelper(private val callback: Callback) : RecyclerView.OnChildAttac
|
|||||||
* @param recyclerView The RecyclerView to attach to.
|
* @param recyclerView The RecyclerView to attach to.
|
||||||
*/
|
*/
|
||||||
fun attachToRecyclerView(recyclerView: RecyclerView?) {
|
fun attachToRecyclerView(recyclerView: RecyclerView?) {
|
||||||
recyclerView?.let {
|
if (mRecyclerView === recyclerView) {
|
||||||
if (recyclerViews.contains(it)) return // already attached
|
return // nothing to do
|
||||||
|
|
||||||
recyclerViews.add(it)
|
|
||||||
setupCallbacks(it)
|
|
||||||
}
|
}
|
||||||
|
if (mRecyclerView != null) {
|
||||||
|
destroyCallbacks()
|
||||||
}
|
}
|
||||||
|
mRecyclerView = recyclerView
|
||||||
/**
|
mRecyclerView?.let {
|
||||||
* Detaches the DragDropHelper from the specified RecyclerView.
|
setupCallbacks()
|
||||||
*
|
|
||||||
* @param recyclerView The RecyclerView to detach from.
|
|
||||||
*/
|
|
||||||
fun detachFromRecyclerView(recyclerView: RecyclerView?) {
|
|
||||||
recyclerView?.let {
|
|
||||||
if (!recyclerViews.contains(it)) return // not attached
|
|
||||||
|
|
||||||
destroyCallbacks(it)
|
|
||||||
recyclerViews.remove(it)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets up the necessary callbacks for the RecyclerView.
|
* Sets up the necessary callbacks for the RecyclerView.
|
||||||
*
|
|
||||||
* @param recyclerView The RecyclerView to setup callbacks for.
|
|
||||||
*/
|
*/
|
||||||
private fun setupCallbacks(recyclerView: RecyclerView) {
|
private fun setupCallbacks() {
|
||||||
recyclerView.apply {
|
mRecyclerView?.apply {
|
||||||
val clickListener = RecyclerItemClickListener(
|
recyclerItemClickListener = RecyclerItemClickListener(
|
||||||
context,
|
context,
|
||||||
this,
|
this,
|
||||||
onItemLongClickListener
|
onItemLongClickListener
|
||||||
)
|
).also {
|
||||||
recyclerItemClickListeners[this] = clickListener
|
mRecyclerView?.addOnItemTouchListener(it)
|
||||||
addOnItemTouchListener(clickListener)
|
}
|
||||||
addOnChildAttachStateChangeListener(this@DragDropHelper)
|
addOnChildAttachStateChangeListener(this@DragDropHelper)
|
||||||
setOnDragListener(dropListener)
|
setOnDragListener(dropListener)
|
||||||
}
|
}
|
||||||
@ -82,16 +70,13 @@ class DragDropHelper(private val callback: Callback) : RecyclerView.OnChildAttac
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes the callbacks from the RecyclerView.
|
* Removes the callbacks from the RecyclerView.
|
||||||
*
|
|
||||||
* @param recyclerView The RecyclerView to remove callbacks from.
|
|
||||||
*/
|
*/
|
||||||
private fun destroyCallbacks(recyclerView: RecyclerView) {
|
private fun destroyCallbacks() {
|
||||||
recyclerItemClickListeners[recyclerView]?.let {
|
recyclerItemClickListener?.let {
|
||||||
recyclerView.removeOnItemTouchListener(it)
|
mRecyclerView?.removeOnItemTouchListener(it)
|
||||||
recyclerItemClickListeners.remove(recyclerView)
|
|
||||||
}
|
}
|
||||||
recyclerView.removeOnChildAttachStateChangeListener(this)
|
mRecyclerView?.removeOnChildAttachStateChangeListener(this)
|
||||||
recyclerView.setOnDragListener(null)
|
mRecyclerView?.setOnDragListener(null)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,19 +1,19 @@
|
|||||||
[versions]
|
[versions]
|
||||||
androidGradlePlugin = "8.1.4"
|
androidGradlePlugin = "8.1.4"
|
||||||
agp = "8.5.1"
|
agp = "8.5.0"
|
||||||
kotlin = "1.9.24"
|
kotlin = "1.9.0"
|
||||||
coreKtx = "1.13.1"
|
coreKtx = "1.13.1"
|
||||||
junit = "4.13.2"
|
junit = "4.13.2"
|
||||||
junitVersion = "1.2.1"
|
junitVersion = "1.1.5"
|
||||||
espressoCore = "3.6.1"
|
espressoCore = "3.5.1"
|
||||||
appcompat = "1.7.0"
|
appcompat = "1.7.0"
|
||||||
kotlinxCoroutinesAndroid = "1.7.3"
|
kotlinxCoroutinesAndroid = "1.7.3"
|
||||||
lifecycleViewmodelKtx = "2.8.4"
|
lifecycleViewmodelKtx = "2.8.2"
|
||||||
material = "1.12.0"
|
material = "1.12.0"
|
||||||
activity = "1.9.1"
|
activity = "1.9.0"
|
||||||
constraintlayout = "2.1.4"
|
constraintlayout = "2.1.4"
|
||||||
timber = "5.0.1"
|
timber = "5.0.1"
|
||||||
unboundDragDrop = "0.1.0"
|
unboundDragDrop = "0.0.1"
|
||||||
|
|
||||||
[libraries]
|
[libraries]
|
||||||
android-tools-build-gradle-plugin = { group = "com.android.tools.build", name = "gradle", version.ref = "androidGradlePlugin" }
|
android-tools-build-gradle-plugin = { group = "com.android.tools.build", name = "gradle", version.ref = "androidGradlePlugin" }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user