可展开的列表组件——ExpandableListView简单举例

可展开的列表组件——ExpandableListView简单举例

ExpandableListView是ListView的子类,它在普通的ListView的基础上进行了扩展,把应用中的列表项分成几组,每组里又包含多个列表项。

与Adapter类似的是,实现ExpandableListAdapter也有如下三种常用方式:
1. 扩展BaseExpandableListAdapter实现ExpandableAdapter
2. 使用SimpleExpandableListAdapter将两个List集合包装成ExpandableListAdapter。
3. 使SimpleCursorTreeAdapter将Cursor中的数据包装成SimpleCursorTreeAdapter。

以下是使用第一和第二种方式的例子。

一、扩展BaseExpandableListAdapter实现ExpandableAdapter
activity_main.xml文件:

<LinearLayout
    android:id="@+id/ll"
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical">
    <ExpandableListView 
        android:id="@+id/elv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

    </ExpandableListView>
</LinearLayout>

MainActivity文件

package com.example.expandablelistadapter;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //创建一个BaseExpandableListAdapter对象
        ExpandableListAdapter adapter = new BaseExpandableListAdapter() {
            int[] logos = new int[]{
                R.drawable.img02,
                R.drawable.img03,
                R.drawable.img04
            };
            private String[] armTypes = new String[]{
                    "神族兵种",
                    "虫族兵种",
                    "人族兵种"
            };
            private String[][] arms = new String[][]{
                    {"狂战士","龙骑士","大法师"},
                    {"疯狗","喷子","分龙","地刺"},
                    {"C4兵","医疗兵","隐形兵"}
            };
            //定义一个TextView控件
            private TextView getTextView(){
                AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 64);
                TextView textView = new TextView(MainActivity.this);
                textView.setLayoutParams(lp);
                textView.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);
                textView.setPadding(36, 0, 0, 0);
                textView.setTextSize(20);
                return textView;

            }

            @Override
            public boolean isChildSelectable(int groupPosition, int childPosition) {
                // TODO Auto-generated method stub
                return true;
            }

            @Override
            public boolean hasStableIds() {
                // TODO Auto-generated method stub
                return true;
            }

            @Override
            public View getGroupView(int groupPosition, boolean isExpanded,
                    View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                //新建一个LinearLayout控件
                LinearLayout ll = new LinearLayout(MainActivity.this);
                //垂直分布
                ll.setOrientation(0);
                //新建一个ImageView控件
                ImageView logo = new ImageView(MainActivity.this);
                logo.setImageResource(logos[groupPosition]);
                //将ImageView添加到LinearLayout
                ll.addView(logo);
                //新建一个TextView
                TextView textView = getTextView();
                textView.setText(getGroup(groupPosition).toString());
                //将TextVeiw添加到LinearLayout
                ll.addView(textView);
                return ll;
            }
            //获取第一级列表项的位置
            @Override
            public long getGroupId(int groupPosition) {
                // TODO Auto-generated method stub
                return groupPosition;
            }
            //获取第一级列表项的长度
            @Override
            public int getGroupCount() {
                // TODO Auto-generated method stub
                return armTypes.length;
            }
            //获取第一级列表项内容
            @Override
            public Object getGroup(int groupPosition) {
                // TODO Auto-generated method stub
                return armTypes[groupPosition];
            }
            //获取第二级列表项长度
            @Override
            public int getChildrenCount(int groupPosition) {
                // TODO Auto-generated method stub
                return arms[groupPosition].length;
            }

            @Override
            public View getChildView(int groupPosition, int childPosition,
                    boolean isLastChild, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                TextView textView = getTextView();
                textView.setText(getChild(groupPosition, childPosition).toString());

                return textView;
            }

            @Override
            public long getChildId(int groupPosition, int childPosition) {
                // TODO Auto-generated method stub
                return childPosition;
            }

            @Override
            public Object getChild(int groupPosition, int childPosition) {
                // TODO Auto-generated method stub
                return arms[groupPosition][childPosition];
            }
        };
      ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.elv);
      expandableListView.setAdapter(adapter);
    }

}

运行截图:
这里写图片描述

源码地址:

二、 使用SimpleExpandableListAdapter将两个List集合包装成ExpandableListAdapter。
expandlisttext01.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ExpandableListView 
        android:id="@+id/elv01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        ></ExpandableListView>
</LinearLayout>

groups.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:paddingLeft="20dp">
        <TextView
        android:id="@+id/textGroup"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingLeft="40px"
        android:paddingTop="6px"
        android:paddingBottom="6px"
        android:textSize="20sp"
        android:text="No data"
    />

</LinearLayout>

list_item_order.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    <!-- <ImageView android:id="@+id/img_od" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_margin="5px"/>-->
        <LinearLayout android:orientation="vertical"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content">
            <TextView android:id="@+id/title_od" 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                 />
            <TextView android:id="@+id/info_od"  
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                 />
        </LinearLayout>

</LinearLayout>

ExpandableListViewText文件:

package com.example.expandablelistadapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;

public class ExpandableListViewText extends Activity {

    private String[] name = new String[]{
            "绿亮电动车NO.0225","上海黄浦区电瓶车专卖店",
            "绿源电动车","依莱达电动车","上海尚品电动车",
            "上海赛峰电动车专卖","上海永久牌电力自行车LPG燃气助力车","凤凰电动车专营直销店"
    };
//  private int[] img = new int[]{
//          R.drawable.img02,R.drawable.img03,R.drawable.img04,
//          R.drawable.img05,R.drawable.img06,R.drawable.img07,
//          R.drawable.img08
//  };

    private String address[] = new String[]{
            "上海市黄浦区永寿路204号-临",
            "制造局路699",
            "黄浦区虎丘路95号",
            "上海市黄浦区",
            "徽宁路361号",
            "上海市黄浦区广西南路46号",
            "乔家路95-1附近",
            "中华路638"

    };
    List<Map<String, String>>groups = new ArrayList<Map<String,String>>();
    List<List<Map<String, String>>>childs = new ArrayList<List<Map<String,String>>>();
    ExpandableListView elv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expandlisttext01);

        Map<String, String>title1 = new HashMap<String, String>();
        Map<String, String>title2 = new HashMap<String, String>();
        title1.put("group", "2011-12-03");
        title2.put("group", "2011-12-05");
        groups.add(title1);
        groups.add(title2);

        List<Map<String, String>>child = new ArrayList<Map<String,String>>();
        Map<String, String>map;
        for (int i = 0; i < name.length; i++) {
            map = new HashMap<String, String>();
            map.put("address", address[i]);
            map.put("name", name[i]);
            child.add(map);
        }
        childs.add(child);
        childs.add(child);

        elv = (ExpandableListView) findViewById(R.id.elv01);
        SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(ExpandableListViewText.this,
                groups, R.layout.groups, new String[]{"group"}, new int[]{R.id.textGroup}, 
                childs, R.layout.list_item_order, new String[]{"name","address"}, new int[]{R.id.title_od,R.id.info_od});
        elv.setAdapter(adapter);
    }
}

运行截图:
这里写图片描述

以上是实现ExpandableListAdapter的两种方法

  1. 扩展BaseExpandableListAdapter实现ExpandableAdapter。
    下载地址:http://download.csdn.net/detail/u013293125/9459055
  2. 使用SimpleExpandableListAdapter将两个List集合包装成ExpandableListAdapter。
    下载地址:http://download.csdn.net/detail/u013293125/9459057
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值