sdabet
sdabet

Reputation: 18670

Child selection in ExpandableListView

In an ExpandableListView is it possible to initially select a child item so that the containing group is expanded and the list is scrolled to this child's position ?

I thought setSelectedChild would do the trick but it doesn't give any result.

I tested with the following piece of code:

public class MainActivity extends Activity {

private static final String TITLE = "title";

@SuppressWarnings("serial")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ExpandableListView listView = new ExpandableListView(this);
    SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(this, 
        new ArrayList<Map<String,String>>() {
            {
                this.add(new HashMap<String, String>() {
                    {
                        this.put(TITLE, "Group 1");
                    }
                });
                this.add(new HashMap<String, String>() {
                    {
                        this.put(TITLE, "Group 2");
                    }
                });
                this.add(new HashMap<String, String>() {
                    {
                        this.put(TITLE, "Group 3");
                    }
                });
            }
        }, 
        android.R.layout.simple_expandable_list_item_1, 
        new String[] { TITLE }, 
        new int[] { android.R.id.text1 }, 
        new ArrayList<List<? extends Map<String,String>>>() {
            {
                this.add(new ArrayList<Map<String,String>>() {
                    {
                        this.add(new HashMap<String, String>() {
                            {
                                this.put(TITLE, "Child 1-1");
                            }
                        });
                        this.add(new HashMap<String, String>() {
                            {
                                this.put(TITLE, "Child 1-2");
                            }
                        });
                    }
                });
                this.add(new ArrayList<Map<String,String>>() {
                    {
                        this.add(new HashMap<String, String>() {
                            {
                                this.put(TITLE, "Child 2-1");
                            }
                        });
                        this.add(new HashMap<String, String>() {
                            {
                                this.put(TITLE, "Child 2-2");
                            }
                        });
                    }
                });
                this.add(new ArrayList<Map<String,String>>() {
                    {
                        this.add(new HashMap<String, String>() {
                            {
                                this.put(TITLE, "Child 3-1");
                            }
                        });
                        this.add(new HashMap<String, String>() {
                            {
                                this.put(TITLE, "Child 3-2");
                            }
                        });
                    }
                });
            }
        },
        android.R.layout.simple_expandable_list_item_2,
        new String[] { TITLE },
        new int[] { android.R.id.text1 }
    );
    listView.setAdapter(adapter);
    setContentView(listView);

    listView.setSelectedChild(1, 1, true);
}
}

Upvotes: 3

Views: 4772

Answers (2)

Viktor Koval
Viktor Koval

Reputation: 17

In an ExpandableListView is it possible to initially select a child item so that the containing group is expanded and the list is scrolled to this child's position:

//select child and open it's group
ExpListView.setSelectedChild(groupPos, childPos, true);
//scroll to selected child
ExpListView.smoothScrollToPosition(ExpListView.getFlatListPosition(ExpListView.getPackedPositionForChild(groupPos, childPos)));

Upvotes: 0

laalto
laalto

Reputation: 152817

Add a call to listView.expandGroup() before setSelectedChild().

The shouldExpandGroup parameter set to true in setSelectedChild() seems only to work if there's at least one group expanded in the first place.

Upvotes: 8

Related Questions