在ExpandableListView儿童选择儿童、ExpandableListView

2023-09-06 11:24:49 作者:花落忆流年

ExpandableListView 是有可能最初选择一个子项,以便包含组展开,并将列表滚动到这个孩子的位置?

我觉得 setSelectedChild 会做的伎俩,但它不给任何结果。

我用下面这段code测试

 公共类MainActivity延伸活动{

私有静态最后弦乐TITLE =称号;

@燮pressWarnings(串行)
@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);

    ExpandableListView的ListView =新ExpandableListView(本);
    SimpleExpandableListAdapter适配器=新SimpleExpandableListAdapter(这一点,
        新的ArrayList<地图<字符串,字符串>>(){
            {
                this.add(新的HashMap<字符串,字符串>(){
                    {
                        this.put(TITLE组1);
                    }
                });
                this.add(新的HashMap<字符串,字符串>(){
                    {
                        this.put(TITLE组2);
                    }
                });
                this.add(新的HashMap<字符串,字符串>(){
                    {
                        this.put(TITLE组3);
                    }
                });
            }
        },
        android.R.layout.simple_expandable_list_item_1,
        新的String [] {标题},
        新的INT [] {} android.R.id.text1,
        新的ArrayList<名单,LT ;?扩展地图<字符串,字符串>>>(){
            {
                this.add(新的ArrayList<地图<字符串,字符串>>(){
                    {
                        this.add(新的HashMap<字符串,字符串>(){
                            {
                                this.put(TITLE,孩子1-1);
                            }
                        });
                        this.add(新的HashMap<字符串,字符串>(){
                            {
                                this.put(TITLE,孩子1-2);
                            }
                        });
                    }
                });
                this.add(新的ArrayList<地图<字符串,字符串>>(){
                    {
                        this.add(新的HashMap<字符串,字符串>(){
                            {
                                this.put(TITLE,孩子2-1);
                            }
                        });
                        this.add(新的HashMap<字符串,字符串>(){
                            {
                                this.put(TITLE,孩子2-2);
                            }
                        });
                    }
                });
                this.add(新的ArrayList<地图<字符串,字符串>>(){
                    {
                        this.add(新的HashMap<字符串,字符串>(){
                            {
                                this.put(TITLE,孩子3-1);
                            }
                        });
                        this.add(新的HashMap<字符串,字符串>(){
                            {
                                this.put(TITLE,孩子3-2);
                            }
                        });
                    }
                });
            }
        },
        android.R.layout.simple_expandable_list_item_2,
        新的String [] {标题},
        新的INT [] {} android.R.id.text1
    );
    listView.setAdapter(适配器);
    的setContentView(ListView控件);

    listView.setSelectedChild(1,1,真);
}
}
 

解决方案

添加一个调用 listView.expandGroup() setSelectedChild()

shouldExpandGroup 参数设置为true的 setSelectedChild()似乎只是为了工作,如果有至​​少一个组扩大摆在首位。

In an ExpandableListView is it possible to initially select a child item so that the containing group is expanded and the list is scrolled to this child's position ?

Android 实现二级列表 ExpandableListView

I thought setSelectedChild would do the trick but it doesn't give any result.

I tested with the following piece of code:

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

解决方案

Add a call to listView.expandGroup() before setSelectedChild().

The shouldExpandGroup parameter set to true in setSelectedChild() seems only to work if there's at least one group expanded in the first place.

 
精彩推荐
图片推荐