Android基础:ExpandableListView

参考:
ExpandableListView总结
Android ExpandableListView使用小结(一)

简介

ExpandableListView可以分层次显示,用来显示两级列表。

效果图

这里写图片描述

简单使用

1 布局中使用

<ExpandableListView
    android:id="@+id/expandableListView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

2 准备数据:定义数组

String[] groupArray = new String[]{"西游记", "水浒传", "三国演义", "红楼梦"};
String[] childArray = new String[][]{{"唐三藏", "孙悟空", "猪八戒", "沙和尚"}, {"宋江", "林冲", "李逵", "鲁智深"}, {"曹操", "刘备", "孙权", "诸葛亮", "周瑜"}, {"贾宝玉", "林黛玉", "薛宝钗", "王熙凤"}};

3 设置适配器:adapter

ExpandableListView的适配器都是实现了接口ExpandableListAdapter,通常采用extends BaseExpandableListAdapter的方式。

expanListView.setAdapter(new MyExpandableAdapter());
方法含义
getGroupCount()组的数量
getChildrenCount(int i)分组数量
getGroup(int i)获取组内数据
getChild(int i, int i1)获取分组数据
getGroupId(int i)groupId必须唯一
getChildId(int i, int i1)childId必须唯一
hasStableIds()分组和子选项是否持有稳定的ID, 就是说底层数据的改变会不会影响到它们
getGroupView(int i, boolean b, View convertView, ViewGroup viewGroup)获取显示指定分组的视图
getChildView(int i, int i1, boolean b, View convertView, ViewGroup viewGroup)获取显示指定分组中的指定子选项的视图
isChildSelectable(int i, int i1)指定位置上的子元素是否可选中
private class MyExpandableAdapter extends BaseExpandableListAdapter {

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

    @Override
    public int getChildrenCount(int i) {
        return childArray[i].length;
    }

    @Override
    public Object getGroup(int i) {
        return groupArray[i];
    }

    @Override
    public Object getChild(int i, int i1) {
        return childArray[i][i1];
    }

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

    @Override
    public long getChildId(int i, int i1) {
        return 0;
    }

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

    @Override
    public View getGroupView(int i, boolean b, View convertView, ViewGroup viewGroup) {
        GroupViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(MainActivity.this, R.layout.item_group, null);
            holder = new GroupViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (GroupViewHolder) convertView.getTag();
        }
        holder.tv_group.setText(groupArray[i]);
        return convertView;
    }

    @Override
    public View getChildView(int i, int i1, boolean b, View convertView, ViewGroup viewGroup) {
        ChildViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(MainActivity.this, R.layout.item_child, null);
            holder = new ChildViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ChildViewHolder) convertView.getTag();
        }
        holder.tv_child.setText(childArray[i][i1]);
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }
}

public class GroupViewHolder {
    TextView tv_group;

    public GroupViewHolder(View convertView) {
        tv_group = (TextView) convertView.findViewById(R.id.tv_group);
    }
}

public class ChildViewHolder {
    TextView tv_child;

    public ChildViewHolder(View convertView) {
        tv_child = (TextView) convertView.findViewById(R.id.tv_child);
    }
}

4 监听

监听含义
setOnGroupClickListener(…)group的点击事件
setOnChildClickListener(…)child的点击事件
setOnGroupCollapseListener(…)group收缩的监听
setOnGroupExpandListener(…)group展开的监听
expanListView.setAdapter(new MyExpandableAdapter());
expanListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
    @Override
    public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
        ToastUtil.showShortToast(MainActivity.this, groupArray[i]);
        return false;//true/false都可以,但是true,不会展开group
    }
});
expanListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
    @Override
    public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {
        ToastUtil.showShortToast(MainActivity.this, childArray[i][i1]);
        return false;//true/false都可以,
    }
});
expanListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
    @Override
    public void onGroupCollapse(int i) {
        ToastUtil.showShortToast(MainActivity.this, groupArray[i]);
    }
});
expanListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
    @Override
    public void onGroupExpand(int i) {
        ToastUtil.showShortToast(MainActivity.this,  groupArray[i]);
    }
});

源码

https://git.oschina.net/AndroidUI/ExpandableListView

高级

1 怎么让ExpandableListView默认是展开的?

for (int i = 0; i < parentMapList.size(); i++) {
   expandableListView.expandGroup(i);
}

怎么把折叠图片放到右边?

这里写图片描述

准备2张图片,箭头朝上+箭头朝下。
自定义group的layout,右边放ImageView,根据isExpanded的值,判断设置哪张图片。

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
   ...

    if (isExpanded) {
        holder.iv_group.setImageResource(R.mipmap.arrow_down);
    }else{
        holder.iv_group.setImageResource(R.mipmap.arrow_up);
    }
    return convertView;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值