简体   繁体   中英

List item with CheckBox not clickable

I have a list item which contains a CheckBox, and I want to be able to click on the CheckBox and on the list item itself. Unfortunately, there seems to be some sort of conflict between the two, as I can only click on the item when I comment out the CheckBox. It seems like I recall there was a way to fix this, but I can't find it at the moment. Thanks

EDIT: This is with a ListFragment, so there's no need to call setOnItemClickListener.

OK, here's the XML for the list item. The problem is the CheckBox, but I figured might as well copy everything.

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list_item_survey"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    style="@style/SimpleListItem">
    <TextView
        android:id="@+id/survey_title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        style="@style/ListItemTitle" />
    <TextView
        android:id="@+id/survey_date"
        android:layout_below="@id/survey_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/ListItemSubtitle" />
    <TextView
        android:id="@+id/survey_completed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@id/survey_title"
        android:textColor="@color/accent_1"
        android:text="@string/survey_completed"
        style="@style/ListItemSubtitle" />
    <CheckBox
        android:id="@+id/survey_did_not_attend"
        android:layout_below="@id/survey_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/survey_did_not_attend"
        android:focusable="false"
        style="@style/ListItemSubtitle" />
 </RelativeLayout>

你需要将它添加到自定义适配器xml文件android:descendantFocusability =“blocksDescendants”

将其插入到项行 xml文件的根元素中

android:descendantFocusability="blocksDescendants"

As described here and here , this is either a known problem or works as designed. If you have any clickable or focusable items in a list item, the list item itself cannot be clickable. Romain Guy says "This is working as intended to support trackball/dpad navigation."

This did the work for me

<CheckBox
...                
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false" />

Have you tried setting

android:focusable="false"
android:focusableInTouchMode="false"

?

It worked for me.

通过使用android:descendantFocusability =“blocksDescendants”它的工作正常

I solved the problem this way. I implemented OnClickListener inside the Adapter and not in the Fragment/Activity and it works well. Now I can use ListView with checkboxes and can click on both. Here is my code:

public class MyFragment extends Fragment
{
    ...

    private void setView()
    {
        ListView listView = (ListView) mRootView.findViewById(R.id.listview);
        mItems = DatabaseManager.getManager().getItems();

        // create adapter
        if(listView.getAdapter()==null)
        {
            MyAdapter adapter = new MyAdapter(this, mItems);
            try
            {
                listView.setAdapter(adapter);
            }
            catch(Exception e)
            {
                e.printStackTrace();
                return;
            }
        } 
        else 
        {
            try
            {
                ((MyAdapter) listView.getAdapter()).refill(mItems);
                BaseAdapter adapter = (BaseAdapter) listView.getAdapter();
                listView.requestLayout();
                adapter.notifyDataSetChanged();
            }
            catch(Exception e)
            {
                e.printStackTrace();
                return;
            }
        }

        // handle listview item click
        listView.setClickable(true);
        // listView.setOnItemClickListener(...); // this method does not work in our case, so we can handle that in adapter
    }

    ...
}


public class MyAdapter extends BaseAdapter
{
    ...

    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        View view = convertView;
        if (view == null) 
        {
            LayoutInflater inflater = (LayoutInflater) mFragment.getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.listview_item, null);
        }

        ...

        // handle listview item click
        // this method works pretty well also with checkboxes
        view.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // do something here
                // for communication with Fragment/Activity, you can use your own listener
            }
        });

        return view;
    }

    ...
}

Late, but nevertheless a solution for all those who still need it.

It is true that the built-in mechanism using:

final ArrayAdapter<String> adapter = new ArrayAdapter<String>(
    getActivity(),
    android.R.layout.simple_list_item_multiple_choice, lst);

does not allow both, ie click on the checkbox AND click on the list item. Wherever one clicks, the checkbox catches the event.

However, if you create your own ArrayAdapter with getView like this, then it works fine:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.list_item_ecu_fehler, null);
    }

    v.setFocusable(true);
    v.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
        if (DEBUG)
            Log.i(this.getClass().getSimpleName(),
                " ->>"
                    + Thread.currentThread().getStackTrace()[2]
                        .getMethodName());
        }
    });

            CheckBox selectedForClearingCB = (CheckBox) v
            .findViewById(R.id.checkBox);


        if (selectedForClearingCB != null) {
        selectedForClearingCB.setTag(position); //so we know position in the list       selectedForClearingCB.setChecked(true);
        selectedForClearingCB.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

            if (((CheckBox) v).isChecked()) {
                if (DEBUG)
                Log.i(this.getClass().getSimpleName(),
                    " -> CB checked: "
                        + Integer.toString((Integer) v
                            .getTag()));

            }

            }
        });
        }
    }
    return v;
    }

}

ok.. Make a CheckBox instance Like

CheckBox check;
check = (CheckBox) myView.findViewById(R.id.check);
check.setOnClickListener(new CheckBoxSelect(position));

put above code in onItemClickListener or in Adapter you are using. now make a class like below

private class CheckBoxSelect implements OnClickListener 
    {
        int pos;
        String str;

        public CheckBoxSelect(int position) 
        {
            pos = position;
        }

        public void onClick(View v) 
        {

        }

    }

perform any functionality in onClick .

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