I am using a Listfragment which has a checkbox with custom images that changes on state_checked.
But 'onListItemClick' function works only when checkbox is assigned to android:focusable="false" and android:clickable="false" in its layout.
In this case the custom image change of checkbox is not working.
Is there a way to accomplish both together.Any help is appreciated.
public class SympFragment extends ListFragment implements OnClickListener {
private ListView lv;
private String listview_array[] = {
"Text1",
"Text2",
"Text3",
"Text4",
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_symp, container, false);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
R.layout.ch_row, listview_array);
setListAdapter(adapter);
return rootView;
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
switch( position )
{
case 0: Toast.makeText(getActivity().getApplicationContext(), "Well Done!", Toast.LENGTH_LONG).show();
break;
}
}
}
Layout:
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/chcheckbox"
android:textSize="15sp"
android:textColor="#ffffff"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:button="@drawable/chselector"
android:drawableLeft="@android:color/transparent"
android:focusable="false"
android:clickable="false"
android:padding="8dp"
/>
Selector xml:
<?xml version="1.0" encoding="utf-8"?>
<item android:drawable="@drawable/unchecked" android:state_checked="false"/>
<item android:drawable="@drawable/checked" android:state_checked="true"/>
<item android:drawable="@drawable/unchecked"/>
Seems like 'onListItemClick' never accepts checkbox input. So the checkbox has to be setted to android:focusable="false" and android:clickable="false". But checkbox can be triggered by the following way :
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String item = (String) getListAdapter().getItem(position);
Toast.makeText(getActivity(), item + " selected", Toast.LENGTH_LONG).show();
CheckBox chcheckbox;
switch(position){
case 0:
chcheckbox = (CheckBox) v.findViewById(R.id.chcheckbox);
if (chcheckbox.isChecked() == false) {
chcheckbox.setChecked(true);
} else {
chcheckbox.setChecked(false);
} break;
case 1:
chcheckbox = (CheckBox) v.findViewById(R.id.chcheckbox);
if (chcheckbox.isChecked() == false) {
chcheckbox.setChecked(true);
} else {
chcheckbox.setChecked(false);
} break;
}
}
Am not sure whether this is a best practice. But this tweak is much useful as I didn't find any other solution.