You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android开发:实现拖拽ImageView至垃圾桶后隐藏的方法问询

Solution: Hide/Remove Draggable Image When Dropped Into Trash Bin

Got it, let's get this sorted out! You've already got the drag functionality working for your draggableImage — now we just need to add logic to detect when it's dropped into the trash bin (imageView2) and hide or remove it. Here's the step-by-step fix:

Step 1: Update the Touch Listener to Handle Drop Events

We need to listen for the ACTION_UP event (when the user stops dragging) and check if the draggable image overlaps with the trash bin. Replace your existing touch listener code with this:

package com.example.fairytale_quest
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.MotionEvent
import android.view.View
import android.graphics.Rect
import android.view.ViewGroup
import kotlinx.android.synthetic.main.activity_main5.*

class Main5Activity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main5)
        supportActionBar?.hide()

        val listener = View.OnTouchListener { view, motionEvent ->
            when (motionEvent.action) {
                MotionEvent.ACTION_MOVE -> {
                    // Keep your existing drag logic
                    view.y = motionEvent.rawY - view.height / 2
                    view.x = motionEvent.rawX - view.width / 2
                }
                MotionEvent.ACTION_UP -> {
                    // Get screen-positioned bounding rectangles for both views
                    val draggableRect = Rect()
                    view.getGlobalVisibleRect(draggableRect)

                    val trashRect = Rect()
                    imageView2.getGlobalVisibleRect(trashRect)

                    // Check if the draggable image overlaps with the trash bin
                    if (Rect.intersects(draggableRect, trashRect)) {
                        // Option 1: Hide the image (can be re-shown later)
                        view.visibility = View.GONE

                        // Option 2: Permanently remove the image from the layout
                        // (view.parent as ViewGroup).removeView(view)
                    }
                }
            }
            true
        }
        draggableImage.setOnTouchListener(listener)
    }
}

Key Changes Explained

  • Added ACTION_UP handling: This triggers when the user releases the drag, which is the perfect time to check if the image was dropped into the trash.
  • Rect intersection check: Using getGlobalVisibleRect() gives us the screen coordinates of both views, and Rect.intersects() lets us easily detect overlap.
  • Two options for handling the image:
    • View.GONE: Hides the image and removes it from the layout's space (you can set it back to View.VISIBLE later if needed).
    • removeView(view): Permanently deletes the image from the layout (use this if you don't need to reuse it).

Your Layout XML Remains Unchanged

You don't need to modify your existing activity_main5.xml — the layout structure with both draggableImage and imageView2 is already set up correctly.

内容的提问来源于stack exchange,提问作者savera sleemy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:19:07