ExpandableListView 使用方式

 ExpandableListView是可打开收起的含有子项的列表,就像qq分组一样

首先两个布局文件:

main.xml 定义一个ExpandableListView ,TextView表示无值显示

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
	<ExpandableListView 
	    android:layout_width="fill_parent"
	    android:layout_height="fill_parent"
	    android:id="@android:id/list"
	    ></ExpandableListView>
	<TextView 
	    android:layout_width="fill_parent"
	    android:layout_height="fill_parent"
	    android:id="@android:id/empty"
	     />
</LinearLayout>

lanmu.xml 定义组项和子项的显示样式

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
	<TextView 
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"
	    android:layout_marginLeft="50dp"
	    android:layout_gravity="center"
	    android:textSize="18dp"
	    android:id="@+id/txt1"
	    />
		<TextView 
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"
	    android:layout_marginLeft="50dp"
	    android:id="@+id/txt2"
	    />
</LinearLayout>


代码:

public class AndroidExpActivity extends ExpandableListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //存放group的list
        List<Map<String, String>> listGroup=new ArrayList<Map<String,String>>();
        //第一个group
        Map<String, String> group1=new HashMap<String, String>();
        group1.put("group1", "我的好友");
        //第二个group
        Map<String, String> group2=new HashMap<String, String>();
        group2.put("group1", "我的同学");
        listGroup.add(group1);
        listGroup.add(group2);
        //存放子项的list
        List<Map<String, String>> listRow1=new ArrayList<Map<String,String>>();
        //第一组的第一个子项
        Map<String,String> row1=new HashMap<String, String>();
        row1.put("row1", "小明");
        //第一组的第二个子项
        Map<String,String> row2=new HashMap<String, String>();
        row2.put("row1", "小花");
        Map<String,String> row3=new HashMap<String, String>();
        row3.put("row1", "小草");
        listRow1.add(row1);
        listRow1.add(row2);
        listRow1.add(row3);
        
        //第二组子项
        List<Map<String, String>> listRow2=new ArrayList<Map<String,String>>();
        Map<String,String> row4=new HashMap<String, String>();
        row4.put("row1", "大明");
        Map<String,String> row5=new HashMap<String, String>();
        row5.put("row1", "大华");
        Map<String,String> row6=new HashMap<String, String>();
        row6.put("row1", "大曹");
        listRow2.add(row4);
        listRow2.add(row5);
        listRow2.add(row6);
        
        //将子项添加到list
        List<List<Map<String, String>>> row=new ArrayList<List<Map<String,String>>>();
        row.add(listRow1);
        row.add(listRow2);
        //创建适配器
        //this,listGroup:组的数据,R.layout.lanmu:组的布局文件,new String[]{"group1"}:组对象的key,new int[]{R.id.txt1}:组样式,row:子项的数据,R.layout.lanmu:子项的布局文件,new String[]{"row1"}:子项的key,子项的样式
        SimpleExpandableListAdapter sela=new SimpleExpandableListAdapter(this, listGroup, R.layout.lanmu, new String[]{"group1"}, new int[]{R.id.txt1}, row, R.layout.lanmu,new String[]{"row1"}, new int[]{R.id.txt2});
        
        setListAdapter(sela);
        
        
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值