ExpandableListView child click listener not firing
Asked Answered
C

6

28

Not sure why my onChildClick isn't firing. Everything works perfectly, except that when one of the child items is tapped, absolutely nothing happens. Otherwise, the expandable groups work as expected.

I've traced this back to my usage of the checkbox in the child xml file. When I remove this checkbox, the onChildClick fires as expected. But I need this checkbox for the functionality of this activity. What I am I doing wrong? Thanks!

public class MySettings extends Activity {

    private ExpandListAdapter expAdapter;
    private ArrayList<ExpandListGroup> expListItems;
    private ExpandableListView expandableList;
    private String client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_settings);

        expandableList = (ExpandableListView) findViewById(R.id.expandable_list);
        expListItems = SetStandardGroups();  //works fine - can show code if needed
        expAdapter = new ExpandListAdapter(MySettings.this, expListItems);
        expandableList.setAdapter(expAdapter);

        expandableList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                //Nothing here ever fires
                System.err.println("child clicked");
                Toast.makeText(getApplicationContext(), "child clicked", Toast.LENGTH_SHORT).show();
                return true;
            }
        });

    }

Here are the xml files:

activity_my_settings.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@drawable/background" >

    <ImageView
        android:id="@+id/logo"
        android:layout_marginTop="5dip"
        android:layout_alignParentTop="true"
        android:layout_width="wrap_content"
        android:layout_height="70dp"
        android:layout_gravity="left"
        android:contentDescription="@string/blank"
        android:src="@raw/logo" >
    </ImageView>

    <TextView
        android:id="@+id/my_settings"
        android:textColor="#000000"
        android:layout_below="@id/logo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:text="@string/my_settings"
        android:textSize="30sp"
        android:textStyle="bold" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/my_settings" >

        <ExpandableListView
            android:id="@+id/expandable_list"
            android:scrollingCache="false"
            android:layout_marginTop="20dip"
            android:layout_height="wrap_content"
            android:layout_width="match_parent" />

    </LinearLayout>

</RelativeLayout>

expandlist_group_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="55dip"
    android:background="#FF7C7C7C" >

    <TextView
        android:id="@+id/group_header"
        android:layout_marginLeft="40dp"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:gravity="center_vertical"
        android:textColor="#000000"
        android:textSize="22sp" />

</LinearLayout>

expandlist_child_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="55dip"
    android:orientation="horizontal" >

    <CheckBox
        android:id="@+id/check_box"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/expand_list_item"
        android:paddingLeft="10dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="@dimen/smart_finder_settings_font_size"
        android:textColor="#FFFFFF" />

</LinearLayout>
Carrousel answered 3/6, 2013 at 0:20 Comment(0)
C
28

I got it. All I had to do was add

android:focusable="false"

within the CheckBox section of my expandlist_child_item.xml file.

I hope that this helps somebody.

Carrousel answered 3/6, 2013 at 5:7 Comment(2)
Yes its working for me to but when we click on the checkbox item exactly at that time also the onlick listener is not firing.?Nicolis
@Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; }Splenectomy
L
57

Be sure to override the isChildSelectable method of your expandable list adapter and return true, like so:

public class MyExpandableListAdapter extends BaseExpandableListAdapter {
...

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}
...
}
Laurynlausanne answered 19/2, 2014 at 5:23 Comment(1)
This seems to be problem in many situations where custom layouts are used to fill the parent, the views in the custom layout will steal the focus and will be implementing there very own ClickListners and LongClickListeners....Centring
C
28

I got it. All I had to do was add

android:focusable="false"

within the CheckBox section of my expandlist_child_item.xml file.

I hope that this helps somebody.

Carrousel answered 3/6, 2013 at 5:7 Comment(2)
Yes its working for me to but when we click on the checkbox item exactly at that time also the onlick listener is not firing.?Nicolis
@Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; }Splenectomy
S
12

Looks all right, still:

  1. Check that you have not set a click listener to any parent view of listview.

  2. Check that isChildSelectable() of adapter returns true. Also areAllItemsEnabled() should return true.

Sidell answered 3/6, 2013 at 5:4 Comment(0)
P
4

Checkbox should not be focusable, nor clickable.

Pancratium answered 27/2, 2014 at 8:2 Comment(0)
C
2

Checkbox should not be focuable and clickable ..

<CheckBox
            android:focusable="false"
            android:clickable="false"
            android:focusableInTouchMode="false"
            android:id="@+id/expandedListItem"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
            android:paddingTop="10dp"
            android:paddingBottom="10dp" />
Couteau answered 3/1, 2017 at 5:52 Comment(0)
T
0

I think yu have to use the onItemClickListener and use the passed parameter to see if it is group click or not

Trophic answered 3/6, 2013 at 2:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.