3

是否可以最初选择一个ExpandableListView子项,以便扩展包含组并将列表滚动到该子项的位置?

我认为setSelectedChild会做的伎俩,但它没有给出任何结果。

我使用以下代码进行了测试:

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);
}
}
4

2 回答 2

8

添加对listView.expandGroup()之前的调用setSelectedChild()

shouldExpandGroup设置为 true in的参数setSelectedChild()似乎只有在至少有一个组首先展开时才有效。

于 2013-03-21T11:13:47.727 回答
0

在 ExpandableListView 中是否可以最初选择一个子项,以便展开包含组并将列表滚动到该子项的位置:

//select child and open it's group
ExpListView.setSelectedChild(groupPos, childPos, true);
//scroll to selected child
ExpListView.smoothScrollToPosition(ExpListView.getFlatListPosition(ExpListView.getPackedPositionForChild(groupPos, childPos)));
于 2016-02-17T20:58:51.327 回答