skip to Main Content

I’ve been searching for anything I can find on drag listeners, but all I found was the android studio guide which further confused me.

I’m trying to get this to work dashboard.setOnDragListener() (dashboard is just a cardview), but I’m confused by the parameters it needs, the error I get: No value passed for parameter ‘l’

I think the documentation mentioned 2 more parameters, but I’m lost

If I can get better help than what I got from the documentation I’ll be a happy man

MainActivity.kt:

var dashboard = findViewById<View>(R.id.dashboard)

2

Answers


  1. Remember that the target view that receives drag-and-drops gets the drag listener. The views that will be dragged get a call to startDragAndDrop.

    Here’s what mine looks like:

    inner class MyDragListener(): View.OnDragListener {
            override fun onDrag(view: View?, dragEvent: DragEvent?): Boolean {
    
                //you might also want to use this if to distinguish between different views that might be dragged
                if (dragEvent!!.action==DragEvent.ACTION_DROP)
                {
                    //do stuff here
                    return true
                }
    
        }
    
    

    Then for the views that are getting dragged, register them for the dragging operation like this.

    dragableView.setOnLongClickListener({
                    //here you could add some effects for 
                    //hinting to the user that something is being dragged.
                    //I like to use a vibrator and toggle the visibility  
                    //of something else on the screen
                    val dbs= View.DragShadowBuilder(it)
                    var clipData= ClipData.newPlainText("","")
                    it.startDragAndDrop(clipData,dbs,it,0)
                })
    
    

    Oh right, I almost forgot. Pass your draglistener to the target.

    dashboard.setOnDragListener(MyDragListener())
    
    Login or Signup to reply.
  2. img?.setOnDragListener { view, dragEvent ->
                when (dragEvent.action) {
                    DragEvent.ACTION_DRAG_STARTED -> {
                        Log.d(msg, "Action is DragEvent.ACTION_DRAG_STARTED")
                    }
                    DragEvent.ACTION_DRAG_ENTERED -> {
                        Log.d(msg, "Action is DragEvent.ACTION_DRAG_ENTERED")
                    }
                    DragEvent.ACTION_DRAG_EXITED -> {
                        Log.d(msg, "Action is DragEvent.ACTION_DRAG_EXITED")
                    }
                    DragEvent.ACTION_DRAG_LOCATION -> {
                        Log.d(msg, "Action is DragEvent.ACTION_DRAG_LOCATION")
                    }
                }
                true
            }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search