title: Drag and drop in a list view
body: <p>I have little experience with android.</p>
  
  <p>I'm trying to make a drag+drop aware list view because various other solutions don't do what the client wants, and this project is likely going to have me making a lot of other "fun" draggy-droppy-swipey things like this for a while.</p>
  
  <p>The requirements are simple: User can drag a thing from a list out and into something else using the android drag+drop API (if you call it that). It works as long as said things aren't in a list view or something that intercepts long presses or whatever.</p>
  
  <p>Here's what isn't working so far:</p>
  
  <pre><code>package blah;
  
  import android.os.*;
  import android.app.*;
  import android.content.*;
  import android.util.*;
  import android.view.*;
  import android.view.View*;
  import android.widget.*;
  
  public class ListFragment extends Fragment
  {
      public static class ItemView extends TextView
      {
          public int position;
  
          public ItemView(Context context, AttributeSet attrs)
          {
              super(context,attrs);
          }
      }
  
      private class _Adapter extends BaseAdapter
      {
          private Activity _activity;
  
          @Override
          public int getCount()
          {
              // TEST: Make it go
              return 16;
          }
  
          @Override
          public Object getItem(int arg0)
          {
              return null;
          }
  
          @Override
          public long getItemId(int arg0)
          {
              return 0;
          }
  
          @Override
          public View getView(int position, View convert, ViewGroup parent)
          {
              LayoutInflater inflater = (LayoutInflater)this._activity.getSystemService(
                      Context.LAYOUT_INFLATER_SERVICE
                      );
  
              ItemView itemView = (ItemView)inflater.inflate(R.layout.list_item_view,parent,false);
  
               // TEST: Just display positions
              itemView.setText(Integer.toString(position));
  
              itemView.setOnClickListener(new _ItemClickListener());
  
              itemView.setOnDragListener(new _ItemDragListener());
  
              itemView.position = position;
  
              return itemView;
          };
  
          public _Adapter(Activity activity)
          {
              this._activity = activity;
          }
      }
  
      private class _ItemDragListener implements OnDragListener
      {
          @Override
          public boolean onDrag(View view, DragEvent event)
          {
              ItemView targetItemView = (ItemView)view;
  
              int action = event.getAction();
  
              switch(action){
              case DragEvent.ACTION_DROP:
                  {
                      // not really sure which is which here, since nothing works
                      ItemView sourceItemView = (ItemView)event.getLocalState();
  
                      Log.d("DERP",
                              "DROPPED " + Integer.toString(sourceItemView.position) +
                              " ONTO " + Integer.toString(targetItemView.position)
                              );
                  }break;
              }
              return true;
          }
      }
  
      private class _ItemClickListener implements OnClickListener
      {
          @Override
          public void onClick(View view)
          {
              ItemView itemView = (ItemView)view;
              Log.d("DERP", Integer.toString(itemView.position) + " WAS CLICKED");
  
              // nothing happens here
              view.startDrag(
                  ClipData.newPlainText("",""),
                  new View.DragShadowBuilder(view),
                  view,
                  0
                  );
          }
  
          public _ItemClickListener()
          {
          }
      }
  
      private Activity _activity;
  
      @Override
      public void onAttach(Activity activity)
      {
          super.onAttach(activity);
          this._activity = activity;
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
      {
          ListView listView = (ListView)inflater.inflate(R.layout.list_fragment, container, false);
  
          listView.setAdapter(new _Adapter(this._activity));
  
          return listView;
      }
  }
  </code></pre>
  
  <p>My intuition tells me that making a drag+drop list should be simpler than this, given the "breadth" of features supported by the android UI. However, this is also Java, so I'm not expecting straightforward answers.</p>