简体   繁体   中英

drag and drop images and shift their position in android

i have a layout and i have placed image view and text view on it text view position are fixed on the layout but image view are not fixed when i drag and image and drop at some other place the position of all the images automatically changed .

example : i have 4 images and i drag 3 image and drop at the place of 1 image then 1 image will placed at 2nd position and second image at third and so on.any one has code please provide i have no code for this i just create a simple layout that contains image view and text view

thanks in advance.

here is my answer

public class MainActivity extends Activity {

private ImageView option1, option2, choice1, choice2;
TextView tv1, tv2;
int mScrollDistance;
MyScrollView myScrollView;
LinearLayout layout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tv1 = (TextView) findViewById(R.id.tv1);
    tv2 = (TextView) findViewById(R.id.tv2);

    tv1.setTag("0");
    tv2.setTag("0");

    // views to drag
    option1 = (ImageView) findViewById(R.id.option_1);
    option2 = (ImageView) findViewById(R.id.option_2);

    // views to drop onto
    choice1 = (ImageView) findViewById(R.id.choice_1);
    choice2 = (ImageView) findViewById(R.id.choice_2);

    // set touch listeners

    option1.setOnLongClickListener(new ChoiceTouchListener());
    option2.setOnLongClickListener(new ChoiceTouchListener());

    option1.setOnDragListener(new ChoiceDragListener());
    option2.setOnDragListener(new ChoiceDragListener());

    option1.setTag("1");
    option2.setTag("1");
    // set drag listeners
    choice1.setOnLongClickListener(new ChoiceTouchListener());
    choice2.setOnLongClickListener(new ChoiceTouchListener());

    choice1.setOnDragListener(new ChoiceDragListener());
    choice2.setOnDragListener(new ChoiceDragListener());

    choice1.setTag("1");
    choice2.setTag("1");
    tv1.setOnDragListener(new ChoiceDragListener());
    tv2.setOnDragListener(new ChoiceDragListener());

    myScrollView = (MyScrollView) findViewById(R.id.scroll);

    myScrollView
            .setOnScrollViewListener(new MyScrollView.OnScrollViewListener(){
                @Override
                public void onScrollChanged1(OnScrollViewListener listener) {
                    // TODO Auto-generated method stub
                    mScrollDistance = myScrollView.getScrollY();
                }
            });

}

private final class ChoiceTouchListener implements OnLongClickListener {

    @SuppressLint("NewApi")
    public boolean onTouch(View view, MotionEvent motionEvent) {

        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            // setup drag

            ClipData data = ClipData.newPlainText("", "");
            DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(
                    view);
            // start dragging the item touch ed

            view.startDrag(data, shadowBuilder, view, 0);
        }
        return true;
    }

    @Override
    public boolean onLongClick(View v) {
        // TODO Auto-generated method stub
        ClipData data = ClipData.newPlainText("", "");
        DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
        // start dragging the item touch ed

        v.startDrag(data, shadowBuilder, v, 0);
        return true;
    }

}

private class ChoiceDragListener implements OnDragListener {

    @SuppressWarnings("deprecation")
    @Override
    public boolean onDrag(View v, DragEvent event) {
        // handle drag events
        switch (event.getAction()) {
        case DragEvent.ACTION_DRAG_STARTED:
            // no action necessary
            System.out.println("Started");
            break;
        case DragEvent.ACTION_DRAG_ENTERED:

            System.out.println("ENtered");
            // no action necessary
            break;
        case DragEvent.ACTION_DRAG_EXITED:
            // no action necessary
            System.out.println("Exited");
            break;

        case DragEvent.ACTION_DRAG_LOCATION:

            int y = Math.round(v.getY());// + Math.round(event.getY());
            int translatedY = y - mScrollDistance;
            // System.out.printf("translated", "" + translatedY + " "
            // + mScrollDistance + " " + y);
            int threshold = 50;
            // make a scrolling up due the y has passed the threshold
            if (translatedY < 200) {
                // make a scroll up by 30 px
                myScrollView.smoothScrollBy(0, -30);
            }
            // make a autoscrolling down due y has passed the 500 px border
            if (translatedY + threshold > 500) {
                // make a scroll down by 30 px
                myScrollView.smoothScrollBy(0, 30);
            }
            break;

        case DragEvent.ACTION_DROP:

            View view = (View) event.getLocalState();

            if (((String) v.getTag()).equals("0")) {

            } else if (((String) v.getTag()).equals("1")) {
                ImageView dropTarget = (ImageView) v;

                // view being dragged and dropped

                ImageView dropped = (ImageView) view;

                Drawable temp_img = dropped.getBackground();

                dropped.setBackgroundDrawable(dropTarget.getBackground());

                dropTarget.setBackgroundDrawable(temp_img);
            }
            break;
        case DragEvent.ACTION_DRAG_ENDED:
            // no action necessary
            break;
        default:
            break;
        }
        return true;
    }
}

} here is the second class used

public class MyScrollView extends ScrollView {

public OnScrollViewListener mListener;

public MyScrollView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
    // TODO Auto-generated method stub
    super.onScrollChanged(l, t, oldl, oldt);
    if (mListener != null) {
        mListener.onScrollChanged1(mListener);
    }
}

public void setOnScrollViewListener(OnScrollViewListener listener) {
    mListener = listener;
}

public static interface OnScrollViewListener {
    public void onScrollChanged1(OnScrollViewListener listener);
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM