android expandablelistview does not expand or receive click events
Asked Answered
H

11

12

I can't for the life of me figure out why my ExpandableListView doesn't expand... I have used log statements in just about every click listener I can find for the ExpandableListView and it doesnt look like any of them get called.

I know there are many posts on this topic but I have read through them all and tried many things and am having no luck, hopefully I'm missing some tiny error that will be easy to spot for someone else.

Main Activity:

public class ForumListActivity extends Activity  {

    private static ArrayList<Forum> forumList;
    private static ArrayList<ArrayList<SubForum>> subForumList;
    private ExpandableListView forumListView;
    private ForumListAdapter forumListAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.main_page);
        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

        forumListView = (ExpandableListView) this.findViewById(R.id.main_page_forum_list);

        forumList = new ArrayList<Forum>();
        subForumList = new ArrayList<ArrayList<SubForum>>();
        setUpForums(this);

        forumListAdapter = new ForumListAdapter(this, forumList, subForumList);
        forumListView.setAdapter(forumListAdapter);

        forumListView.setOnGroupExpandListener(new OnGroupExpandListener(){
            @Override
            public void onGroupExpand(int groupPosition) {
                Log.d("onGroupExpand", "this works?");
                for(int i=0; i<forumListAdapter.getGroupCount(); i++) {
                    if(i != groupPosition) 
                        forumListView.collapseGroup(groupPosition);
                }
            }
        });

        forumListView.setOnGroupClickListener(new OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                Log.d("onGroupClick:", "worked");
                parent.expandGroup(groupPosition);
                return true;
            }
        });
    }

Note: the method setUpForums() just takes system arrays and puts them into forumList and subForumList

ListViewAdapter:

public class ForumListAdapter extends BaseExpandableListAdapter {

    private ArrayList<Forum> groups;
    private ArrayList<ArrayList<SubForum>> children;
    private Context ctx;

    public ForumListAdapter(Context ctx, ArrayList<Forum> groups, ArrayList<ArrayList<SubForum>> children) {
        this.ctx = ctx;
        this.groups = groups;
        this.children = children;
    }



    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return children.get(groupPosition).get(childPosition);
    }



    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }



    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(ctx);
            convertView = inflater.inflate(R.layout.forum_list_child_item_row, null);
        }

        SubForum currentSubForum = children.get(groupPosition).get(childPosition);
        TextView name = (TextView)convertView.findViewById(R.id.child_row_forum_title);
        TextView desc = (TextView)convertView.findViewById(R.id.child_row_forum_description);

        if (name != null)
            name.setText(currentSubForum.getName());

        if (desc != null)
            desc.setText(currentSubForum.getDescription());

        convertView.setFocusableInTouchMode(true);
        return convertView;
    }



    @Override
    public int getChildrenCount(int groupPosition) {
        return children.get(groupPosition).size();
    }



    @Override
    public Object getGroup(int groupPosition) {
        return groups.get(groupPosition);
    }



    @Override
    public int getGroupCount() {
        return groups.size();
    }



    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }



    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null)
        {
            LayoutInflater inflater = LayoutInflater.from(ctx);
            convertView = inflater.inflate(R.layout.forum_list_group_item_row, null);
        }

        Forum currentForum = (Forum) groups.get(groupPosition);
        TextView name = (TextView) convertView.findViewById(R.id.group_item_forum_title);
        //ImageView image = (ImageView) convertView.findViewById(R.id.group_item_expander_image);

        if(name != null)
            name.setText(currentForum.getName());           

        /*
        if(image != null) {
            int[][] group_state_sets = {{}, {android.R.attr.state_expanded}};
            image.setVisibility(View.VISIBLE);  
            int stateSetIndex = (isExpanded ? 1 : 0) ;  
            Drawable drawable = image.getDrawable();  
            drawable.setState(group_state_sets[stateSetIndex]);  
        }
        */

        return convertView;
    }



    @Override
    public boolean hasStableIds() {
        return true;
    }



    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }


}

Group Layout:

<?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="fill_parent"
    android:background="@drawable/turquoise_gradient"
    android:orientation="horizontal"
    android:paddingTop="6dip"
    android:paddingBottom="6dip"
    android:paddingLeft="6dip" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/turquoise_gradient"
        android:orientation="vertical"
        android:padding="2dip" >

        <TextView
            android:id="@+id/group_item_forum_title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|left"
            android:gravity="left"
            android:paddingLeft="5dip"
            android:textColor="@color/white"
            android:textSize="16dip" />

    </LinearLayout>

    <!--  
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:gravity="center|right">

        <ImageView
            android:id="@+id/group_item_expander_image"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/collapse_down" />


    </LinearLayout> -->

</LinearLayout>

child layout:

