I was trying to make a custom ListView
. The ListView contains a RelativeLayout
who contains a TextView
and a Switch
. When you press on the Switch the Switch have to 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
isn't called when I click on the TextView, Switch or on the space between the two objects. When I hit the switch the switch acts normally (the state changes). But my onItemClickListener
isn't called. I tried to disabled clickable
and focusable
of the Switch and the TextView but that doesn't work either.
The setOnItemClickListeren is executed.
Add the line below to the listview row's container:
android:descendantFocusability="blocksDescendants"
Remove all clickables/focusables from wherever you put them. Then the onItemClick should be called if you press on the whole item.
Also, if you would like to have buttons inside the listview row clickable as well, add an onClickListener to the button inside your ListView adapter getView() method.
Some UI elements may get the focus instead of the list item so use this to get it back
android:focusable="false"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With