0

I have a custom adapter, which extends BaseExpandableListAdapter.

I want my ExpandableList to expand and collapse when I'm clicking some TextView.

How should I implement it?

I suppose, i have to add OnClickListener to methods getGroupView and getChildView. And I see, there is boolean flag isExpanded.

So it should look like this:

@Override
public View getGroupView(int groupPosition, final boolean isExpanded,
                         View convertView, ViewGroup parent) {
textView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (isExpanded){

               //What should i write here?
            }
        }
    });
    return convertView;
}

But what should I write in OnClickListener? I cannot find any functions, which allow me to expand/collapse my listview.

Is there any function like listView.expandGroup(itemPosition);?

Alex
  • 350
  • 5
  • 20

2 Answers2

2

There actually is an ExpandableListView.expandGroup. And it's reverse action collapseGroup.

If the TextView you are referring to is inside a listview item, then you are correct to the action inside an onClickListener inside the adapter. This post talks about expanding an ExpandableListView dynamically, I think you can just get the idea and reverse the action in order to do what you are aiming for.

Hope this helps. Cheers! :D

Community
  • 1
  • 1
AL.
  • 36,815
  • 10
  • 142
  • 281
  • The main problem is that I have a 3-level expandable list. So i have a `ParentAdapter` and `ChildAdapter`. Inside Parent I create my custom `ExpendableList` and setting it to a `ChildAdapter`: `expList.setAdapter(new ChildAdapter(...);` – Alex May 10 '16 at 11:12
  • By `ParentAdapter` is it for the `GroupHeader`s and the `ChildAdapter` is for the items inside the `Group`? – AL. May 10 '16 at 11:13
  • Yes, you got it totally right. So I need to use `collapseGroup` method inside `ChildAdapter`, but I dont know how to transfer my custom `ExpandableList` to my `ChildAdapter`. – Alex May 10 '16 at 11:15
  • 1
    Hmm.. You can simply pass a reference of your `ExpandableListView` in your `ChildAdapter` constructor, then use it accordingly from inside. :) – AL. May 10 '16 at 11:19
  • 1
    Oh man, you are my savior, how could I be so blind. It's the most obvious option possible. Thank you! – Alex May 10 '16 at 11:24
  • Glad to be of help. Happy coding. Cheers! :D – AL. May 10 '16 at 11:25
0

The TextView you want to works as expand or collapse button for a group when you click on it must be a header of the ExpandableListViewAdapter.

Also you have setOnGroupExpandListener, and setOnGroupCollapseListener that triggers when a group is expanded or collapsed.

deimian86
  • 1,127
  • 1
  • 11
  • 26