ListView onItemClickListener not called

I tried to create a custom ListView . ListView contains RelativeLayout , which contains TextView and a Switch . When you click on the switch, the switch should change from true to false (and vice versa).

This is my getView method:

 public View getView(int position, View convertView, ViewGroup parent) { View vi = convertView; if (convertView == null) vi = inflater.inflate(R.layout.item_gproblem, null); //vi.setClickable(true); Tried this //vi.setFocusable(true); TextView txt_comment_description = (TextView) vi .findViewById(R.id.txt_comment_description); txt_comment_description.setText(MyTasks.allGComments.get(position) .getProblemDescription()); //txt_comment_description.setFocusable(false); Tried this //txt_comment_description.setClickable(false); Switch switch_comment = (Switch) vi.findViewById(R.id.switch_comment); //switch_comment.setFocusable(false); Tried this //switch_comment.setClickable(false); //First time running getMyGComments returns a empty ArrayList if (MyTasks.allCustomers.get(ServerData.myID - 1).getMyGComments() .size() > position) { switch_comment.setChecked(MyTasks.allCustomers .get(ServerData.myID - 1).getMyGComments().get(position) .isProblemValue()); } return vi; } 

This is my onClickListener:

 list_quality.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //Do Something } }); 

My onItemClickListener not called when I click on a TextView, Switch, or a space between two objects. When I press the switch, the switch works fine (the state changes). But my onItemClickListener not called. I tried to disable the clickable and focusable switch and TextView, but this also does not work.

Running setOnItemClickListeren.

+6
source share
2 answers

Add the line below to the listview row container:

 android:descendantFocusability="blocksDescendants" 

Remove all clickables / focusables from where you placed them. Then you should call onItemClick if you click on the whole element.

Also, if you want the buttons inside the list box to be clickable as well, add onClickListener to the button inside your getView () ListView () method.

+6
source

Some UI elements may receive focus instead of a list item, so use it to return it

 android:focusable="false" 
+3
source

All Articles