content: package com.mobeta.android.demodslv;
  
  import java.util.Arrays;
  import java.util.ArrayList;
  
  import android.support.v4.app.ListFragment;
  import android.app.Activity;
  import android.view.View;
  import android.view.ViewGroup;
  import android.view.LayoutInflater;
  import android.os.Bundle;
  import android.widget.ArrayAdapter;
  import android.widget.TextView;
  import com.mobeta.android.dslv.DragSortListView;
  import com.mobeta.android.dslv.DragSortController;
  
  
  public class DSLVFragment extends ListFragment {
  
      ArrayAdapter<String> adapter;
  
      private String[] array;
      private ArrayList<String> list;
  
      private DragSortListView.DropListener onDrop =
              new DragSortListView.DropListener() {
                  @Override
                  public void drop(int from, int to) {
                      if (from != to) {
                          String item = adapter.getItem(from);
                          adapter.remove(item);
                          adapter.insert(item, to);
                      }
                  }
              };
  
      private DragSortListView.RemoveListener onRemove = 
              new DragSortListView.RemoveListener() {
                  @Override
                  public void remove(int which) {
                      adapter.remove(adapter.getItem(which));
                  }
              };
  
      protected int getLayout() {
          // this DSLV xml declaration does not call for the use
          // of the default DragSortController; therefore,
          // DSLVFragment has a buildController() method.
          return R.layout.dslv_fragment_main;
      }
      
      /**
       * Return list item layout resource passed to the ArrayAdapter.
       */
      protected int getItemLayout() {
          /*if (removeMode == DragSortController.FLING_LEFT_REMOVE || removeMode == DragSortController.SLIDE_LEFT_REMOVE) {
              return R.layout.list_item_handle_right;
          } else */
      	if (removeMode == DragSortController.CLICK_REMOVE) {
              return R.layout.list_item_click_remove;
          } else {
              return R.layout.list_item_handle_left;
          }
      }
  
      private DragSortListView mDslv;
      private DragSortController mController;
  
      public int dragStartMode = DragSortController.ON_DOWN;
      public boolean removeEnabled = false;
      public int removeMode = DragSortController.FLING_REMOVE;
      public boolean sortEnabled = true;
      public boolean dragEnabled = true;
  
      public static DSLVFragment newInstance(int headers, int footers) {
          DSLVFragment f = new DSLVFragment();
  
          Bundle args = new Bundle();
          args.putInt("headers", headers);
          args.putInt("footers", footers);
          f.setArguments(args);
  
          return f;
      }
  
      public DragSortController getController() {
          return mController;
      }
  
      /**
       * Called from DSLVFragment.onActivityCreated(). Override to
       * set a different adapter.
       */
      public void setListAdapter() {
          array = getResources().getStringArray(R.array.jazz_artist_names);
          list = new ArrayList<String>(Arrays.asList(array));
  
          adapter = new ArrayAdapter<String>(getActivity(), getItemLayout(), R.id.text, list);
          setListAdapter(adapter);
      }
  
      /**
       * Called in onCreateView. Override this to provide a custom
       * DragSortController.
       */
      public DragSortController buildController(DragSortListView dslv) {
          // defaults are
          //   dragStartMode = onDown
          //   removeMode = flingRight
          DragSortController controller = new DragSortController(dslv);
          controller.setDragHandleId(R.id.drag_handle);
          controller.setClickRemoveId(R.id.click_remove);
          controller.setRemoveEnabled(removeEnabled);
          controller.setSortEnabled(sortEnabled);
          controller.setDragInitMode(dragStartMode);
          controller.setRemoveMode(removeMode);
          return controller;
      }
  
  
      /** Called when the activity is first created. */
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          mDslv = (DragSortListView) inflater.inflate(getLayout(), container, false);
  
          mController = buildController(mDslv);
          mDslv.setFloatViewManager(mController);
          mDslv.setOnTouchListener(mController);
          mDslv.setDragEnabled(dragEnabled);
  
          return mDslv;
      }
  
      @Override
      public void onActivityCreated(Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
  
          mDslv = (DragSortListView) getListView(); 
  
          mDslv.setDropListener(onDrop);
          mDslv.setRemoveListener(onRemove);
  
          Bundle args = getArguments();
          int headers = 0;
          int footers = 0;
          if (args != null) {
              headers = args.getInt("headers", 0);
              footers = args.getInt("footers", 0);
          }
  
          for (int i = 0; i < headers; i++) {
              addHeader(getActivity(), mDslv);
          }
          for (int i = 0; i < footers; i++) {
              addFooter(getActivity(), mDslv);
          }
  
          setListAdapter();
      }
  
  
      public static void addHeader(Activity activity, DragSortListView dslv) {
          LayoutInflater inflater = activity.getLayoutInflater();
          int count = dslv.getHeaderViewsCount();
  
          TextView header = (TextView) inflater.inflate(R.layout.header_footer, null);
          header.setText("Header #" + (count + 1));
  
          dslv.addHeaderView(header, null, false);
      }
  
      public static void addFooter(Activity activity, DragSortListView dslv) {
          LayoutInflater inflater = activity.getLayoutInflater();
          int count = dslv.getFooterViewsCount();
  
          TextView footer = (TextView) inflater.inflate(R.layout.header_footer, null);
          footer.setText("Footer #" + (count + 1));
  
          dslv.addFooterView(footer, null, false);
      }
  
  }
  