How to Implement Drag and Drop Functionality in Android Apps

编程狂想曲
编程狂想曲 2021-12-02T19:23:31+08:00
0 0 0
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/image" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Drag me"
        android:padding="16dp"
        android:background="@drawable/draggable_bg" />

</LinearLayout>
class MainActivity : AppCompatActivity(), View.OnDragListener, View.OnTouchListener {

    private var draggableTextView: TextView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        draggableTextView = findViewById(R.id.textView)
        draggableTextView?.setOnTouchListener(this)
        findViewById<LinearLayout>(R.id.linearLayout).setOnDragListener(this)
    }

    override fun onTouch(v: View, event: MotionEvent): Boolean {
        if (event.action == MotionEvent.ACTION_DOWN) {
            val shadowBuilder = View.DragShadowBuilder(v)
            v.startDrag(null, shadowBuilder, v, 0)
            v.visibility = View.INVISIBLE
            return true
        }
        return false
    }

    override fun onDrag(v: View, event: DragEvent): Boolean {
        when (event.action) {
            DragEvent.ACTION_DRAG_STARTED -> { // Called once drag is started
                // Do necessary initialization
                return true
            }
            DragEvent.ACTION_DRAG_ENTERED -> { // Called when the dragged view enters the bounds of the target view
                // Apply any visual changes to indicate hovering
                return true
            }
            DragEvent.ACTION_DRAG_EXITED -> { // Called when the dragged view exits the bounds of the target view
                // Revert any visual changes made during ACTION_DRAG_ENTERED
                return true
            }
            DragEvent.ACTION_DROP -> { // Called when the dragged view is released over the target view
                // Handle the dropped object
                val draggedView = event.localState as View
                val dropTarget = v as LinearLayout
                dropTarget.addView(draggedView)
                draggedView.visibility = View.VISIBLE
                return true
            }
            DragEvent.ACTION_DRAG_ENDED -> { // Called when the drag operation ends
                // Reset any visual changes made during ACTION_DRAG_STARTED
                val draggedView = event.localState as View
                draggedView.visibility = View.VISIBLE
                return true
            }
        }
        return false
    }
}
public class MainActivity extends AppCompatActivity implements View.OnDragListener, View.OnTouchListener {

    private TextView draggableTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        draggableTextView = findViewById(R.id.textView);
        draggableTextView.setOnTouchListener(this);
        findViewById(R.id.linearLayout).setOnDragListener(this);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
            v.startDrag(null, shadowBuilder, v, 0);
            v.setVisibility(View.INVISIBLE);
            return true;
        }
        return false;
    }

    @Override
    public boolean onDrag(View v, DragEvent event) {
        switch (event.getAction()) {
            case DragEvent.ACTION_DRAG_STARTED: // Called once drag is started
                // Do necessary initialization
                return true;

            case DragEvent.ACTION_DRAG_ENTERED: // Called when the dragged view enters the bounds of the target view
                // Apply any visual changes to indicate hovering
                return true;

            case DragEvent.ACTION_DRAG_EXITED: // Called when the dragged view exits the bounds of the target view
                // Revert any visual changes made during ACTION_DRAG_ENTERED
                return true;

            case DragEvent.ACTION_DROP: // Called when the dragged view is released over the target view
                // Handle the dropped object
                View draggedView = (View) event.getLocalState();
                LinearLayout dropTarget = (LinearLayout) v;
                dropTarget.addView(draggedView);
                draggedView.setVisibility(View.VISIBLE);
                return true;

            case DragEvent.ACTION_DRAG_ENDED: // Called when the drag operation ends
                // Reset any visual changes made during ACTION_DRAG_STARTED
                View draggedView = (View) event.getLocalState();
                draggedView.setVisibility(View.VISIBLE);
                return true;
        }
        return false;
    }
}
相关推荐
广告位招租

相似文章

    评论 (0)

    0/2000