Drag and Drop similar to the android shortcut screen

Asked

Viewed 156 times

2

I’m using Android 4.0 to develop, I need to develop a drag and drop in my app and I will like to do as the shortcuts screen, a table 4x4 on the screen. With this the elements will have 16 defined places and whenever drop one on top of the other it repositions where it takes place.

Thank you in advance for your attention!

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:id="@+id/topleft"
    android:layout_width="360dp"
    android:layout_height="300dp"
    android:layout_column="0"
    android:layout_row="0"
    android:layout_columnSpan="2"
    android:background="#9864" >

    <ImageView
        android:id="@+id/myimage1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="0"
        android:layout_row="0"
        android:src="@drawable/busca_crt" />

    <ImageView
    android:id="@+id/myimage2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_column="0"
    android:layout_row="0"
    android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/myimage3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="0"
        android:layout_row="0"
        android:src="@drawable/ic_launcher" />
</LinearLayout>

public class draganddrop extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_draganddrop);
    findViewById(R.id.myimage1).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.myimage2).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.myimage3).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.topleft).setOnDragListener(new MyDragListener());
}

private final class MyTouchListener implements View.OnTouchListener{
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            ClipData data = ClipData.newPlainText("", "");
            View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
            view.startDrag(data, shadowBuilder, view, 0);
            view.setVisibility(View.INVISIBLE);
            return true;
        } else {
            return false;
        }
    }
}

class MyDragListener implements View.OnDragListener {

    @Override
    public boolean onDrag(View v, DragEvent event) {
        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
                View view = (View) event.getLocalState();
                ViewGroup owner = (ViewGroup) view.getParent();
                owner.removeView(view);
                LinearLayout container = (LinearLayout) v;
                container.addView(view);
                view.setVisibility(View.VISIBLE);
                break;
            case DragEvent.ACTION_DRAG_ENDED:
               // v.setBackgroundDrawable(normalShape);
            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.draganddrop, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

I got it but I don’t know if I’m on the right track!

  • 1

    Of more details than you are having problems, and post what you have already done. So it is simpler to understand your problem to be able to help you.

  • I believe that on this link you can solve this problem. http://www.vogella.com/tutorials/AndroidDragAndDrop/article.html

  • I believe that this library can help!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.