在ExpandableListView
中是否可以初始選擇一個子項目,以便包含的組被擴展並且列表滾動到該子項的位置?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);
}
}
就是這樣。謝謝 – fiddler 2013-03-21 12:29:24
完美的解決方案.... – karn 2014-01-22 06:56:12
爲什麼文檔沒有告訴'setSelectedChild()中的shouldExpandGroup參數設置爲true,只有在至少有一個組在第一個位置展開時才起作用。 – nmxprime 2014-06-05 08:57:03