在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);
}
}
答案 0 :(得分:7)
在listView.expandGroup()
之前添加对setSelectedChild()
的电话。
在shouldExpandGroup
中设置为true的setSelectedChild()
参数似乎只有在首先扩展了至少一个组时才会起作用。
答案 1 :(得分: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)));