gpt4 book ai didi

android - ExpandableListView 中的子选择

转载 作者:太空宇宙 更新时间:2023-11-03 12:21:11 25 4
gpt4 key购买 nike

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

最佳答案

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

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

关于android - ExpandableListView 中的子选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15542892/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com