Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnItemClickListener doesn't work with ListView item containing button

I have ListView with custom Adapter which supplies View to ListView in this way:

   public View getView(int position, View convertView, ViewGroup parent)    {         RelativeLayout.LayoutParams lineParams;         RelativeLayout line=new RelativeLayout(context);          TextView tv=new TextView(context);         tv.setText("Text in postion="+i);         lineParams=new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);         lineParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);         line.addView(tv, lineParams);         lineParams.addRule(RelativeLayout.CENTER_IN_PARENT);          //checkbox         CheckBox checkBox=new CheckBox(context);         lineParams=new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);         lineParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);         lineParams.addRule(RelativeLayout.CENTER_IN_PARENT);         line.addView(checkBox, lineParams);         return line;     } 

And somewhere inside ListView there's setOnItemClickListener(), which should intercept item clicking events. My problem that, whenever I try to add checkbox to item - I don't get any responces from my ListView. If I skip CheckBox or any other Button it works.

I am really stuck with this problem, I have tried all kind of Layouts, aligning, wrapping and so on - useless. Looks like CheckBox interferes ListView item click events.

Any ideas how to overcome?

like image 973
Barmaley Avatar asked Dec 07 '11 10:12

Barmaley


People also ask

Why is OnItemClickListener not working?

If any row item of list contains focusable or clickable view then OnItemClickListener won't work. The row item must have a param like android:descendantFocusability = "blocksDescendants" . Save this answer.

What is the use of ListView explain list view with example?

Android ListView is a ViewGroup that is used to display the list of items in multiple rows and contains an adapter that automatically inserts the items into the list. The main purpose of the adapter is to fetch data from an array or database and insert each item that placed into the list for the desired result.


2 Answers

just add this line into the item views instead of listView itself

android:focusable="false" 

check more detail about this from Android custom ListView unable to click on items

like image 50
Pratik Avatar answered Sep 19 '22 03:09

Pratik


If you have ImageButtons inside the list item, you need to add:

android:descendantFocusability="blocksDescendants" 

to the root list item element [such as the root layout].

Then within each ImageButton in the list item, you need to add:

android:focusableInTouchMode="true" 

This worked for me - but I was using ImageButtons, not the standard button.

like image 44
dandev91 Avatar answered Sep 19 '22 03:09

dandev91