简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM