图像在droppping imageview android

图像在droppping imageview android,第1张

我正在为Android尝试简单的拖放程序。我在互联网上提到了各种材料。问题是我能够正确地拖动图像,但当我放下它时,它就会消失。 以下是代码..

package com.example.vishal_raj.dragndrop;

    import android.app.Activity;
    import android.content.ClipData;
    import android.graphics.drawable.Drawable;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.DragEvent;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.DragShadowBuilder;
    import android.view.View.OnDragListener;
    import android.view.View.OnTouchListener;
    import android.view.ViewGroup;
    import android.widget.LinearLayout;
    import android.widget.RelativeLayout;
    public class MainActivity extends Activity {
        public String msg;
        private View selected_item = null;
        private int offset_x = 0;
        private int offset_y = 0;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            findViewById(R.id.img).setOnTouchListener(new MyTouchListener());
            }
        }

    final class MyTouchListener implements View.OnTouchListener {
        public boolean onTouch(View view, MotionEvent motionEvent) {
            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                ClipData data = ClipData.newPlainText("", "");
                View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
                view.startDrag(data, shadowBuilder, view, 0);
                view.setVisibility(View.INVISIBLE);
                return true;
            } else {
                return false;
            }
        }
    }

    class MyDragListener implements View.OnDragListener {
        private LinearLayout.LayoutParams layoutParams;

        @Override
        public boolean onDrag(View v, DragEvent event) {
            int action = event.getAction();
            switch (event.getAction()) {
                case DragEvent.ACTION_DRAG_STARTED:
                    break;
                case DragEvent.ACTION_DRAG_ENTERED:
                    break;
                case DragEvent.ACTION_DRAG_EXITED:
                    break;
                case DragEvent.ACTION_DROP:

                    View view = (View) event.getLocalState();
                    ViewGroup owner = (ViewGroup) view.getParent();
                    owner.removeView(view);
                    LinearLayout container = (LinearLayout) v;
                    container.addView(view);
                    view.setVisibility(View.VISIBLE);
                    break;
                case DragEvent.ACTION_DRAG_ENDED:
                    break;
                default:
                    break;
            }
            return true;
        }
    }

以下是xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
        android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

        <LinearLayout
            android:layout_width="400dp"
            android:layout_height="400dp">

            <ImageView
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:id="@ id/img"
                android:src="@drawable/abc"/>

        </LinearLayout>

    </RelativeLayout>

最佳答案:

1 个答案:

答案 0 :(得分:0)

你写这个课

非常好
  

MyDragListener

但是你没有将它分配给任何视图,所以基本上它从未调用过onDrag()方法。您的布局中没有接受任何拖动事件的任何视图,因为任何未注册到这些事件或实现的视图都是onDragEvent()

您需要添加以下内容:

findViewById(R.id.your_desire_view_that_accept_events).setOnDragListener(new MyDragListener ());
本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复