在ExpandableListView中,是否可以初始选择子项,以便扩展包含组并将列表滚动到该子项的位置? 我认为setSelectedChild可以做到这一点,但它没有给出任何结果. 我测试了以下代码: public class
我认为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()中设置为true的shouldExpandGroup参数似乎只有在首先扩展了至少一个组时才起作用.