ExpandableListView 使用方式

来源:互联网 发布:网络图书馆 编辑:程序博客网 时间:2024/05/19 02:30

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




原创粉丝点击