Question

I use DragSortListView library. It works fine - drag and remove items from my listview, but i also want to click ListView items. When I set OnClickListener it don't work. What could be the problem? Sorry for my bad english

My fragment:

private DragSortListView listView;
private MakeSingleBetAdapter adapter;
private View view;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    MyLog.d(TAG, "onCreateView");
    BugSenseHandler.initAndStartSession(getActivity(), APP_Parameters.BugSense);
    view = inflater.inflate(R.layout.fragment_make_single_bet,
            container, false);

    listView = (DragSortListView) view.findViewById(R.id.makeBetList);
    listView.setRemoveListener(this);
    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Toast.makeText(getActivity(), "Click: " + position, Toast.LENGTH_SHORT).show();
        }
    });

    cursor = dbHelper.getAllBets();
    adapter = new MakeSingleBetAdapter(getActivity(), cursor, 3, dbHelper);
    listView.setAdapter(adapter);
    return view;
}

@Override
public void remove(int which) {
    Cursor cursorId = (Cursor) (listView.getItemAtPosition(which));
    String id = cursorId.getString(cursor
            .getColumnIndex(DBHelper.COL_BET_ID));
    dbHelper.deleteBetById(id);
    adapter.swapCursor(dbHelper.getAllBets());
}

My XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/ch.bettings"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="5dp"
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true"
    android:orientation="vertical" >

    ....

    <com.mobeta.android.dslv.DragSortListView
        android:id="@+id/makeBetList"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/View1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:cacheColorHint="@color/slidingMenu"
        android:divider="#b5b5b5"
        app:drag_enabled="true"
        app:drag_start_mode="onMove"
        app:remove_enabled="true"
        app:remove_mode="flingRemove"
        app:sort_enabled="false" />

    ....

</RelativeLayout>

If I add ClickListener in my adapter - it work's fine, but I can not drag list items

Was it helpful?

Solution

Try adding these to your listview row:

android:focusable="false"
 android:focusableInTouchMode="false"

OTHER TIPS

This worked for me, I have been clicking the item in the list and its going to touch event so my code is as below. I wanted to only perform Onclick but not Ontouch just override the method and do nothing.

 private DragSortListView list;
 list = view.findViewById(android.R.id.list);
 list.addHeaderView(footerView);
        list.setFocusable(false);
        list.setHeaderDividersEnabled(false);

        list.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Toast.makeText(getActivity(),"its a touch" , Toast.LENGTH_LONG).show();
                Log.d("stack", "TOUCH EVENT");
                return false;
            }
        });


        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getActivity(),"Position :"+position+" ListItem : " +id , Toast.LENGTH_LONG).show();
                Log.i("edit recipe click", "clicked");
                EditableIngredient ingredient = (EditableIngredient) parent.getItemAtPosition(position);
                host.handleIngredientSelected(ingredient);
            }
        });

        return view;
    }

My Xml looks like

 <FrameLayout
            android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <com.mobeta.android.dslv.DragSortListView
                android:id="@android:id/list"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:choiceMode="singleChoice"
                app:drag_handle_id="@id/drag_handle"
                app:float_background_color="@android:color/white"
                 />

        </FrameLayout>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top