Newps
Newps

Reputation: 1

OnDragListener not being called during ImageView drag and drop

I am writing a solitaire-type game, based on cribbage. The game consists of 5 hands of 4 cards each, along with a "crib" that is automatically populated for the player. The game initializes with the first card in each of the 4 hands visible, along with a draw card that is dragged to whichever hand the player chooses. I have got the game initializing properly and the draw card drags just fine, but when I go to drop it on any of the target locations, the DragListener is never called.

I looked at a bunch of examples online and tried some variations, but the consensus seems to be to use the long click listener for the draw card and then for the target card locations, use the drag listener.

I'm trying to do this in Kotlin; I had done the game years ago in Java and it worked fine, but I want to update it and keep it current now.

Here's what I have implemented. I'm only trying to test dragging to the first hand right now and once I get that working, I will extend to the others:

class GameActivity : AppCompatActivity(), View.OnTouchListener, View.OnDragListener {



    var deltaX: Float = 0f
    var deltaY: Float = 0f
    var initialX: Float = 0f
    var initialY: Float = 0f
    var finalX: Float = 0f
    var finalY: Float = 0f

    var scoreForGame: Int = 0

    val TAG: String = "GamePageActivity"



    private lateinit var gameEngine: CribbageSolitaireEngine


    private lateinit var binding: ActivityGameBinding

    lateinit var layoutParams : RelativeLayout.LayoutParams

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //enableEdgeToEdge()

        binding = ActivityGameBinding.inflate(layoutInflater)
        val view = binding.root

        startNewGame(this.binding)

        setContentView(view)

        val drawCardImage: ImageView = binding.drawCard

        Log.d(TAG, "SET ON LONG CLICK LISTENER")
        drawCardImage.setOnLongClickListener { v ->

            val dragShadowBuilder = View.DragShadowBuilder(v)
            val clipData = ClipData.newPlainText("","")

            v.startDragAndDrop(clipData, dragShadowBuilder, null, View.DRAG_FLAG_GLOBAL)

            //  it.visibility = View.INVISIBLE

            true

        }

// Set the drag event listener for the View.
        val firstHandSecondCard : ImageView = binding.hand1Card2

        firstHandSecondCard.setOnDragListener { v, e ->

            Log.d(TAG, "IN DRAG LISTENER")
            // Handle each of the expected events.
            when (e.action) {
                ACTION_DRAG_STARTED -> {
                    Log.d(TAG, "ON DRAG STARTED")
                    // Determine whether this View can accept the dragged data.
                    if (e.clipDescription.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
                        // As an example, apply a blue color tint to the View to
                        // indicate that it can accept data.
                        //val targetColor = Color.Blue
                        //(v as? ImageView)?.setColorFilter(targetColor)


                        // Invalidate the view to force a redraw in the new tint.
                        v.invalidate()

                        // Return true to indicate that the View can accept the dragged
                        // data.
                        true
                    } else {
                        // Return false to indicate that, during the current drag and
                        // drop operation, this View doesn't receive events again until
                        // ACTION_DRAG_ENDED is sent.
                        false
                    }
                }
                ACTION_DRAG_ENTERED -> {
                    Log.d(TAG, "ON DRAG ENTERED")
                    // Apply a green tint to the View.
                    //(v as? ImageView)?.setColorFilter(Color.Green)

                    // Invalidate the view to force a redraw in the new tint.
                    v.invalidate()

                    // Return true. The value is ignored.
                    true
                }

                ACTION_DRAG_LOCATION -> {
                    // Ignore the event.
                    Log.d(TAG, "ON DRAG LOCATION")
                    true
                }
                ACTION_DRAG_EXITED -> {

                    Log.d(TAG, "ON DRAG EXITED")
                    // Reset the color tint to blue.
                   // (v as? ImageView)?.setColorFilter(Color.Gray)

                    // Invalidate the view to force a redraw in the new tint.
                    v.invalidate()

                    // Return true. The value is ignored.
                    true
                }
                ACTION_DROP -> {

                    Log.d(TAG, "ON DRAG DROP")
                    // Get the item containing the dragged data.
                    val item: ClipData.Item = e.clipData.getItemAt(0)

                    // Get the text data from the item.
                    val dragData = item.text

                    // Display a message containing the dragged data.
                    Toast.makeText(this, "Dragged data is $dragData", Toast.LENGTH_LONG).show()

                    // Turn off color tints.
                    (v as? ImageView)?.clearColorFilter()

                    // Invalidate the view to force a redraw.
                    v.invalidate()

                    // Return true. DragEvent.getResult() returns true.
                    true
                }

                ACTION_DRAG_ENDED -> {

                    Log.d(TAG, "ON DRAG ENDED")
                    // Turn off color tinting.
                    (v as? ImageView)?.clearColorFilter()

                    // Invalidate the view to force a redraw.
                    v.invalidate()

                    // Do a getResult() and display what happens.
                    when(e.result) {
                        true ->
                            Toast.makeText(this, "The drop was handled.", Toast.LENGTH_LONG)
                        else ->
                            Toast.makeText(this, "The drop didn't work.", Toast.LENGTH_LONG)
                    }.show()

                    // Return true. The value is ignored.
                    true
                }
                else -> {
                    // An unknown action type is received.
                    Log.e("DragDrop Example", "Unknown action type received by View.OnDragListener.")
                    false
                }
            }
        }

Upvotes: 0

Views: 16

Answers (0)

Related Questions