首先要明确,可折叠列表在每个项是包含子项的,那么肯定会用到集合嵌套!
下面是封装的两个实体类:

package com.yx.pojo;

public class Chid {

        private int img;
        private String txt;

        public int getImg() {
                return img;
        }
public void setImg(int img) {
                this.img = img;
        }
public String getTxt() {
                return txt;
        }
public void setTxt(String txt) {
                this.txt = txt;
        }

}

package com.yx.pojo;

import java.util.ArrayList;
import java.util.List;

public class Group {
        private int img;
        private String txt;
        private List<Chid> list_child = new ArrayList<Chid>();

        public List<Chid> getList_child() {
                return list_child;
        }

        public void setList_child(List<Chid> list_child) {
                this.list_child = list_child;
        }

        public int getImg() {
                return img;
        }

        public void setImg(int img) {
                this.img = img;
        }

        public String getTxt() {
                return txt;
        }

        public void setTxt(String txt) {
                this.txt = txt;
        }

}

下面,既然是适配器,那一定要有适配器组件,本次用到的是ExpandableListView ,即在布局文件中添加该控件,简单设置width height id 即可

在activity文件中,获取适配器组件并绑定适配器!

package com.yx.android_day1004;

import java.util.ArrayList;
import java.util.List;

import com.example.android_day1004.R;
import com.yx.adapter.MyExpandAdapter;
import com.yx.pojo.Chid;
import com.yx.pojo.Group;

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

public class SecondActivity extends Activity {

        private static final String TAG = "tag";
        // 准备数据
        private List<Group> list_data;
        private ExpandableListView mExpandList;

        private int[] images = { R.drawable.headimage01, R.drawable.headimage02, R.drawable.headimage03 };

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_second);

                initData();
                intiView();
        }

        private void intiView() {
                mExpandList = (ExpandableListView) findViewById(R.id.expanded_list);
                MyExpandAdapter adapter = new MyExpandAdapter(this, list_data);
                mExpandList.setAdapter(adapter);
        }

        private void initData() {
                list_data = new ArrayList<Group>();
                for (int i = 0; i < 5; i++) {
                        Group group = new Group();
                        group.setImg(R.drawable.ic_launcher);
                        group.setTxt("组" + (i + 1));
                        List<Chid> child = new ArrayList<Chid>();
                        for (int j = 0; j < 3; j++) {
                                Chid chid = new Chid();
                                chid.setImg(images[j]);
                                chid.setTxt("子" + (j + 1));
                                child.add(chid);
                        }
                        group.getList_child().addAll(child);
                        list_data.add(group);
                }
        }
}

下面就是最重要的自定义适配器的部分啦。。。。。。。。。

package com.yx.adapter;

import java.util.List;

import com.example.android_day1004.R;
import com.yx.pojo.Group;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
* Created by Administrator on 2016/10/4.
*/
public class MyExpandAdapter extends BaseExpandableListAdapter {
        private List<Group> list_data;
        private Context context;

        public MyExpandAdapter(Context context, List<Group> list_data) {
                this.context = context;
                this.list_data = list_data;
        }

        @Override
        public int getGroupCount() {
                return list_data.size();
        }

        @Override
        public int getChildrenCount(int groupPosition) {

                return list_data.get(groupPosition).getList_child().size();
        }

        @Override
        public Object getGroup(int groupPosition) {
                return list_data.get(groupPosition);
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
                return list_data.get(groupPosition).getList_child().get(childPosition);
        }

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

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

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

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
                HolderGroup group = null;
                if (convertView == null) {
                        convertView = LayoutInflater.from(context).inflate(R.layout.activity_second_group_item, null);
                        group = new HolderGroup(convertView);
                        convertView.setTag(group);
                } else {
                        group = (HolderGroup) convertView.getTag();
                }
                group.img.setImageResource(list_data.get(groupPosition).getImg());
                group.tv.setText(list_data.get(groupPosition).getTxt());

                return convertView;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
                        ViewGroup parent) {
                HolderChild child = null;
                if (convertView == null) {
                        convertView = LayoutInflater.from(context).inflate(R.layout.activity_second_child_item, null);
                        child = new HolderChild(convertView);
                        convertView.setTag(child);
                } else {
                        child = (HolderChild) convertView.getTag();
                }
                child.img.setImageResource(list_data.get(groupPosition).getList_child().get(childPosition).getImg());

                child.tv.setText(list_data.get(groupPosition).getList_child().get(childPosition).getTxt());

                return convertView;
        }

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

        class HolderGroup {
                ImageView img;
                TextView tv;

                public HolderGroup(View convertView) {
                        img = (ImageView) convertView.findViewById(R.id.img_second_group_item);
                        tv = (TextView) convertView.findViewById(R.id.tv_second_group_item);
                }
        }

        class HolderChild {
                ImageView img;
                TextView tv;

                public HolderChild(View convertView) {
                        img = (ImageView) convertView.findViewById(R.id.img_second_child_item);
                        tv = (TextView) convertView.findViewById(R.id.tv_second_child_item);
                }
        }
}

本文中,一共有两个小细节需要注意
1、Group中的List一定要记得new出来,不然在用的时候会报空指针异常,然后又各种找不到就惨啦。。。。。
2、由于我们的ExpandableListView控件默认会在组上面加一个小箭头,记得让你的组布局项向右移一下,给它留出空间,不然不然。。丑的话自己忍。。。

