3

In my listview there is a checkbox, textviews and an imageview. When I click on an item, the onclicklistnere is not working. I know this is because the checkbox has its own listener and it overrides the listview onclicklistener. Fine. When I set

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

in the checkbox, I can click on the list item but the checkbox becames orange as well (like the item).

So this is a half solution, does anyone know a better one?

This is the class:

    public class ListViewTutorial2Activity extends Activity {

    SimpleAdapter mSchedule;
    ListView list;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        list = (ListView) findViewById(R.id.SCHEDULE);

        ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("train", "101");
        map.put("from", "6:30 AM");
        map.put("to", "7:40 AM");
        mylist.add(map);
        map = new HashMap<String, String>();
        map.put("train", "103(x)");
        map.put("from", "6:35 AM");
        map.put("to", "7:45 AM");
        mylist.add(map);
         mSchedule = new SimpleAdapter(this, mylist, R.layout.row,
                    new String[] {"train", "from", "to"}, new int[] {R.id.TRAIN_CELL, R.id.FROM_CELL, R.id.TO_CELL});
        list.setAdapter(mSchedule);

        list.setOnItemClickListener(new OnItemClickListener() 
        {
        @Override
        public void onItemClick(AdapterView<?> a, View v, int position, long id) {
             Object o = list.getItemAtPosition(position);
            }
        });
    }
}

and row.xml:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:paddingTop="4dip"
     android:paddingBottom="6dip"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal">
    <CheckBox
        android:id="@+id/check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:descendantFocusability="beforeDescendants">
    </CheckBox>
     <TextView android:id="@+id/TRAIN_CELL"
         android:layout_width="50dip"
         android:layout_height="wrap_content"/>

     <TextView android:id="@+id/FROM_CELL"
         android:layout_width="70dip"
         android:layout_height="wrap_content" android:layout_weight="1"/>

     <TextView android:id="@+id/TO_CELL"
         android:layout_width="60dip"
         android:layout_height="wrap_content"  android:layout_weight="1"/>

            <ImageView
        android:id="@+id/icon"
        android:layout_width="22px"
        android:layout_height="22px"
        android:layout_marginLeft="4px"
        android:layout_marginRight="10px"
        android:layout_marginTop="4px"
        android:src="@drawable/icon" >
    </ImageView>
</LinearLayout>

5 Answers 5

8

Have you tried adding this to your CheckBox in your xml layout?

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

This worked for me and might be a better solution then what you had to go through.

Sign up to request clarification or add additional context in comments.

Comments

1

You don't need to set any special properties on the CheckBox to do this. In the ListView, however, try the following:

android:clickable="true"
android:descendantFocusability="beforeDescendants"

If that doesn't work, you're probably doing something wrong elsewhere.

2 Comments

You shouldn't deal with ListViews and ExpandableListViews that way (trying to access the items directly). Instead, you should make your own custom ListAdapter (for example, extends SimpleAdapter) and override it's .getView() method. In your custom version of the .getView() method you can do a local search in the child View itself for it's CheckBox (i.e. view.findViewById(R.id.itemcheckbox)) and set it's OnClickListener there. ListViews re-use Views for different items, so you must treat ListViews and their child views in this manner.
I accept your answer for this explanation. thanks. I managed to solve my problem at last but that was not easy.
1
android:focusable="false"

It will solve the problem in this context

Comments

0

I meet this problem at RecyclerView, maybe this answer will be useful for somebody. All of these answers don't helped me. Maybe this solutions isn't optimal, but here it is:

<FrameLayout
            android:id="@+id/like"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/views_holder"
            android:layout_toEndOf="@+id/btn_comment"
            android:clickable="true">

            <CheckBox
                android:id="@+id/cb_like"
                android:layout_width="wrap_content"
                android:layout_height="@dimen/pressable_size"
                android:gravity="center_vertical"
                android:button="@drawable/ic_heart_checkbox"
                android:paddingStart="8dp"
                android:paddingEnd="8dp"
                android:clickable="false"
                android:focusable="false"
                android:focusableInTouchMode="false"
                tools:text="324"
                />

        </FrameLayout>

I have added ViewGroup wrapper (in this case - FrameLayout) - and set click listener to FrameLayout: (In my case with ButterKnife):

@OnClick(R.id.like)
void onLikeClicked() {
        cbLike.setChecked(!cbLike.isChecked());
        if (onNewsClickListener != null) {
            onNewsClickListener.onLikeClicked(getAdapterPosition());
        }
    }

R.id.like - FrameLayout, cbLike(CheckBox). It works correctly.

Comments

0

I had the same problema even when i add this to the checkbox

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

Then I realized that my problem it was because i had a onStateChangedListener for the checkbox

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.