Frage

I am new in android and I am having problem in drag and drop in android. The images are dragged right but when I drop them, they show problems. Like image1 dropped good in its position but when image2 is dropped it also show image1 with it and when image3 is dropped it shows image1.

My code of main activity is:

    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;
}

}

The xml file is given below:

    <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>

Kindly help me. I am stucked in it.

War es hilfreich?

Lösung

Try this i`ve changed few of lines at your code

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;
                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;
    }
}

}

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top