en ,效果在这。。

总之,这个适配器最难的地方就是需要嵌套,保持思路清晰并不是特别难。ok

关于BaseExpandableListAdapter的更多相关文章

  1. Android ExpandableListView BaseExpandableListAdapter (类似QQ分组列表)

    分组列表视图(ExpandableListView) 和ListView不同的是它是一个两级的滚动列表视图,每一个组可以展开,显示一些子项,类似于QQ列表,这些项目来至于ExpandableListA ...

  2. Android开发之ExpandableListView扩展(BaseExpandableListAdapter的使用)(完整版)

    Android开发之ExpandableListView扩展(BaseExpandableListAdapter的使用)(完整版)

  3. android 三级菜单 BaseExpandableListAdapter

    在网上搜了非常长时间.没有找到合适的Android三级菜单.所以就自己动手写了一个,主要使用了BaseExpandableList来实现,通过三个布局文件来完毕相应的菜单项,详细实现请參照下图. wa ...

  4. [Android]BaseExpandableListAdapter实现可折叠列表

    使用BaseExpandableListAdapter 以实现的可折叠的所谓列表,例如QQ朋友们在分组功能. 基于BaseExpandableListAdapter扩大ExpandableList说明 ...

  5. Android之ExpandableList扩展用法(基于BaseExpandableListAdapter)

    1.简介 基于基于BaseExpandableListAdapter扩展的ExpandableList用法,现在网上流行的主要有两种:第一种是向BaseExpandableListAdapter传入两 ...

  6. ExpandableListView之BaseExpandableListAdapter

    之前使用的SimpleExpandableListAdapter有较大局限性,样式单一,修改难度大,这里不建议使用,而是利用BaseExpandableListAdapter,其实SimpleExpa ...

  7. Android Bug BaseExpandableListAdapter, getChildView

    @Override public View getChildView(final int groupPosition, final int childPosition, boolean isLastC ...

  8. Android中使用ExpandableListView实现微信通讯录界面(完善仿微信APP)

    之前的博文<Android中使用ExpandableListView实现好友分组>我简单介绍了使用ExpandableListView实现简单的好友分组功能,今天我们针对之前的所做的仿微信 ...

  9. Android中使用ExpandableListView实现好友分组

    一个视图显示垂直滚动两级列表中的条目.这不同于列表视图,允许两个层次,类似于QQ的好友分组.要实现这个效果的整体思路为: 1.要给ExpandableListView 设置适配器,那么必须先设置数据源 ...

随机推荐

  1. Ubuntu 14.04下Django+MySQL安装部署全过程

    一.简要步骤.(Ubuntu14.04) Python安装 Django Mysql的安装与配置 记录一下我的部署过程,也方便一些有需要的童鞋,大神勿喷~ 二.Python的安装 由于博主使用的环境是 ...

  2. HTML元素事件

    事件触发模型 简要说明 onclick 鼠标单击链接 ondbclick 鼠标双击链接 onmousedown 鼠标在链接的位置按下 onmmouseout 鼠标移出链接所在的位置 onmouseov ...

  3. SQLServer查询所有库表结构信息

    1.查询数据库中的所有数据库名: SELECT Name FROM Master..SysDatabases ORDER BY Name 2.查询某个数据库中所有的表名: SELECT Name FR ...

  4. Spring Boot+Cloud RestTemplate 调用IP或域名

    在SpringBoot+Cloud的项目中,我们使用了自动配置的OAuth2RestTemplate,RestTemplate,但是在使用这些restTemplate的时候,url必须是服务的名称,如 ...

  5. C++对象模型详解

    原文链接:吴秦大神的C++对象模型. 何为C++对象模型? C++对象模型可以概括为以下2部分: 1.语言中直接支持面向对象程序设计的部分: 2.对于各种支持的底层实现机制. 语言中直接支持面向对象程 ...

  6. archlinux配置答疑

    Q: chinese can not appear in my firefox and terminal rightly A: pacman -S wqy-microhei Q: install pi ...

  7. 第六百零六天 how can I 坚持(应该是六百零六天吧)

    找了个考研的借口,也是挺逗的,终于结束了,而且考的很渣. 最近发生了很多事,很快就要离开泛华了,放弃安逸,开始改变吧,其实感觉自己内心挺怂的,很怕改变,哎,这不像是有梦想,能成事的人应该有的. 还是想 ...

  8. PHP连接MySQL的时候报错SQLSTATE[HY000] [2002] No such file or directory

    错误环境:Mac OS 10.10 找到mysql.sock文件的位置 $sudo find / -name mysql.sock ------结果如下---------- find: /dev/fd ...

  9. Haproxy日志配置

    haproxy在默认情况不会记录日志,除了在haproxy.conf中的global段指定日志的输出外,还需要配置系统日志的配置文件.下面以centos6.4为例,haproxy使用系统自带的rpm报 ...

  10. git和github

    GIT是一款分布式版本控制系统.与SVN相比可以不依赖网络,并且对分支和合并有更好的支持.但是命令稍微复杂一些,这里简单介绍使用git将项目上传到github. 首先GIT安装只需要去官网下载安装即可 ...