Android开发之ExpandableListView: 可展开的ListView

本文介绍了Android中ExpandableListView的使用方法,包括通过SimpleExpandableListAdapter快速实现数据展示及自定义适配器实现复杂布局。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 ExpandableListView: 可展开的ListView

         ListView 对于Android开发者来说是再熟悉不过的了,不过ListView的功能也有限。当需要展示第二层级数据时,使用ExpandableListView会更方便,下面是两个实例:


(一).
public class MainActivity extends ExpandableListActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /*
        这里不用setContentView()去加载布局,因为继承了类ExpandableListActivity        如果需要加载自定义布局,则不需继承类ExpandableListActivity        而在自定义布局文件里添加ExpandableListView等控件
         */

        //创建主List,用于存放第一层级数据
        List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
        //创建子List,用于存放第二层级数据
        List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();

        //向主List、子List中添加数据
        for (int i = 0; i < 5; i++) {
            //向主List中添加数据
            Map<String, String> groupMap = new HashMap<String, String>();
            groupMap.put("group", "group " + i);
            groupData.add(groupMap);

            //向子List中添加数据
            List<Map<String, String>> children = new ArrayList<Map<String, String>>();
            for (int j = 0; j < 3; j++) {
                Map<String, String> childMap = new HashMap<String, String>();
                childMap.put("child", "child " + j);
                children.add(childMap);
            }
            childData.add(children);
        }

        //利用SimpleExpandableListAdapter类构建设配器,可类比SimpleAdapter.
        ExpandableListAdapter mAdapter = new SimpleExpandableListAdapter(this,
                groupData,   //第一层级数据
                android.R.layout.simple_expandable_list_item_1,  //第一层级子布局,可自定义
                new String[]{"group"},   //键值
                new int[]{android.R.id.text1},   //第一层级子布局中的显示内容的控件Id
                childData,    //第二层级数据
                android.R.layout.simple_expandable_list_item_2,  //第二层级子布局,可自定义
                new String[]{"child"},    //键值
                new int[]{android.R.id.text1}  //第二层级子布局中的显示内容的控件Id
        );

        setListAdapter(mAdapter);


    }
}
效果图如下:
   
*以上只是简单地显示字符串,当然也可以自定义更复杂的布局,具体可参考SimpleAdapter


(二).
public class MainActivity1 extends Activity {
    ExpandableListView expandableListView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //与上一个例子不同的是这里加载了自定义布局
        setContentView(R.layout.activity_main);

        expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);

        MyAdapter madapter = new MyAdapter();
        expandableListView.setAdapter(madapter);


    }

    //自定义一个继承BaseExpandableListAdapter的设配器类,可类比BaseAdapter
    public class MyAdapter extends BaseExpandableListAdapter{
        TextView textView;
     //groupData为第一层级的数据,childData为第二层级的数据
        public  String [] groupData = {"name 1","name 2","name 3","name 4"};
        public  String [][] childData = {
                {"number 1_1","number 1_2"},
                {"number 2_1"},
                {"number 3_1","number 3_2","number 3_3"},
                {"number 4_1","number 4_2"}
        };


        @Override
        public int getGroupCount() {
            return groupData.length;
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return childData[groupPosition].length;
        }

        @Override
        public Object getGroup(int groupPosition) {
            return groupData[groupPosition];
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return childData[groupPosition][childPosition];
        }

        @Override
        public long getGroupId(int groupPosition) {
            return 0;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return 0;
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_item,null);
            textView = (TextView) view.findViewById(R.id.textView);
            textView.setText(groupData[groupPosition]);
            return view;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_item,null);
            textView = (TextView) view.findViewById(R.id.textView);
            textView.setText(childData[groupPosition][childPosition]);
            return view;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }
    }

}
效果图如下:
   

以上只是简单地显示字符串,当然也可以自定义更复杂的布局,具体可参考BaseAdapter

**第一次写博客,记录自己所学,若有不足之处,望指出。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值