2012-05-23 7 views
5

He estado tratando de crear un ListView que puedo ordenar utilizando arrastrar y soltar.Android ListView Arrastrar y soltar para Honeycomb e ICS "Error: Informes de caída de resultados: falso"

He intentado seguir la guía de Android here y algún código fuente proporcionado en Git sobre here. Además, no quiero usar el ejemplo de la aplicación de Música porque estoy tratando de usar las nuevas herramientas dadas en Honeycomb y más.

Hasta ahora he tenido éxito en la creación de la lista y puedo arrastrar los artículos. Lamentablemente, cuando dejo caer el elemento en la lista, aparece el siguiente error:

"I/ViewRoot (22739): informe de caída del resultado: falso".

Tengo la sospecha de que mi detector de gotas no se ha creado en el elemento correcto y, por lo tanto, nunca se llama a la gota. Aquí hay un código fuente, muchas gracias por su ayuda.

XML correspondiente a la lista

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/dropTarget" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1"> 
    <ListView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:id="@android:id/list" > 
    </ListView> 
</LinearLayout> 

Mi Listview: todavía no he sido capaz de entrar en evento "ACTION_DROP" para que el código no se ha probado. Solo algo en lo que estaba trabajando. Mi pregunta principal es que nunca me meto en ACTION_DROP.

public class procedureListView extends ListActivity { 
    private ListView mListView = null; 
    private ArrayAdapter<String> mArrayAdapter = null; 
    private View layoutDropArea = null; 

    public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.list); 

      String[] countries = getResources().getStringArray(R.array.arrayOfStuff); 
      mArrayAdapter = new ArrayAdapter<String>(this, R.layout.list_item, countries); 
      setListAdapter(mArrayAdapter); 

      mListView = getListView(); 
      mListView.setTextFilterEnabled(true); 

      layoutDropArea = findViewById(R.id.dropTarget); 

      setupDragDrop(); 
    } 
    /** 
    * Setup what to do when we drag list items 
    */ 
    public void setupDragDrop(){ 
     mListView.setOnItemLongClickListener(new OnItemLongClickListener() { 
      public boolean onItemLongClick(AdapterView<?> arg0, View v, int position, long arg3){ 
       String value = (String) ((TextView) v).getText(); 
       ClipData data = ClipData.newPlainText("procedure", value); 
       v.startDrag(data, new mDragShadowBuilder(v), null, 0);   
       return true; 
      } 
     }); 
     myDragListener mDragListener = new myDragListener(); 
     //mListView.setOnDragListener(mDragListener); 

     layoutDropArea.setOnDragListener(mDragListener); 



    } 
    protected class myDragListener implements OnDragListener{ 

     public boolean onDrag(View v, DragEvent event) { 
      final int action = event.getAction(); 
      switch (action) { 
       case DragEvent.ACTION_DRAG_ENTERED: 
        v.setBackgroundColor(Color.GRAY); 
        break; 
       case DragEvent.ACTION_DRAG_EXITED: 
        v.setBackgroundColor(Color.TRANSPARENT); 
        break; 
       case DragEvent.ACTION_DRAG_STARTED: 
        break; 
       case DragEvent.ACTION_DRAG_LOCATION: 
        v.setVisibility(View.VISIBLE); 
       // return processDragStarted(event); 
       case DragEvent.ACTION_DROP: 
        v.setBackgroundColor(Color.TRANSPARENT); 
        int newPosition = mListView.getPositionForView(v); 
        if (newPosition != ListView.INVALID_POSITION) 
         return processDrop(event, newPosition); 
        else 
         return false; 
      } 
      return false; 
     } 

    } 

    private boolean processDrop(DragEvent event, int newPosition) { 
     ClipData data = event.getClipData(); 
     if (data != null) { 
      if (data.getItemCount() > 0) { 
       Item item = data.getItemAt(0); 
       String value = item.toString(); 
       updateViewsAfterDropComplete(value, newPosition); 
       return true; 
      } 
     } 
     return false; 
    } 
    private void updateViewsAfterDropComplete(String listItem, int index) { 
     Log.d("InsertItem", "Position: "+ index); 
     mArrayAdapter.insert(listItem, index); 
     mArrayAdapter.notifyDataSetChanged(); 
    } 
    private boolean processDragStarted(DragEvent event) { 
     ClipDescription clipDesc = event.getClipDescription(); 
     if (clipDesc != null) { 
      return clipDesc.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN); 
     } 
     return false; 
    } 
} 

¡Muchas gracias por su ayuda!

ACTUALIZACIÓN:

No podía entender por qué. Pero cuando cambié cambiar a este caso, parece que ha funcionado:

switch (action) { 
       case DragEvent.ACTION_DRAG_ENTERED: 
        //v.setBackgroundColor(Color.GRAY); 
        return false; 

       case DragEvent.ACTION_DRAG_EXITED: 
        //v.setBackgroundColor(Color.TRANSPARENT); 
        return true; 

       case DragEvent.ACTION_DRAG_STARTED: 
        return true; 

       case DragEvent.ACTION_DRAG_LOCATION: 
        //v.setVisibility(View.VISIBLE); 
        return false; 
       // return processDragStarted(event); 
       case DragEvent.ACTION_DROP: 
        v.setBackgroundColor(Color.TRANSPARENT); 
        int newPosition = mListView.pointToPosition((int)(event.getX()),(int) event.getY()); 
        Log.d("Position", Integer.toString(newPosition)); 
        if (newPosition != ListView.INVALID_POSITION) 
         return processDrop(event, newPosition); 
        else 
         return false; 
       default: 
        return true; 

      } 

Respuesta

5

Su actualización solucionado el problema porque hay que volver true de onDrag cuando llegue DragEvent.ACTION_DRAG_STARTED con el fin de continuar recibiendo los eventos de arrastre a ese oyente. En su actualización, devuelve true para este caso, por lo que continúa recibiendo eventos de arrastre y la lógica de soltar funciona correctamente.

Si no devuelve true para la caja DragEvent.ACTION_DRAG_STARTED, su oyente no obtendrá ningún otro evento excepto DragEvent.ACTION_DRAG_ENDED.

Cuestiones relacionadas