android - ExpandableListView 中的子选择

标签 android 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);
}
}

最佳答案

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

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

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

相关文章:

java - 所有 EditText 均在旋转并按回后填充来自一个 EditText 的数据

java - 如何将字符串从一个 Activity 传递到另一个 Activity ?

android - Android 的 SQLiteDatabase 中的冲突解决

android - 从我的适配器中刷新 ExpandableListView

java - 在 ExpandableListView 中拥有多个 TextView 时遇到问题

android - 在 Android 4.3 中使用“查找我”配置文件

Android - 具有多个 ListActivity 的应用程序导致 onListItemClick 中的正确 Id

java - 如何重命名展开 ListView (android)的子ID?

android - gmail android 应用程序,如对话 View

android - ExpandableListView 总是折叠的