android - 在android中拖放,掉落问题

标签 android drag-and-drop

我是 android 新手,在 android 中拖放时遇到问题。图像向右拖动,但是当我放下它们时,它们显示出问题。就像 image1 放在它的位置很好但是当 image2 被放下时它也会显示 image1 并且当 image3 被放下时它会显示 image1。

我的主要 Activity 代码是:

    public class Level1 extends Activity {

public boolean checkState = false;
ImageView car1,car2,car3,car4,car5,car6;
private boolean dragging = false;
private boolean dragging1 = false;
private boolean dragging2 = false;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_level1);
    car1 = (ImageView) findViewById(R.id.car4);
    car2 = (ImageView) findViewById(R.id.car5);
    car3 = (ImageView) findViewById(R.id.car6);
    car4 = (ImageView) findViewById(R.id.car7);
    car5 = (ImageView) findViewById(R.id.car8);
    car6 = (ImageView) findViewById(R.id.car9);
    findViewById(R.id.car4).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.car6).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.car8).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.lcar4).setOnDragListener(new MyDragListener());
    findViewById(R.id.lcar5).setOnDragListener(new MyDragListener());
    findViewById(R.id.lcar6).setOnDragListener(new MyDragListener());
    findViewById(R.id.lcar7).setOnDragListener(new MyDragListener());
    findViewById(R.id.lcar8).setOnDragListener(new MyDragListener());
    findViewById(R.id.lcar9).setOnDragListener(new MyDragListener());
}

private final class MyTouchListener implements OnTouchListener{

    @SuppressLint("NewApi")
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        // TODO Auto-generated method stub
        if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){

            ClipData data = ClipData.newPlainText("","");
            DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
            view.startDrag(data, shadowBuilder, view, 0);
            view.setVisibility(View.INVISIBLE);
            if(view == findViewById(R.id.car4)){
            dragging = true;
            }
            else if(view == findViewById(R.id.car6)){
                dragging1 = true;
            }
            else if(view == findViewById(R.id.car8)){
                dragging2 = true;
            }
          }
            return false;
    }

}
@SuppressLint("NewApi")
class MyDragListener implements OnDragListener{

    @Override
    public boolean onDrag(View v, DragEvent event) {
        // TODO Auto-generated method stub
        //int action = event.getAction();
        switch (event.getAction()) {
          case DragEvent.ACTION_DRAG_STARTED:
            // do nothing
            break;
          case DragEvent.ACTION_DRAG_ENTERED:
            //v.setBackgroundDrawable(enterShape);
            break;
          case DragEvent.ACTION_DRAG_EXITED:
            //v.setBackgroundDrawable(normalShape);
            break;
          case DragEvent.ACTION_DROP:
            // Dropped, reassign View to ViewGroup
              if(dragging){
              if(v == findViewById(R.id.lcar5)){
            View view1 = (View) event.getLocalState();
            ViewGroup owner1 = (ViewGroup) view1.getParent();
            owner1.removeView(view1);
            LinearLayout container1 = (LinearLayout) v;
            container1.addView(view1);
            view1.setVisibility(View.INVISIBLE);
            car2.setImageResource(R.id.car4);
              dragging = false;
              dragging1 = false;
              dragging2 = false;
              }
              }
              if(dragging1){
                  if(v == findViewById(R.id.lcar7)){
                    View view2 = (View) event.getLocalState();
                    ViewGroup owner2 = (ViewGroup) view2.getParent();
                    owner2.removeView(view2);
                    LinearLayout container2 = (LinearLayout) v;
                    container2.addView(view2);
                    view2.setVisibility(View.INVISIBLE);
                    car4.setImageResource(R.id.car6);
                      dragging = false;
                      dragging1 = false;
                      dragging2 = false;
                      }
              }
              if(dragging2){
                  if(v == findViewById(R.id.lcar9)){
                    View view3 = (View) event.getLocalState();
                    ViewGroup owner3 = (ViewGroup) view3.getParent();
                    owner3.removeView(view3);
                    LinearLayout container3 = (LinearLayout) v;
                    container3.addView(view3);
                    view3.setVisibility(View.INVISIBLE);
                    car6.setImageResource(R.id.car4);
                      dragging = false;
                      dragging1 = false;
                      dragging2 = false;
                      }
              }
              else{
                    View view = (View) event.getLocalState();
                    view.setVisibility(View.VISIBLE);
                    break;
                }

              break;
          case DragEvent.ACTION_DRAG_ENDED:
            //v.setBackgroundDrawable(normalShape);
              break;
          default:
            break;
          }
          return true;
        }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.level1, menu);
    return true;
}

