Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView doesnt fire setOnLongClickListener, only setOnItemClickListener

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?

like image 381
Ofershap Avatar asked Jul 07 '11 07:07

Ofershap


2 Answers

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) {

}
});
like image 157
kannappan Avatar answered Nov 12 '22 22:11

kannappan


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

like image 6
c0ming Avatar answered Nov 12 '22 21:11

c0ming