mirror of
https://github.com/AdrianKuta/Unbound-Drag-Drop.git
synced 2025-04-20 06:59:02 +02:00
Compare commits
3 Commits
e42b32142b
...
a3439ee9cd
Author | SHA1 | Date | |
---|---|---|---|
a3439ee9cd | |||
f1cfc2a98a | |||
cc5cc07f4a |
@ -7,7 +7,7 @@ plugins {
|
|||||||
|
|
||||||
android {
|
android {
|
||||||
namespace = "dev.adriankuta.unbounddragdrop"
|
namespace = "dev.adriankuta.unbounddragdrop"
|
||||||
version = "0.0.3"
|
version = "0.1.0"
|
||||||
|
|
||||||
kotlinOptions {
|
kotlinOptions {
|
||||||
jvmTarget = JavaVersion.VERSION_17.toString()
|
jvmTarget = JavaVersion.VERSION_17.toString()
|
||||||
@ -15,7 +15,7 @@ android {
|
|||||||
|
|
||||||
mavenPublishing {
|
mavenPublishing {
|
||||||
coordinates("dev.adriankuta", "unbound-drag-drop", version.toString())
|
coordinates("dev.adriankuta", "unbound-drag-drop", version.toString())
|
||||||
publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL, automaticRelease = true)
|
publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL)
|
||||||
signAllPublications()
|
signAllPublications()
|
||||||
pom {
|
pom {
|
||||||
name = "Unbound Drag & Drop"
|
name = "Unbound Drag & Drop"
|
||||||
|
@ -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 a RecyclerView.
|
* Helper class to handle drag and drop functionality in multiple RecyclerViews.
|
||||||
*
|
*
|
||||||
* @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(callback: Callback) :
|
class DragDropHelper(private val callback: Callback) : RecyclerView.OnChildAttachStateChangeListener {
|
||||||
RecyclerView.OnChildAttachStateChangeListener {
|
private val recyclerViews = mutableListOf<RecyclerView>()
|
||||||
private var mRecyclerView: RecyclerView? = null
|
private val recyclerItemClickListeners = mutableMapOf<RecyclerView, RecyclerItemClickListener>()
|
||||||
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,30 +39,42 @@ class DragDropHelper(callback: Callback) :
|
|||||||
* @param recyclerView The RecyclerView to attach to.
|
* @param recyclerView The RecyclerView to attach to.
|
||||||
*/
|
*/
|
||||||
fun attachToRecyclerView(recyclerView: RecyclerView?) {
|
fun attachToRecyclerView(recyclerView: RecyclerView?) {
|
||||||
if (mRecyclerView === recyclerView) {
|
recyclerView?.let {
|
||||||
return // nothing to do
|
if (recyclerViews.contains(it)) return // already attached
|
||||||
|
|
||||||
|
recyclerViews.add(it)
|
||||||
|
setupCallbacks(it)
|
||||||
}
|
}
|
||||||
if (mRecyclerView != null) {
|
}
|
||||||
destroyCallbacks()
|
|
||||||
}
|
/**
|
||||||
mRecyclerView = recyclerView
|
* Detaches the DragDropHelper from the specified RecyclerView.
|
||||||
mRecyclerView?.let {
|
*
|
||||||
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() {
|
private fun setupCallbacks(recyclerView: RecyclerView) {
|
||||||
mRecyclerView?.apply {
|
recyclerView.apply {
|
||||||
recyclerItemClickListener = RecyclerItemClickListener(
|
val clickListener = RecyclerItemClickListener(
|
||||||
context,
|
context,
|
||||||
this,
|
this,
|
||||||
onItemLongClickListener
|
onItemLongClickListener
|
||||||
).also {
|
)
|
||||||
mRecyclerView?.addOnItemTouchListener(it)
|
recyclerItemClickListeners[this] = clickListener
|
||||||
}
|
addOnItemTouchListener(clickListener)
|
||||||
addOnChildAttachStateChangeListener(this@DragDropHelper)
|
addOnChildAttachStateChangeListener(this@DragDropHelper)
|
||||||
setOnDragListener(dropListener)
|
setOnDragListener(dropListener)
|
||||||
}
|
}
|
||||||
@ -70,13 +82,16 @@ class DragDropHelper(callback: Callback) :
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes the callbacks from the RecyclerView.
|
* Removes the callbacks from the RecyclerView.
|
||||||
|
*
|
||||||
|
* @param recyclerView The RecyclerView to remove callbacks from.
|
||||||
*/
|
*/
|
||||||
private fun destroyCallbacks() {
|
private fun destroyCallbacks(recyclerView: RecyclerView) {
|
||||||
recyclerItemClickListener?.let {
|
recyclerItemClickListeners[recyclerView]?.let {
|
||||||
mRecyclerView?.removeOnItemTouchListener(it)
|
recyclerView.removeOnItemTouchListener(it)
|
||||||
|
recyclerItemClickListeners.remove(recyclerView)
|
||||||
}
|
}
|
||||||
mRecyclerView?.removeOnChildAttachStateChangeListener(this)
|
recyclerView.removeOnChildAttachStateChangeListener(this)
|
||||||
mRecyclerView?.setOnDragListener(null)
|
recyclerView.setOnDragListener(null)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -133,4 +148,4 @@ class DragDropHelper(callback: Callback) :
|
|||||||
targetViewHolder: ViewHolder?
|
targetViewHolder: ViewHolder?
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,19 +1,19 @@
|
|||||||
[versions]
|
[versions]
|
||||||
androidGradlePlugin = "8.1.4"
|
androidGradlePlugin = "8.1.4"
|
||||||
agp = "8.5.0"
|
agp = "8.5.1"
|
||||||
kotlin = "1.9.0"
|
kotlin = "1.9.24"
|
||||||
coreKtx = "1.13.1"
|
coreKtx = "1.13.1"
|
||||||
junit = "4.13.2"
|
junit = "4.13.2"
|
||||||
junitVersion = "1.1.5"
|
junitVersion = "1.2.1"
|
||||||
espressoCore = "3.5.1"
|
espressoCore = "3.6.1"
|
||||||
appcompat = "1.7.0"
|
appcompat = "1.7.0"
|
||||||
kotlinxCoroutinesAndroid = "1.7.3"
|
kotlinxCoroutinesAndroid = "1.7.3"
|
||||||
lifecycleViewmodelKtx = "2.8.2"
|
lifecycleViewmodelKtx = "2.8.4"
|
||||||
material = "1.12.0"
|
material = "1.12.0"
|
||||||
activity = "1.9.0"
|
activity = "1.9.1"
|
||||||
constraintlayout = "2.1.4"
|
constraintlayout = "2.1.4"
|
||||||
timber = "5.0.1"
|
timber = "5.0.1"
|
||||||
unboundDragDrop = "0.0.1"
|
unboundDragDrop = "0.1.0"
|
||||||
|
|
||||||
[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