How to drag an item from a list and drop it on an external textView?

I am trying to implement an android drag and drop example. After a long press on an element, I drag it to the target level and drop it there. I am somehow successful. But I want something else. I did something like this .. enter image description here

But I want to add all the elements to the View list, and after a long click on the listview, I will lay out one element and then drag the element to its target level and drop it. I want something like this .. enter image description here

My code is 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 tried some tutorials, but I can't get a solution from there. View Android list in drag list

+6
source share
2 answers

I have found a solution. It's simple, just need to call the 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; } }); 
+7
source

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

If this does not give you a solution, try adding a simple onTouchListener to drag the object and see if the object falls into the View frames that you want to drag while dragging (text view in your case)

A simple implementation of Drag ontouchlistener can be found here: fooobar.com/questions/46281 / ...

0
source

Source: https://habr.com/ru/post/948607/


All Articles