setOnItemLongClickListener and setOnItemClickListener are not triggered when using drag-sort-listview (DSLV)

munkay picture munkay · Aug 14, 2012 · Viewed 9.6k times · Source

I'm using the excellent drag-sort-listview by Carl Bauer (https://github.com/bauerca/drag-sort-listview) to implement a drag-sort enabled listview. However, my requirement is not to need a drag handle on the list, but instead to allow the user to drag the list items using the item itself.

I've gotten that part to work, by setting the @id/drag property to the list item itself. However, it has a side-effect of not responding to itemClick and itemLongClick events.

Is there any way to get the item clicks / long clicks to work without having a separate draggable layout?

For reference, my code looks like below -

ListView.xml:

<com.mobeta.android.dslv.DragSortListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:dslv="http://schemas.android.com/apk/res/com.myproject"
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    dslv:collapsed_height="1dp"
    dslv:drag_scroll_start="0.33"
    dslv:max_drag_scroll_speed="0.5" /> 

ItemView.xml:

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="@dimen/list_item_height"
  android:orientation="horizontal">
  <CheckBox 
      android:id="@+id/check_box"
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:gravity="center_vertical"/>
  <TextView
    android:id="@+id/drag"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:padding="@dimen/list_padding"
    android:gravity="center_vertical" />
</LinearLayout>

Activity.java:

    DragSortListView listView = (DragSortListView) view.findViewById(R.id.list);

    listView.setOnItemLongClickListener(new OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            Toast.makeText(arg0.getContext(), ((TextView)arg1).getText(), Toast.LENGTH_SHORT).show();
            return false;
        }
    });

    listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> listView, View itemView, int index,
                long id) {
            Toast.makeText(getView().getContext(), ((TextView)itemView).getText(), Toast.LENGTH_SHORT).show();
        }
    });

As a bonus, if anyone can help enable multiple-select in addition to click/longclick, it would be much appreciated.

Thanks!

Answer

BurgerZ picture BurgerZ · May 3, 2013

To be able to use OnItemClick and OnItemLongClick in your list you need to set this parameter to the com.mobeta.android.dslv.DragSortListView layout.

dslv:drag_start_mode="onMove"