ExpandableListView with CheckedTextView

ExpandableListView

I was implementing a settings list activity with CheckedTextView and I found out that Android’s ExpandableListView is a handy component to display some hierarchical data. However in my case there was one weakness. You can not see which items are selected when a parent group is collapsed. I wanted to display the text of selected child items in the parent view. My biggest problem was that I couldn’t reach the parent view in reliably way when child item was clicked.

I tried to search from the internets are there any solutions ready, but I found nothing there (or I used wrong search terms). As earlier I made my own solution.

My ExpandableListView solution is based on this tutorial. 

I solved parent view issue by setting unique tag to parent view in Adapter class.

@Override
//in this method you must set the text to see the parent/group on the list
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {

if (view == null) {
view = inflater.inflate(R.layout.settings_list_item_parent, viewGroup,false);
}
// set category name as tag so view can be found view later
view.setTag(getGroup(i).toString());

….

}

Based on that tag I can find parent view later when child item is clicked.

@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {

CheckedTextView checkbox = (CheckedTextView)v.findViewById(R.id.list_item_text_child);
checkbox.toggle();

// find parent view by tag
View parentView = categoriesList.findViewWithTag(categories.get(groupPosition).name);


}

One nice feature that I have used in my ExpandableListView is autocollapse feature that I found here.

Get the full source code here: https://github.com/nevalla/expandablelistview-demo

Advertisements

One thought on “ExpandableListView with CheckedTextView

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s