0

I'm using a CheckBox in the row item of my ListView. Is set the CheckBox to android:focusable="false". So the OnItemClickListener for the ListView works correctly but the checkBox seems to be unclickable. How can i solve this?

in my custom arrayAdapter:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        FileHolder holder;
        if(v == null){
            LayoutInflater inflater = ((Activity)getContext()).getLayoutInflater();
            v = inflater.inflate(layoutResourceId, parent, false);
            holder = new FileHolder();
            holder.isChecked = (CheckBox)v.findViewById(R.id.checkBox1);
            v.setTag(holder);
            holder.isChecked.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View v) {
                    Log.d("common", "checked");
                }

            });
        }
}

in row_item.xml:

<CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true" 
        android:button="@drawable/checkbox"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:clickable="true"/>

EDIT: I tried to register an OnTouchListener, but onTouch() gets also not called.

user2224350
  • 2,262
  • 5
  • 28
  • 54

1 Answers1

1

I don't think that you should set focusable and focusableInTouchMode to false. Try to set parent at null when you inflate layout

v = inflater.inflate(layoutResourceId, null, false);

Hope it will help.

Also I think that if your list is long you will have an performance issue. It discourage to create new OnClickListener(){} on getView(int position, View convertView, ViewGroup parent) {} if you list have a lot of row. Create on instance of listener set it for all row. You can use tag to determinate which view is click.

user3535747
  • 253
  • 1
  • 2
  • 7
  • Yes you're right with the OnClickListener, thanks ! But, setting focusable to false is necessary otherwise the onItemClickListener would not work : http://www.mysamplecode.com/2012/07/android-listview-checkbox-example.html Unfortunately, inflating the row view without a rootView doesnt work better – user2224350 Apr 16 '14 at 19:20