user2224350
user2224350

Reputation: 2334

Android CheckBox inside ListView is not clickable while onItemClickedListener works

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.

Upvotes: 0

Views: 466

Answers (1)

user3535747
user3535747

Reputation: 253

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.

Upvotes: 1

Related Questions