Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to expand a list row when we tap on it in listview

In my app I have a Custom listview with some textviews and Image view like following image. enter image description here

My requirement is when we tap on a list row it should expand and when tap on it again or tap on any other row it should collapse. Expanded row should be like this enter image description here

Contents of the expanded row is dynamic, some times no data and some times more values. It should look like in the 2nd image. Please give a solution for this.

like image 427
Aju Avatar asked May 16 '12 06:05

Aju


1 Answers

You are looking for ExpandableListView.

Expandable ListView bydefault has two levels. First is called the "Group View" and the second level is called the "Child View". You can simply achieve by making use of the Custom Adapter sample from the very first link I have provided.

here are few links which will get you started,

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/ExpandableList1.html

http://about-android.blogspot.in/2010/04/steps-to-implement-expandablelistview.html

http://www.techienjoy.com/android-expandable-list-dynamically-created-example.php

EDIT 1

To make only child expanded at a particular time, add this,

explist.setOnGroupExpandListener(new OnGroupExpandListener() {

    public void onGroupExpand(int groupPosition) {


        for(int i=0; i<myExpAdapter.getGroupCount(); i++) {
            if(i != groupPosition) {
                explist.collapseGroup(i);
            }
        }
    }

});
like image 144
Andro Selva Avatar answered Oct 15 '22 19:10

Andro Selva