拖放给定容器

时间:2017-01-10 20:00:29

标签: android drag-and-drop

我试图拖放一个容器的ImageView(例如LinearLayout),屏幕上还有其他组件,并希望确定该特定的容器。如果在其他地方完成掉落,那么ImageView'应该回到原产地。

我有这段代码:

import android.content.ClipDescription;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import br.com.atlantic.albumshow.R;

public class Main2Activity extends AppCompatActivity {


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

        ImageView imageView = (ImageView) findViewById(R.id.img);
        imageView.setOnTouchListener(new MyTouchListener());
        imageView.setOnDragListener(new View.OnDragListener() {

            @Override
            public boolean onDrag(View v, DragEvent event) {
                int action = event.getAction();

                switch (action) {
                    case DragEvent.ACTION_DRAG_STARTED:
                        if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
                            return (true);
                        }
                        return (false);
                    case DragEvent.ACTION_DRAG_ENTERED:
                        break;
                    case DragEvent.ACTION_DRAG_LOCATION:
                        break;
                    case DragEvent.ACTION_DRAG_EXITED:
                        break;
                    case DragEvent.ACTION_DROP:
                        break;
                    case DragEvent.ACTION_DRAG_ENDED:
                        View view = (View) event.getLocalState();
                        if (v instanceof LinearLayout) {

                            ViewGroup owner = (ViewGroup) view.getParent();
                            owner.removeView(view);
                            LinearLayout container = (LinearLayout) v;
                            container.addView(view);

                        }
                        view.setVisibility(View.VISIBLE);
                        break;
                }

                return true;
            }

        });
    }

    float x, y = 0.0f;
    boolean moving = false;

    private class MyTouchListener implements View.OnTouchListener {
        public boolean onTouch(final View view, MotionEvent motionEvent) {

            switch (motionEvent.getAction()) {

                case MotionEvent.ACTION_MOVE:
                    if(moving) {
                        x = motionEvent.getRawX() - view.getWidth() / 2;
                        y = motionEvent.getRawY() - view.getHeight() * 3 / 2;
                        view.setX(x);
                        view.setY(y);
                    }
                    break;

                case MotionEvent.ACTION_DOWN:
                    moving = true;
                    break;

                case MotionEvent.ACTION_UP:
                    moving = false;
                    break;

            }

            return true;

        }

    }
}

我尝试使用使用DragShadowBuilder的各种examples,但只有在能够识别容器并且此放置位于不同位置时,其放置效果才会很好。简而言之,DragShadowBuilder无法删除已标识的容器,并删除拖动项目以移动到原始位置的动画。

如果不是容器(例如containerA),我可以使用DragShadowBuilder删除此动画。

我可以识别出我正在做下落的观点。

如果DragShadowBuilder无法使用,那么我可以使用该代码从drop中识别容器吗?

0 个答案:

没有答案