在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);
}
}
最佳答案
在listView.expandGroup()
之前添加对setSelectedChild()
的调用。shouldExpandGroup
中的setSelectedChild()
参数设置为true似乎仅在至少有一个组首先展开时才起作用。