Android ExpandableListView展开列表控件使用实例

2019-12-10 20:02:23丽君

        childs.add(childs_1);
        childs.add(childs_2);
        
        //创建ExpandableList的Adapter容器
        //参数: 1.上下文    2.一级集合 3.一级样式文件 4. 一级条目键值  5.一级显示控件名
        //   6. 二级集合 7. 二级样式 8.二级条目键值 9.二级显示控件名
        SimpleExpandableListAdapter sela = new SimpleExpandableListAdapter(
                this, gruops, R.drawable.groups, new String[]{"group"}, new int[]{R.id.textGroup}, 
                childs, R.drawable.childs, new String[]{"child"}, new int[]{R.id.textChild}
                );
        
        //加入列表
        setListAdapter(sela);
    }
}
//最后, 如果想响应各操作的话, 就要重载下面的方法
//列表内容按下
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id)
{
    // TODO Auto-generated method stub
    return super.onChildClick(parent, v, groupPosition, childPosition, id);
}

 

//二级标题按下
@Override
public boolean setSelectedChild(int groupPosition, int childPosition, boolean shouldExpandGroup)
{
    // TODO Auto-generated method stub
    return super.setSelectedChild(groupPosition, childPosition, shouldExpandGroup);
}

//一级标题按下
@Override
public void setSelectedGroup(int groupPosition)
{
    // TODO Auto-generated method stub
    super.setSelectedGroup(groupPosition);
}

 

再最后, 运行你的模拟器就可以看见啦。
将上面的ExpandableListView控件化.
控件化比较简单我们只要用普通的Activity类就可以了, 不用再继承ExpandableListView.
只需要在成员变量中添加
private ExpandableListView expandList;
然后在添加内容时改成