<?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="fill_parent"
    android:background="@drawable/turquoise_gradient"
    android:orientation="horizontal"
    android:paddingTop="6dip"
    android:paddingBottom="6dip"
    android:paddingLeft="6dip" >


    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="2dip"
        android:background="@drawable/turquoise_gradient" >

        <TextView
            android:id="@+id/child_row_forum_title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:layout_gravity="center_vertical"
            android:paddingLeft="5dip"
            android:textColor="@color/white"
            android:maxLines="1"
            android:textSize="11dip"  />

         <TextView
            android:id="@+id/child_row_forum_description"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:layout_gravity="center_vertical"
            android:paddingLeft="15dip"
            android:textColor="@color/white"
            android:maxLines="2"
            android:textSize="11dip"  />

    </LinearLayout>

</LinearLayout>

main page layout:

<?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="fill_parent"
    android:background="@color/black"
    android:orientation="vertical" >

    <ExpandableListView
        android:id="@+id/main_page_forum_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/black"
        android:divider="@color/black"
        android:dividerHeight="1dip"
        android:clickable="true" />

</LinearLayout>

Any help you can provide is greatly appreciated!

Hydrophyte answered 13/6, 2012 at 4:40 Comment(2)
its may be because you dont have child data there, have u debuggedDeliberative
try adding some default text value in your childs layout for textviewsDeliberative
E
25

I've also encountered similar problem like you. After a couple of days of investigation, I found that I did something wrong. So I fixed it to work correctly by making small change.

Let's look at the body of boolean onGroupClick(...) in setOnGroupClickListener. You've returned true that means "the click was handled"

You should return false if you want to expand. So I suggest you to do like this :

forumListView.setOnGroupClickListener(new OnGroupClickListener() {
    @Override
    public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
            Log.d("onGroupClick:", "worked");
            parent.expandGroup(groupPosition);
            return false;
        }
    });

in android.widget.ExpandableListView class, there is a method named boolean handleItemClick(View v, int position, long id) which is responsible for expanding/collapsing groups or passing on the click to the proper child.

 /* It's a group click, so pass on event */
         if (mOnGroupClickListener != null) {
             if (mOnGroupClickListener.onGroupClick(this, v,
                     posMetadata.position.groupPos, id)) {
                 posMetadata.recycle();
                 return true;
             }
         }

  /* expanding/collapsing/other tasks... */

if you implement onGroupClick to return true, the the code below 8th line will never be executed. (that means, groups will never be collapsed, expanded)

Hope my answer helped you :-) good luck!

Eaves answered 14/7, 2012 at 16:2 Comment(1)
you just saved a 3 days of headaches.thanks and +1 for a great answerSmokeproof
C
25

In case you have a widget on your list item, such as a Button, you may have to add android:focusable="false" to it. The Button was not allowing my list item to be clicked. That was the issue in my case.

Cockade answered 10/7, 2015 at 16:41 Comment(2)
This helped me.Planometer
It might not work if you do it in xml. Add button.setFocusable(false); in java.Cavallaro
D
7

There are probably three things u need to check,

  1. check if u have any data available for the chid, cos if u dont have any data the child will not appear at all.

2.try removing if condition check while you using layout inflaters

 if (convertView == null) {
    LayoutInflater inflater = LayoutInflater.from(ctx);
    convertView = inflater.inflate(R.layout.forum_list_child_item_row, null);
    }
  1. you need to also pass Viewgroup here

      convertView = inflater.inflate(R.layout.forum_list_child_item_row,parent, false);
    
Deliberative answered 13/6, 2012 at 5:7 Comment(1)
The bad data was my problem, Thanks again!!Hydrophyte
P
5

I know this was already answered, but try setting the base layout of whatever you're inflating to have the attribute:

android:descendantFocusability="blocksDescendants"
Putumayo answered 31/8, 2016 at 15:21 Comment(0)
B
4

If your expandable listview parent has button or switch it doesn't get called i wasted whole day in this. So just use below code

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

Add this code inside toggle button,switch button or any which is on expandable listview

Bedeck answered 11/4, 2018 at 5:50 Comment(1)
Add this code inside toggle button,switch button or any which is on expandable listview.Bedeck
S
3

Make sure your custom group layout does not have android:textIsSelectable="false" as "true", if the text in textview is set to selectable, the expandable listview would expand in gingerbread but not in jellybean and might not work in ICS too.

Sugihara answered 2/3, 2013 at 8:11 Comment(0)
I
1

I had a similar problem and it was solved by removing the android:clickable="true" property from ExpandableListView on xml.

Idun answered 17/12, 2014 at 13:49 Comment(0)
O
0

Add implements OnGroupExpandListener to your Activity. Then It will works. I am Using same it works fine.

Overnice answered 13/6, 2012 at 6:24 Comment(0)
L
0

