Cómo arrastrar un elemento de un listview y dejarlo en un textView exterior?
Estoy tratando de implementar un ejemplo de arrastrar y soltar de android. Después de presionar un ítem largo, lo estoy arrastrando hasta un nivel objetivo y cayendo ahí. Tengo éxito de alguna manera. Pero quiero algo diferente. He hecho algo como esto
Pero quiero agregar todos los elementos a una lista y voy a pic de un elemento después de presionar largo en listview, entonces voy a arrastrar el elemento a mi nivel de destino y dejarlo allí. Quiero algo como esto
- Android: realizar arrastrar y soltar mediante programación
- Arrastrar y soltar RecyclerView que se rellena desde SQLiteDatabase
- Android scroll mientras arrastra y suelta
- Drag'n'Drop ConcurentModificationException
- RecyclerView in DialogFragment -> arrastre y suelte (swap) retrasos de animación
Mi código se da a continuación …
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; } }; }
He intentado algunos tutoriales, pero no puedo conseguir la solución de allí. Lista de Android Ver Arrastrar y soltar ordenar
- Arrastrar y soltar para poner elementos de List1 a List2
- Android - arrastrar y soltar - reorganizar la lista
- Jquery arrastrar caída cuelga o se queda bloqueado para el navegador android
- Cómo cancelar View.OnDragListener en android durante la operación de arrastrar?
- Cómo arrastrar el TextView al destino correcto otro TextView
- Programaticamente finaliza un Android DragEvent
- Android: Problema de arrastrar y soltar
- Arrastrar y soltar imágenes en android
He encontrado la solución. Es fácil, solo tienes que llamar al método startDrag dentro de 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; } });
Hay buen tutorial aquí http://www.vogella.com/articles/AndroidDragAndDrop/article.html por Lars Vogel. Pero funcionará sólo desde Android 4 y superior (después de ICS).
Si eso no te da una solución, trata de agregar un simple onTouchListener para arrastrar un objeto y ver si el objeto mientras arrastra cae dentro de los límites de la Vista en la que quieres arrastrar (una textview en tu caso)
Una sencilla implementación de Drag ontouchlistener se puede encontrar aquí: https://stackoverflow.com/a/9398861/2138983
- Cómo hacer una actividad de fondo transparente y difuminar el fondo
- El informe de error de google analytics sólo muestra la primera línea de stacktrace