Skip to content

Latest commit

 

History

History
85 lines (82 loc) · 2.65 KB

README.md

File metadata and controls

85 lines (82 loc) · 2.65 KB

Briefing

Do you want to implement In-App drag and drop in your android kotlin application? It's very simple! Here is 5 (or less) steps to do it with DSL using easy dnd (if you don't like DSL skip it):

  1. Create class which will store all transfering data or use some kotlin type:
data class Assignment(val tag: String)
  1. Create sets with sender and receiver objects:
val sendersSet = setOf(textView.assign(Assignment("textview")))
val receiversSet = setOf(textView2.assign(Assignment("textview2")))

You can also use infix form of function!

textView2 assign Assignment("textview2")

Or you can do it later (see point 4b).

  1. Use enableDragAndDrop function and define callbacks using default (optional):
enableDragAndDrop<Assignment, Assignment> {
    default {
        onDropped { sender, receiver ->
            Toast.makeText(applicationContext, "Dropped sender on receiver", Toast.LENGTH_SHORT).show()
        }
    }
}

4a. Create set mappings:

enableDragAndDrop<Assignment, Assignment> {
    ...
    mapSets(sendersSet, receiversSet)
}

You can create as many mappings as you want. If some mapping must have unique behaviour just write it and it will override default methods. Optionally you can also call default method using callSuper() function.

enableDragAndDrop<Assignment, Assignment> {
    ...
    mapSets(sendersSet, receiversSet) {
        onDragEntered {
            callSuper(it)
            //some action when sender object covers receiver object
        }
    }
}

4b. There is also a new way to create mappings. You can define mapping right here in the DSL!

mapSets {
    textView assignSender Assignment("textview")
    textView2 assignReceiver Assignment("textview2")
    config {
        onDropped { ... }
        onDragEntered { ... }
        onDragExited { ... }
    }
}
  1. Enjoy it! You can also find kotlin docs here.

Classic way to configure

To configure drag-and-drop do next steps:

  1. Create instance of DragAndDropManager class.
  2. Init default config.
  3. Create sets and optionally LocalConfig for each (see point 2 of previous topic).
  4. Use mapSets method for all desired mappings.
  5. Call applyDragAndDrop() method to enable drag-and-drop.

Installation

  1. Add repository into the root build.gradle file:
allprojects {
  repositories {
    ...
    maven { url 'https://jitpack.io' }
  }
}
  1. Add the dependency
dependencies {
        implementation 'com.github.STRENCH0:EasyDragAndDrop:2.0.1'
}