When you are working with expandable lists then group expand is default functionality in it . means that group will expand itself only when you click on it you donot need to overrirde onGroupExpand(int groupPosition) or any other method just simply populate your data into your list somthing like this:

   public class MyActivity extends Activity { 

  private ExpandableListView forumListView;
  private ForumListAdapter forumListAdapter;
  String[] forumList={"group 1","group 2","group 3"};
 String[][] subForumList={{"group 1 child1","group 1 child1","group 1 child3"},
                     {"group 2 child1","group 2 child2","group 2 child3"},
                     {"group 3 child1","group 3 child2","group 3 child3"},
                     };
     @Override
     public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);


forumListView = (ExpandableListView) this.findViewById(R.id.main_page_forum_list);




forumListAdapter = new ForumListAdapter(this, forumList, subForumList);
forumListView.setAdapter(forumListAdapter);



  /* forumListView.setOnGroupExpandListener(new OnGroupExpandListener(){
    public void onGroupExpand(int groupPosition) {
        Log.d("onGroupExpand", "this shit works?");
        for(int i=0; i<forumListAdapter.getGroupCount(); i++) {
            if(i != groupPosition) 
                forumListView.collapseGroup(groupPosition);
        }
    }
});

forumListView.setOnGroupClickListener(new OnGroupClickListener() {
     public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long  id) {
        Log.d("onGroupClick:", "worked");
        parent.expandGroup(groupPosition);
        return true;
    }
});*/
     }

    public class ForumListAdapter extends BaseExpandableListAdapter {

      String[] groups;
   String[][] children;
     private Context ctx;

   public ForumListAdapter(Context ctx, String[] groups, String[][] children) {
    this.ctx = ctx;
    this.groups = groups;
    this.children = children;
}

public Object getChild(int arg0, int arg1) {
    // TODO Auto-generated method stub
    return children[arg0][arg1];
}

public long getChildId(int arg0, int arg1) {
    // TODO Auto-generated method stub
    return arg1;
}

public View getChildView(int arg0, int arg1, boolean arg2, View arg3,
        ViewGroup arg4) {
    if (arg3 == null) {
        LayoutInflater inflater = LayoutInflater.from(ctx);
        arg3 = inflater.inflate(R.layout.child, null);
    }

    String childData = children[arg0][arg1];
    TextView name = (TextView)arg3.findViewById(R.id.child_row_forum_title);
    TextView desc = (TextView)arg3.findViewById(R.id.child_row_forum_description);

    if (name != null)
        name.setText(childData);

    if (desc != null)
       // desc.setText(currentSubForum.getDescription());

    arg3.setFocusableInTouchMode(true);
    return arg3;}

public int getChildrenCount(int arg0) {
    // TODO Auto-generated method stub
    return children[arg0].length;
}

public Object getGroup(int arg0) {
    // TODO Auto-generated method stub
    return groups[arg0];
}

public int getGroupCount() {
    // TODO Auto-generated method stub
    return groups.length;
}

public long getGroupId(int arg0) {
    // TODO Auto-generated method stub
    return arg0;
}

public View getGroupView(int arg0, boolean arg1, View arg2, ViewGroup arg3) {
    if (arg2 == null)
    {
        LayoutInflater inflater = LayoutInflater.from(ctx);
        arg2 = inflater.inflate(R.layout.group, null);
    }


    TextView name = (TextView) arg2.findViewById(R.id.group_item_forum_title);
    //ImageView image = (ImageView) arg2.findViewById(R.id.group_item_expander_image);

    if(name != null)
        name.setText(groups[arg0]);           

    /*
    if(image != null) {
        int[][] group_state_sets = {{}, {android.R.attr.state_expanded}};
        image.setVisibility(View.VISIBLE);  
        int stateSetIndex = (isExpanded ? 1 : 0) ;  
        Drawable drawable = image.getDrawable();  
        drawable.setState(group_state_sets[stateSetIndex]);  
    }
    */

    return arg2;}

public boolean hasStableIds() {
    // TODO Auto-generated method stub
    return false;
}

public boolean isChildSelectable(int arg0, int arg1) {
    // TODO Auto-generated method stub
    return false;
}
    }

       }
Lens answered 13/6, 2012 at 7:2 Comment(0)
I
0

forumListView.collapseGroup(groupPosition);

should be

forumListView.collapseGroup(i);

Isallobar answered 14/3, 2015 at 21:38 Comment(0)
N
0

In my case I had buttons in group and child views and even setting android:focusable="false" android:focusableInTouchMode="false" on both it didn't work.

So I had to change them from ImageButton to ImageView. The listeners for the clicks are the same. You might need to create a custom background to give the touch animation for the ImageView.

Nonpros answered 19/2, 2020 at 17:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.