ListView doesnt fire setOnLongClickListener, only setOnItemClickListener
Asked Answered
I

4

8

I'd like to have both type of clicks on a listView - onClick and LongClick.

I've implemented it like this:

this.listViewSub = (ListView) this.findViewById(R.id.listsub);

this.listViewSub.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(final AdapterView parent, final View view, final int position,
                final long id) { ... }    });

        // listen to long click - to share texts
    this.listViewSub.setOnLongClickListener(new OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) { ... } });

But it does't fire the Long Click. Anyone has any idea why?

Insipience answered 7/7, 2011 at 7:54 Comment(0)
C
25

You have to enable the LongClickable

list.setLongClickable(true);

and

list.setOnItemLongClickListener(new OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                final int arg2, long arg3) {

}
});
Crifasi answered 7/7, 2011 at 8:19 Comment(3)
Thanks man! Solved my problem :) And BTW - you don't have to do setLongClickable trough code, its nicer when you determn it through the XML: android:longClickable="true"Insipience
The documentation of setOnItemLongClickListener says that "If this view is not long clickable, it becomes long clickable."Scarcity
Make sure to use setOnItemLongClickListener, NOT setOnLongClickListenerArmagh
A
7

@Vadim, are your listview's adapter is extends from BaseAdapter? if yes, then also need to set convertView.setLongClickable(true); in the getView().

Apparel answered 17/1, 2013 at 4:57 Comment(1)
Why did I have to set setLongClickable(true) in getView() and android:longClickable="true" in ListView's XML declaration?Amaze
L
1

For me, I had to set android:longClickable="true" in the XML file that contains my ListView row layout (not ListView layout) for the item to be long-clickable.

Lot answered 29/5, 2013 at 8:49 Comment(1)
Of the many proposed solutions I've seen for this issue (and I've tried them ALL), this is the only one that has worked for me.Rackrent
P
0

onLongClick returns true if the callback consumed the long click, false otherwise. So if the event is handled by this method, return true.

Paeon answered 7/7, 2011 at 8:7 Comment(1)
I've added a breakpoint inside this method, and it doesn't stops there.. it looks like it doesnt fire the event.Insipience

© 2022 - 2024 — McMap. All rights reserved.