android - 落在我松开手指的地方

标签 android drag-and-drop

我做了一个拖放系统,但是当我插入项目时,它看起来像重力在上面,当插入新项目时,它们保持在彼此之上,我如何放下我松开手指的地方?

项目在列表中,我是这样得到它们的:

public void criarListagem(List<croquiA> cro) {
        CroquiAdapter adapter = new CroquiAdapter(cro, this);
        listView.setAdapter(adapter);
        listView.setDivider(null);

        listView.setOnItemLongClickListener((parent, view, position, id) -> {
            arrayClass.p = arrayClass.pistasIds[position];
            ClipData data = ClipData.newPlainText("simple_text", "text");
            View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
            view.startDrag(data, shadowBuilder, view, 0);
            view.setVisibility(View.VISIBLE);

            newClick(arrayClass.p, l);

            if(l != 0){
                findViewById(R.id.container).setOnDragListener(new MyOnDragListener());
            }

            return false;
        });

MyOnDragListener:

class MyOnDragListener implements View.OnDragListener{

        public MyOnDragListener(){
            super();
        }

        @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;
                   }else{
                       return false;
                   }

                case DragEvent.ACTION_DRAG_ENTERED:

                    break;

                case DragEvent.ACTION_DRAG_LOCATION:
                    break;

                case DragEvent.ACTION_DRAG_EXITED:
                    break;

                case DragEvent.ACTION_DROP:

                    View view = (View) event.getLocalState();
                    RelativeLayout container = (RelativeLayout) v;

                    //Here I copy to the view and add an ImageView 
                    //instead of remove because it is an adapter, so it is 
                    //not possible to remove it

                    ImageView oldView = (ImageView) view;
                    ImageView newView = new ImageView(getApplicationContext());

                    newView.setImageBitmap(((BitmapDrawable) oldView.getDrawable()).getBitmap());

                    container.addView(newView);

                    break;

                case DragEvent.ACTION_DRAG_ENDED:
                    break;

            }

            return true;
        }

当我放下它时它看起来像这样:

enter image description here

因为我做了 2 滴,意识到一个图像在另一个图像之上,它们不是在我放开手指的地方创建的

最佳答案

在您的 MyOnDragListener 中,您需要指定将新 View 放入布局的位置。尝试以下操作:更改此行

container.addView(newView);

进入

RelativeLayout.LayoutParams params;
params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
params.leftMargin = (int) event.getX() - oldView.getWidth()/2;
params.topMargin = (int) event.getY() - oldView.getHeight()/2;

container.addView(newView, params);

关于android - 落在我松开手指的地方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49972178/

相关文章:

android - 如何确保我的通知声音在所有设备上可靠播放?

java - Android onClick 函数不起作用

java - 获取给定 java TimeZone 的国家/地区

c# - 在 ListView C# 中拖放

jquery - 在幻灯片中拖动图片

android - 在 Activity 之间共享域对象

具有垂直和水平笔划的 Android 手势叠加

excel - VBA将文件拖放到用户窗体中以获取文件名和路径

javascript - 启用/禁用可排序取决于元素组

graphics - 有谁知道拖放、可重新排序列表的低级(无框架)示例?