xml文件如下:

    <LinearLayout 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"
tools:context=".Level1"
android:orientation="horizontal"
android:weightSum="4" >
<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_weight="3"
    android:background="#800080"
    android:weightSum="3">
    <LinearLayout 
        android:id="@+id/lcar4"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginTop="5dp">
    <ImageView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/car4"
        android:id="@+id/car4"/>
    </LinearLayout>
    <LinearLayout 
        android:id="@+id/lcar6"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginTop="5dp">
    <ImageView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/car6"
        android:id="@+id/car6"/>
    </LinearLayout>
    <LinearLayout 
        android:id="@+id/lcar8"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginTop="5dp">
    <ImageView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/car8"
        android:id="@+id/car8"/>
    </LinearLayout>
</LinearLayout>

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:layout_weight="1"
    android:background="#4c8bff"
    android:weightSum="2">


    <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_weight="1"
    android:id="@+id/lcar5">

        <ImageView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/car5"
        android:id="@+id/car5"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"/>

    </LinearLayout>
    <LinearLayout 

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_weight="1"
    android:weightSum="2">
        <LinearLayout 
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/lcar7">
        <ImageView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/car7"
        android:id="@+id/car7"
        android:layout_marginTop="5dp"/>
        </LinearLayout>
        <LinearLayout 
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/lcar9">
        <ImageView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/car9"
        android:id="@+id/car9"
        android:layout_marginTop="5dp"/>
        </LinearLayout>
        </LinearLayout>
</LinearLayout>

请帮助我。我深陷其中。

最佳答案

试试这个,我已经更改了你代码中的几行

公共(public)类 Level1 扩展 Activity {

public boolean checkState = false;
ImageView car1, car2, car3, car4, car5, car6;
private boolean dragging = false;
private boolean dragging1 = false;
private boolean dragging2 = false;

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_level1);
    car1 = (ImageView) findViewById(R.id.car4);
    car2 = (ImageView) findViewById(R.id.car5);
    car3 = (ImageView) findViewById(R.id.car6);
    car4 = (ImageView) findViewById(R.id.car7);
    car5 = (ImageView) findViewById(R.id.car8);
    car6 = (ImageView) findViewById(R.id.car9);

    findViewById(R.id.car4).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.car6).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.car8).setOnTouchListener(new MyTouchListener());

    findViewById(R.id.lcar4).setOnDragListener(new MyDragListener());
    findViewById(R.id.lcar5).setOnDragListener(new MyDragListener());
    findViewById(R.id.lcar6).setOnDragListener(new MyDragListener());
    findViewById(R.id.lcar7).setOnDragListener(new MyDragListener());
    findViewById(R.id.lcar8).setOnDragListener(new MyDragListener());
    findViewById(R.id.lcar9).setOnDragListener(new MyDragListener());
}

private final class MyTouchListener implements OnTouchListener {

    @SuppressLint("NewApi")
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        // TODO Auto-generated method stub
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {

            ClipData data = ClipData.newPlainText("", "");
            DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(
                    view);
            view.startDrag(data, shadowBuilder, view, 0);
            view.setVisibility(View.INVISIBLE);
            if (view == findViewById(R.id.car4)) {
                dragging = true;
                dragging1 = false;
                dragging2 = false;
            } else if (view == findViewById(R.id.car6)) {
                dragging1 = true;
                dragging = false;
                dragging2 = false;
            } else if (view == findViewById(R.id.car8)) {
                dragging2 = true;
                dragging1 = false;
                dragging = false;
            }
            return true;
        }
        return false;
    }

}

