2012-05-23 16 views
5

Ho cercato di creare un ListView che posso ordinare utilizzando il trascinamento della selezione.Elenco di Android Drag and Drop per Honeycomb e ICS "Errore: segnalazione risultato di rilascio: falso"

Ho tentato di seguire la guida Android here e qualche codice sorgente fornito su Git oltre here. Inoltre, non voglio utilizzare l'esempio di app Music mentre sto cercando di utilizzare i nuovi strumenti forniti in Honeycomb e versioni successive.

Finora sono riuscito a creare l'elenco e posso trascinare gli elementi. Sfortunatamente quando rilascio l'elemento nell'elenco, viene visualizzato il seguente errore:

"I/ViewRoot (22739): Risultato di generazione del rapporto: falso".

Ho il sospetto che il mio drop listener non sia stato creato sull'elemento giusto e che quindi il drop non venga mai chiamato. Ecco alcuni codici sorgente, grazie mille per il tuo aiuto.

XML per 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> 

mio listView: Non ho ancora stato in grado di entrare nella manifestazione "ACTION_DROP" in modo che il codice non è testato. Solo qualcosa su cui stavo lavorando. La mia domanda principale è che non prendo mai 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; 
    } 
} 

Grazie mille per il vostro aiuto!

UPDATE:

non riuscivo a capire perché. Ma quando ho cambiato passare caso a questo, tutto sembrava funzionare:

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; 

      } 

risposta

5

vostro aggiornamento risolto il problema, perché si deve tornare true da onDrag quando si arriva DragEvent.ACTION_DRAG_STARTED al fine di continuare a ricevere gli eventi di trascinamento a quella ascoltatore. Nell'aggiornamento si restituisce true per questo caso, quindi si continua a ricevere gli eventi di trascinamento e la logica di rilascio funziona correttamente.

Se non si restituisce true per il caso DragEvent.ACTION_DRAG_STARTED, il listener non riceverà altri eventi tranne DragEvent.ACTION_DRAG_ENDED.