博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ExpandableListView的使用
阅读量:7195 次
发布时间:2019-06-29

本文共 3582 字,大约阅读时间需要 11 分钟。

ExpandableListView的使用

效果图

这里写图片描述

布局

初始化

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

填充数据

KongqwExpandableListviewAdapter kongqwExpandableListviewAdapter = new KongqwExpandableListviewAdapter(this);expandableListView.setAdapter(kongqwExpandableListviewAdapter);

Adapter

package com.example.kongqw.myapplication;import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseExpandableListAdapter;import android.widget.TextView;import java.util.ArrayList;/** * Created by kongqw on 2015/12/21. */public class KongqwExpandableListviewAdapter extends BaseExpandableListAdapter {
private Context mContext; private ArrayList
mGroups; private ArrayList
mChilds; // 构造方法 public KongqwExpandableListviewAdapter(Context context) { mContext = context; // 模拟初始化数据 mGroups = new ArrayList
(); mGroups.add("Group 1"); mGroups.add("Group 2"); mGroups.add("Group 3"); mGroups.add("Group 4"); mGroups.add("Group 5"); mChilds = new ArrayList
(); mChilds.add("Child 1"); mChilds.add("Child 2"); mChilds.add("Child 3"); mChilds.add("Child 4"); mChilds.add("Child 5"); mChilds.add("Child 6"); mChilds.add("Child 7"); mChilds.add("Child 8"); mChilds.add("Child 9"); mChilds.add("Child 10"); } @Override public int getGroupCount() { return mGroups.size(); } @Override public int getChildrenCount(int groupPosition) { return mChilds.size(); } @Override public Object getGroup(int groupPosition) { return mGroups.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return mChilds.get(childPosition); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { View view = View.inflate(mContext, R.layout.expandable_group_item, null); TextView textView = (TextView) view.findViewById(R.id.group_item); textView.setText(mGroups.get(groupPosition)); return view; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { View view = View.inflate(mContext, R.layout.expandable_child_item, null); TextView textView = (TextView) view.findViewById(R.id.child_item); textView.setText(mChilds.get(childPosition)); return view; } @Override public boolean hasStableIds() { return true; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; }}

去掉箭头

expandableListView.setGroupIndicator(null);

默认展开

// 设置ExpandableListView默认是展开的for (int i = 0; i < kongqwExpandableListviewAdapter.getGroupCount(); i++) {    expandableListView.expandGroup(i);}

Group不可点击

expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {    @Override    public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {        // TODO Auto-generated method stub        return true;    }});

TODO 复用

转载于:https://www.cnblogs.com/sesexxoo/p/6190479.html

你可能感兴趣的文章
【JAVA练习】- 接收三个班各四个学员的成绩,算平均分
查看>>
Python3新特性 类型注解 以及 点点点
查看>>
【解决】node的环境变量
查看>>
Can’t connect to local MySQL server through socket的解决方法
查看>>
<input type="file">上传文件
查看>>
图论总结
查看>>
找倍数(回溯)
查看>>
JS执行模糊搜索
查看>>
重学JAVA基础(六):多线程的同步
查看>>
十天冲刺之八
查看>>
Arrays.asList 为什么不能 add 或者 remove 而 ArrayList 可以
查看>>
python GUI初步
查看>>
openstack4j接口调试
查看>>
内核分析阅读笔记
查看>>
安卓手机当Transmission下载机、FTP、要点总结
查看>>
移动端无缝滚动兼拖动插件
查看>>
PyQt5学习笔记-从主窗体打开一个子窗体
查看>>
English 好的报纸
查看>>
CMS 01
查看>>
NSValue&NSNumber
查看>>