@SuppressLint("NewApi")
class MyDragListener implements OnDragListener {

    @Override
    public boolean onDrag(View v, DragEvent event) {
        // TODO Auto-generated method stub
        // int action = event.getAction();
        switch (event.getAction()) {
        case DragEvent.ACTION_DRAG_STARTED:
            // do nothing
            break;
        case DragEvent.ACTION_DRAG_ENTERED:
            // v.setBackgroundDrawable(enterShape);
            break;
        case DragEvent.ACTION_DRAG_EXITED:
            // v.setBackgroundDrawable(normalShape);
            break;
        case DragEvent.ACTION_DROP:
            // Dropped, reassign View to ViewGroup
            if (dragging) {
                if (v == findViewById(R.id.lcar5)) {
                    Toast.makeText(getBaseContext(), "at green ",
                            Toast.LENGTH_SHORT).show();
                    View view1 = (View) event.getLocalState();
                    ViewGroup owner1 = (ViewGroup) view1.getParent();
                    owner1.removeView(view1);
                    LinearLayout container1 = (LinearLayout) v;
                    container1.addView(view1);
                    view1.setVisibility(View.INVISIBLE);
                    car2.setImageResource(R.id.car4);
                    break;
                } else {
                    View view = (View) event.getLocalState();
                    view.setVisibility(View.VISIBLE);
                    break;
                }
            }
            if (dragging1) {
                if (v == findViewById(R.id.lcar7)) {
                    Toast.makeText(getBaseContext(), "at blue ",
                            Toast.LENGTH_SHORT).show();
                    View view2 = (View) event.getLocalState();
                    ViewGroup owner2 = (ViewGroup) view2.getParent();
                    owner2.removeView(view2);
                    LinearLayout container2 = (LinearLayout) v;
                    container2.addView(view2);
                    view2.setVisibility(View.INVISIBLE);
                    car4.setImageResource(R.id.car6);
                    break;
                } else {
                    View view = (View) event.getLocalState();
                    view.setVisibility(View.VISIBLE);
                    break;
                }
            }
            if (dragging2) {
                if (v == findViewById(R.id.lcar9)) {
                    Toast.makeText(getBaseContext(), "at red ",
                            Toast.LENGTH_SHORT).show();
                    View view3 = (View) event.getLocalState();
                    ViewGroup owner3 = (ViewGroup) view3.getParent();
                    owner3.removeView(view3);
                    LinearLayout container3 = (LinearLayout) v;
                    container3.addView(view3);
                    view3.setVisibility(View.INVISIBLE);
                    car6.setImageResource(R.id.car4);
                    break;
                } else {
                    View view = (View) event.getLocalState();
                    view.setVisibility(View.VISIBLE);
                    break;
                }

            }

            break;
        case DragEvent.ACTION_DRAG_ENDED:
            // v.setBackgroundDrawable(normalShape);
            break;
        default:
            break;
        }
        return true;
    }
}

关于android - 在android中拖放,掉落问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23651771/

相关文章:

android - 使用 APK 拆分进行发布但不使用调试构建类型

android - 使用图形 API 在 Facebook 墙上发帖不起作用

tomcat - 使用 Dropzone.js 和 Tapestry 4 进行拖放

android - 如何在 Android 应用程序中对 imageView 进行分层?

android - 向上导航未启动父 Activity

android - 模型中列表的值已更新,但未反射(reflect)在可组合函数上

javascript - React Native - 如何更改气泡样式

c# - 在同一个 ListView 中拖放项目

java - 拖放 Netbeans 按钮是否隐藏?

c# - 如何在对话框打开时禁用拖放