简体   繁体   中英

How to drag an item from a listview and drop it on a outside textView?

I am trying to implement a Drag and Drop example of android . After long pressing on an item I am dragging it to a target level and dropping there. I am successful some way. But I want something different . I have done something like this.. 在此输入图像描述

But I want to add all the items to a listView and I shall pic up one item after long pressing on listview , then I shall drag the item to my target level and drop it there. I want something like this .. 在此输入图像描述

My Code is given below...

public class MainActivity extends Activity {



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






    findViewById(R.id.mango).setOnLongClickListener(longListen);
    findViewById(R.id.orange).setOnLongClickListener(longListen);
    findViewById(R.id.apple).setOnLongClickListener(longListen);
    findViewById(R.id.banana).setOnLongClickListener(longListen);

    findViewById(R.id.drop_here).setOnDragListener(DropListener);

    listview.setOnItemLongClickListener(new OnItemLongClickListener() {

        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int pos, long id) {
            // TODO Auto-generated method stub

            Log.i("long clicked","pos"+" "+pos);

            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.main, menu);
    return true;
}

OnLongClickListener longListen = new OnLongClickListener() {

    @Override
    public boolean onLongClick(View v) {

        DragShadow dragShadow = new DragShadow(v);

        ClipData data = ClipData.newPlainText("", "");

        v.startDrag(data, dragShadow, v, 0);

        return false;
    }
};

private class DragShadow extends View.DragShadowBuilder {

    ColorDrawable greyBox;

    public DragShadow(View view) {
        super(view);
        // TODO Auto-generated constructor stub
        greyBox = new ColorDrawable(Color.LTGRAY);
    }

    @Override
    public void onDrawShadow(Canvas canvas) {
        // TODO Auto-generated method stub
        // super.onDrawShadow(canvas);

        greyBox.draw(canvas);
    }

    @Override
    public void onProvideShadowMetrics(Point shadowSize,
            Point shadowTouchPoint) {
        // TODO Auto-generated method stub
        // super.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
        View v = getView();

        int height = (int) v.getHeight() / 2;
        int width = (int) v.getWidth() / 2;

        greyBox.setBounds(0, 0, width, height);
        shadowSize.set(width, height);

        shadowTouchPoint.set(width / 2, height / 2);

    }


}

OnDragListener DropListener = new View.OnDragListener() {

    @Override
    public boolean onDrag(View v, DragEvent event) {
        // TODO Auto-generated method stub

        int dragEvent = event.getAction();

        switch (dragEvent) {
        case DragEvent.ACTION_DRAG_ENTERED:
            Log.i("Drag ", "Entered");
            break;
        case DragEvent.ACTION_DRAG_EXITED:
            Log.i("Drag ", "Exit");
            break;
        case DragEvent.ACTION_DROP:
            Log.i("Drag ", "Drop");

            TextView target = (TextView) v;
            TextView dragg = (TextView) event.getLocalState();
            target.setText(dragg.getText());
            break;
        default:
            break;
        }

        return true;
    }
};

}

I have tried some tutorials , but I am unable to get solution from there . Android List View Drag and Drop sort

I have found the solution. It is easy , Just have to call startDrag method inside setOnItemClickListener.

listview.setOnItemLongClickListener(new OnItemLongClickListener() {

        public boolean onItemLongClick(AdapterView<?> arg0, View v,
                int pos, long id) {
            // TODO Auto-generated method stub


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

There is nice tutorial here http://www.vogella.com/articles/AndroidDragAndDrop/article.html by Lars Vogel. But it will work only from Android 4 and above(after ICS).

If that doesnt give you a solution try adding a simple onTouchListener to drag an object and see if the object while dragging falls within the bounds of the View you want to drag into (a textview in your case)

A simple Drag ontouchlistener implementation can be found here : https://stackoverflow.com/a/9398861/2138983

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