• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Collapse/expand ListView item to a different Layout?

I'm looking to have a standard ListView with an "expand/collapse" button.
If you click that button, it expands or collapses a STANDARD RELATIVELAYOUT item so the person can see the "full layout".
Before they click the button, a "short version" of the layout is shown.

Note the difference between what I'm looking for, and the ExpandableListView. The latter allows you to display a SUB-LIST of items under each list item, as you click "expand" on each of the list items. That's not what I'm looking for.

ExpandableListView is the closest thing I've found so far.

I'm looking for more of a ListView of "ExpandableRelativeLayout" items.

This has to be a common need. I looked through Android Studio, but haven't found what I'm looking for yet.
 
I don't quite see how the ExpandableListView doesn't give you what you want.
The layout for each list item can be anything you require. The normal case is just a LinearLayout with a TextView component. But it could be something more sophisticated.
Maybe I'm not understanding what you want to achieve though. Is it possible for you to create a UI mockup showing roughly how you want it to look?
 
Ah, re-reading your post, I think what you want to do is dynamically switch the layout, depending on the expansion state, right? It's possible to do this.

http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/

The method in your adapter which inflates and returns the View for each group node is getGroupView(). Notice that it takes a parameter 'isExpanded'. Your code can return a different View, depending on the value of this parameter.

Code:
@Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            if (isExpanded) {
                convertView = infalInflater.inflate(R.layout.list_group_full, null);
            }
            else {
                convertView = infalInflater.inflate(R.layout.list_group_summary, null);
            }
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        return convertView;
    }
 
Last edited by a moderator:
Back
Top Bottom