1、实现的效果图

2、需要创建的.class和.xml文件:

   2.1实现的结构图如下

3、代码展示如下

   3.1 package com.example.bean

3.1.1 Chile.class类

public class Chile {    private String name;    public Chile(String name) {        super();        this.name = name;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Chile() {        super();        // TODO Auto-generated constructor stub    }    @Override    public String toString() {        return "Chile [name=" + name + "]";    }}


3.1.2  Group.class类如下;

import java.util.ArrayList;public class Group {    private String g_name;    private ArrayList data;    public String getG_name() {        return g_name;    }    public void setG_name(String g_name) {        this.g_name = g_name;    }    public ArrayList getData() {        return data;    }    public void setData(ArrayList data) {        this.data = data;    }    public Group(String g_name, ArrayList data) {        super();        this.g_name = g_name;        this.data = data;    }    public Group() {        super();    }}



3.2 package com.example.adapter包;

  3.2.1  Expand_Adapter.class适配器

import java.util.ArrayList;import com.example.bean.Chile;import com.example.bean.Group;import com.example.expand.R;import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseExpandableListAdapter;import android.widget.TextView;public class Expand_Adapter extends BaseExpandableListAdapter {    private Context context;    private ArrayList data;    public Expand_Adapter(Context context, ArrayList data) {        super();        this.context = context;        this.data = data;    }    @Override    public Object getChild(int groupPosition, int childPosition) {        // TODO Auto-generated method stub        return data.get(groupPosition).getData().get(childPosition);    }    @Override    public long getChildId(int groupPosition, int childPosition) {        // TODO Auto-generated method stub        return childPosition;    }    @Override    public View getChildView(int groupPosition, int childPosition,            boolean isLastChild, View convertView, ViewGroup parent) {        // TODO Auto-generated method stub        Chile chile = data.get(groupPosition).getData().get(childPosition);        View view = View.inflate(context, R.layout.expand_chile, null);        TextView textView = (TextView) view.findViewById(R.id.tv_chile);        textView.setText(chile.getName());        return view;    }    @Override    public int getChildrenCount(int groupPosition) {        // TODO Auto-generated method stub        return data.get(groupPosition).getData().size();    }    @Override    public Object getGroup(int groupPosition) {        // TODO Auto-generated method stub        return data.get(groupPosition);    }    @Override    public int getGroupCount() {        // TODO Auto-generated method stub        return data.size();    }    @Override    public long getGroupId(int groupPosition) {        // TODO Auto-generated method stub        return groupPosition;    }    @Override    public View getGroupView(int groupPosition, boolean isExpanded,            View convertView, ViewGroup parent) {        // TODO Auto-generated method stub        Group group = data.get(groupPosition);        View view = View.inflate(context, R.layout.expand_group, null);        TextView textView = (TextView) view.findViewById(R.id.tv_group);        textView.setText(group.getG_name());        return view;    }    @Override    public boolean hasStableIds() {        // TODO Auto-generated method stub        return false;    }    @Override    public boolean isChildSelectable(int groupPosition, int childPosition) {        // TODO Auto-generated method stub        return true;    }}





3.3 package com.example.expand;

    3.3.1 MainActivity.class代码如下:

import java.util.ArrayList;import com.example.adapter.Expand_Adapter;import com.example.bean.Chile;import com.example.bean.Group;import android.os.Bundle;import android.app.Activity;import android.widget.ExpandableListView;public class MainActivity extends Activity {    private ExpandableListView expandableListView;    private ArrayList data = new ArrayList();    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        expandableListView = (ExpandableListView) findViewById(R.id.expand);        // 数据准备        initData();        // 设置设配器        Expand_Adapter adapter = new Expand_Adapter(getApplicationContext(),                data);        expandableListView.setAdapter(adapter);    }    private void initData() {        // TODO Auto-generated method stub        ArrayList c_list = new ArrayList();        c_list.add(new Chile("李宝志"));        c_list.add(new Chile("李宝志"));        c_list.add(new Chile("李宝志"));        data.add(new Group("My_好友", c_list));        ArrayList c_list1 = new ArrayList();        c_list1.add(new Chile("李宝志"));        c_list1.add(new Chile("李宝志"));        c_list1.add(new Chile("李宝志"));        data.add(new Group("同学", c_list1));        ArrayList c_list2 = new ArrayList();        c_list2.add(new Chile("李宝志"));        c_list2.add(new Chile("李宝志"));        c_list2.add(new Chile("李宝志"));        data.add(new Group("小日本", c_list2));    }}




更多相关文章

  1. (4.1.2.6)Android(安卓)判断app是否在前台还是在后台运行
  2. Android定时任务的实现
  3. Android截图以及加水印Demo
  4. Android中3种方法实现back键动作
  5. Android(安卓)图片平铺效果
  6. Android(安卓)项目无法生成R文件解决办法
  7. 基于 eclipse 的 android 工程如何使用 jar 文件
  8. Android(安卓)Gallery3D效果 教程 案例 代码
  9. Android(安卓)textview实现删除线

随机推荐

  1. 如何写出让大厂面试官满意的字符串复制函
  2. 华为防火墙L2TP ***的配置
  3. 顺序栈(C语言,静态栈)
  4. Oracle Linux 7.9+Oracle 12c+ASM安装文
  5. 总结了几个Java锁的面试题,看你是否能融会
  6. 再议电商业务的复杂性
  7. markdown与Emmet插件的使用方式
  8. 课程表、用户注册及 css 预习
  9. 关于tp6多对多查询隐藏pivot字段
  10. 监控系统项目实施--安装与部署-- MySQL数