在android中拖放,丢弃的问题

时间:2014-05-14 10:15:58

标签: android drag-and-drop

我是android的新手,我在android中遇到拖放问题。图像被拖动,但是当我放下它们时,它们会出现问题。就像image1在其位置下降得很好但是当image2被丢弃时它也会显示image1,当image3被丢弃时它会显示image1。

我的主要活动代码是:

    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>

请帮助我。我被困在里面。

1 个答案:

答案 0 :(得分:1)

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

公共类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;
    }
}

}