展开列表控件ExpandableList,这里直接用一个简单的例子来介绍,先上效果图(默认为展开的列表):

先分析一下,应该是有两个布局,一个用于显示根,一个用显示子,显示根的布局命名为:list_item_expand_group.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="vertical"><RelativeLayoutandroid:id="@+id/group_layout"android:layout_width="fill_parent"android:layout_height="45dp"><TextViewandroid:id="@+id/group_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_centerInParent="true"android:textSize="@dimen/one_level_word"android:textColor="@color/word_color_999999"android:text="-"/><ImageViewandroid:id="@+id/group_state"android:layout_width="16dp"android:layout_height="12dp"android:layout_toRightOf="@+id/group_title"android:layout_centerInParent="true"android:layout_marginLeft="10dp"android:background="@drawable/group_down"/><TextViewandroid:id="@+id/group_num"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="0"android:layout_alignParentRight="true"android:layout_centerInParent="true"android:textSize="@dimen/one_level_word"android:textColor="@color/word_color_666666"/></RelativeLayout></LinearLayout>

显示子的布局命名为:list_item_expand_group_child.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="match_parent"android:background="@color/wirte_ffffff"><TextViewandroid:id="@+id/child_text"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="8dp"android:paddingLeft="50dp"android:text="test"android:textSize="@dimen/two_level_word"android:textColor="@color/word_color_666666"/>
</LinearLayout>

布局定义好了,接下来就是定义它的适配器,ExpanableList的适配器继承于BaseExpandableListAdapter,要显示两级我们需要两个列表,一个用于存根,一个用于存子,这里跟已经定了,是四种情况:迟到人数,请假人数,旷课人数和公假人数,但是子是需要传入的,然后分别要重写其getGroupView()获取根布局的方法和getChildView()获取子布局方法,适配器如下:


import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;import com.gta.zssx.R;
import com.gta.zssx.pub.util.LogUtil;import java.util.ArrayList;
import java.util.List;/*** Created by lan.zheng on 2016/6/24.*/
public class CheckExpandableListAdapter extends BaseExpandableListAdapter {public static int[] group_checked = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0};private String[] group_title_array = new String[] { "迟到人数", "请假人数","旷课人数","公假人数" };private List<String> groupArray;  //title,根数组private List<String> groupCount;  //是否显示箭头的标志private List<List<String>> childArray;  //child数组Context mContext;// 一级标签上的状态图片数据源int[] group_state_array = new int[] { R.drawable.group_down,R.drawable.group_up };public CheckExpandableListAdapter(Context context,String[] late,String[] leave,String[] truant,String[] holiday){mContext = context;groupCount = new ArrayList<>();groupCount.add(""+late.length);groupCount.add(""+leave.length);groupCount.add(""+truant.length);groupCount.add(""+holiday.length);//这里都把获得的根String变为ListgroupArray = new ArrayList<>();for(int index=0;index<group_title_array.length;index++){groupArray.add(group_title_array[index]);}//这里都把获得的子String变为ListchildArray = new ArrayList<>();List<String> childItem1 =new ArrayList<>();List<String> childItem2 =new ArrayList<>();List<String> childItem3 =new ArrayList<>();List<String> childItem4 =new ArrayList<>();for(int i=0;i<late.length;i++){childItem1.add(late[i]);}for(int i=0;i<leave.length;i++){childItem2.add(leave[i]);}for(int i=0;i<truant.length;i++){childItem3.add(truant[i]);}for(int i=0;i<holiday.length;i++){childItem4.add(holiday[i]);}childArray.add(childItem1);childArray.add(childItem2);childArray.add(childItem3);childArray.add(childItem4);}@Overridepublic int getGroupCount() {return groupArray.size();}@Overridepublic int getChildrenCount(int groupPosition) {return childArray.get(groupPosition).size();}@Overridepublic Object getGroup(int groupPosition) {return getGroup(groupPosition);}@Overridepublic Object getChild(int groupPosition, int childPosition) {return childArray.get(groupPosition).get(childPosition);}@Overridepublic long getGroupId(int groupPosition) {return groupPosition;}@Overridepublic long getChildId(int groupPosition, int childPosition) {return childPosition;}@Overridepublic boolean hasStableIds() {return true;}/*** 一级标签设置* @param groupPosition* @param isExpanded* @param convertView* @param parent* @return*/@Overridepublic View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {// 为视图对象指定布局convertView = LinearLayout.inflate(mContext,R.layout.list_item_expand_group, null);RelativeLayout myLayout = (RelativeLayout) convertView.findViewById(R.id.group_layout);/*myLayout.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {mListener.itemClick(group_checked);}});*//*** 声明视图上要显示的控件*/// 新建一个TextView对象,用来显示一级标签上的标题信息TextView group_title = (TextView) convertView.findViewById(R.id.group_title);// 新建一个TextView对象,用来显示一级标签上的大体描述的信息ImageView group_state = (ImageView) convertView.findViewById(R.id.group_state);TextView group_count = (TextView)convertView.findViewById(R.id.group_num) ;/*** 设置相应控件的内容*/// 设置标题上的文本信息group_title.setText(groupArray.get(groupPosition));// 设置整体描述上的文本信息if (group_checked[groupPosition] % 2 == 1) {// 设置默认的图片是选中状态group_state.setBackgroundResource(group_state_array[1]);myLayout.setBackgroundResource(R.drawable.text_item_expandable_list_bg);} else {for (int test : group_checked) {if (test == 0 || test % 2 == 0) {// 设置默认的图片是未选中状态group_state.setBackgroundResource(group_state_array[0]);myLayout.setBackgroundResource(R.drawable.text_item_top_bg);}}}//是否显示箭头和改变countif(groupCount.get(groupPosition).equals("0")){group_state.setVisibility(View.INVISIBLE);myLayout.setBackgroundResource(R.drawable.text_item_expandable_list_bg);}else {group_count.setText(groupCount.get(groupPosition));}// 返回一个布局对象return convertView;}/*** 对一级标签下的二级标签进行设置*/@Overridepublic View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {// 为视图对象指定布局convertView =  LinearLayout.inflate(mContext, R.layout.list_item_expand_group_child, null);/*** 声明视图上要显示的控件*/// 新建一个TextView对象,用来显示具体内容TextView child_text = (TextView) convertView.findViewById(R.id.child_text);/*** 设置相应控件的内容*/// 设置要显示的文本信息child_text.setText(childArray.get(groupPosition).get(childPosition));return convertView;}@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {return true;}public interface Listener {void itemClick(int[] group_checked);}
}

适配器OK,接下来我们也自定义一下我们的ExpandListView,让其在适配ScollView,不然会造成一些小问题( 类似问题点击这里):


import android.content.Context;
import android.util.AttributeSet;
import android.widget.ExpandableListView;/*** Created by lan.zheng on 2016/6/25.*/
public class MyExpandListView extends ExpandableListView{public MyExpandListView(Context context) {super(context);}public MyExpandListView(Context context, AttributeSet attrs) {super(context, attrs);}public MyExpandListView(Context context, AttributeSet attrs,int defStyle) {super(context, attrs, defStyle);}@Override/*** 重写该方法,达到使ListView适应ScrollView的效果*/protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,MeasureSpec.AT_MOST);super.onMeasure(widthMeasureSpec, expandSpec);}}

主函数的主要布局如下:

     <ScrollViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/wirte_ffffff"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:id="@+id/tv_course_name_show"android:layout_width="match_parent"android:layout_height="wrap_content"/><com.fun.view.MyExpandListViewandroid:id="@+id/expanded_menu"android:layout_width="match_parent"android:layout_height="wrap_content"android:childDivider="@color/wirte_ffffff"android:listSelector="#00000000"android:divider="@color/view_line"android:dividerHeight="0.5dp"android:scrollbars="none"></com.fun.view.MyExpandListView></RelativeLayout></ScrollView>

最后我们就要在主函数中使用啦:

private MyExpandListView expandableListView;private CheckExpandableListAdapter mExpandableListAdapter;String[] lateString;    //迟到,自行填充数据String[] leaveString;   //请假,自行填充数据String[] truantString;  //旷课,自行填充数据String[] holidayString; //公假,自行填充数据private TextView mCourseNameTextView; //用于获取焦点private void updateList(){mExpandableListAdapter = null;mExpandableListAdapter = new CheckExpandableListAdapter(this,lateString,leaveString,truantString,holidayString);expandableListView.setAdapter(mExpandableListAdapter);//默认为全部展开int groupCount = expandableListView.getCount();for (int i=0; i<groupCount; i++) {expandableListView.expandGroup(i);}//进行高度计算,为了备注的显示ViewGroup.LayoutParams lLayoutParams = expandableListView.getLayoutParams();lLayoutParams.height = getHeight();expandableListView.setLayoutParams(lLayoutParams);expandableListView.requestLayout();expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {@Overridepublic boolean onGroupClick(ExpandableListView parent, View v,int groupPosition, long id) {CheckExpandableListAdapter.group_checked[groupPosition] = CheckExpandableListAdapter.group_checked[groupPosition]+1;// 刷新界面mExpandableListAdapter.notifyDataSetChanged();return false;}});}

这就完事了?你运行发现似乎还有个小问题,列虽然能显示,但是当数据多了的时候,显示的并不是从头开始的数据,而是列表中间,我们需要获取焦点,让他定位在列表头部开始显示,布局中的TextView在这里发挥了作用,虽然是空的,但是我们可以利用它进行获取焦点,在上面的方法中最后加入这几句即可:

//设置焦点为顶部,因为列表为默认展开,所以需要适配回顶端的标题显示,而不是列表的中间处
        mCourseNameTextView.setFocusable(true);
        mCourseNameTextView.setFocusableInTouchMode(true);
        mCourseNameTextView.requestFocus();

OK,现在我们就完成了这控件的显示,更多的花样你们可以自己尝试一下~~

简单的ExpandableList控件例子相关推荐

  1. 【WPF】一个简单的ColorPicker控件

    在斯克迪亚看到一篇WPF动态改变主题颜色的文章,来了兴趣,于是自己搞了个简单的ColorPicker控件. 控件其实很简单,定义了5个依赖属性 FinalBrushProperty, APropert ...

  2. 通过最简单的button控件,深入学习SAP UI5框架代码系列之零

    Jerry曾经作为SAP成都研究院的Fiori应用开发人员,从事了将近3年的SAP CRM Fiori应用开发,在使用SAP UI5的过程中,遇到过形形色色的问题,不少都是通过调试SAP UI5框架代 ...

  3. 如何在Android实现桌面清理内存简单Widget小控件

    如何在Android实现桌面清理内存简单Widget小控件 我们经常会看到类似于360.金山手机卫士一类的软件会带一个widget小控件,显示在桌面上,上面会显示现有内存大小,然后会带一个按键功能来一 ...

  4. 【写笔记】WPF 自定义简单的TextBox控件

    效果图: 笔记分享: https://www.yuque.com/docs/share/4aac743b-1ff0-42d2-9ec3-c605c145a58e?# <WPF 自定义简单的Tex ...

  5. 【jquery模仿net控件】简单的datalist控件更新,及其简单应用

    接上次的帖子:http://www.cnblogs.com/yexiaochai/archive/2012/01/22/2328729.html 简单的更新了一下代码,主要针对datalist,这次主 ...

  6. Qt 实现自定义Ui控件例子,以自定义的Slider为例(QWidget)

    说明 Qt可以比较方便地实现自定义控件在Qt Creator中使用.网上也有很多大神的控件可以使用,但是如果想要自己简单定制也可以按照这个流程. 本文的要点: [1]如何实现一个自定义控件? 本文使用 ...

  7. (原创)自已实现服务器控件 之 简单的Label控件

    标题:自已实现服务器控件之Label控件 声明:本帖只是一个测试Demo,所以,不会写得太规范,也不会考虑到安全性.以方便为  主.所以,用得到的朋友在项目中使用的时候,还希望对其进行改进. 环境 开 ...

  8. 注册ActiveX控件简单方法及控件未被正确授权解决方案

    最近编程的时候发现有些电脑上部分控件没有被注册,先把注册ActiveX控件的方法简单记录一下: 1.首先要确定你要缺少什么控件,需要注册: 2.在http://www.dll1.cn/ 下载相应的控件 ...

  9. 翻翻git之---一个简单的标签控件 LabelView (随手发了两张小宝宝的玩耍照)

    转载请注明出处:王亟亟的大牛之路 P1:这部分为废话技术内容在P2,不想看的可跳过 最近每天都是在照顾鱼,麦麦,当当之间游离回家几乎没学习,没敲代码,或者说没开工作电脑,慢慢的罪恶感,贴两张周末小朋友 ...

最新文章

  1. Win7环境下IPython Notebook的安装
  2. PowerShell_9_零基础自学课程_9_高级主题:静态类和类的操作
  3. java.lang.IllegalArgumentException: MALFORMED jar解析中文报错问题
  4. 登录验证---添加验证码验证,Cookie保存功能
  5. js中的继承1--类继承
  6. python3安装setuptools步骤_python在Windows下安装setuptools(easy_install工具)步骤详解
  7. linux解析器错误权限不够,实例解析Linux下目录的权限
  8. 刘明计算机学院,西南大学计算机与信息科学学院研究生导师简介-刘明
  9. Oracle数据库的使用(学习)
  10. 20135202闫佳歆——家庭作业汇总
  11. 谷歌云没有信用卡怎么注册服务器,谷歌云免费服务器申请方法
  12. 台式机与笔记本辐射谁大
  13. 高仿精仿微信客户端源码完整版
  14. 【高并发】- 指标介绍
  15. 免费申请Jetbrains的产品
  16. 【图像处理-计算机视觉学习路线】个人记录
  17. 我的世界服务器php插件制作教程,我的世界服务器citizen公民插件使用教程
  18. 5-1 N个数求和 (20分)
  19. 微信IFTTT,用微信同步照片或信息到facebook和twitter
  20. 基于Word的图文试题库系统(一)

热门文章

  1. android iphone 记事本,iPhone上有哪些记事本软件足够你记录生活中的细节?
  2. 如何给音频降噪?这些方法快收藏起来
  3. 闲鱼 12 块买的软件资源!禁止贩卖赚钱
  4. Linux scp 远程复制
  5. 从无到有的基于QT软件的DIY桌面番茄钟(上)
  6. 西门子840D数控机床使用C++DDE技术连接DHServer|Result实现NC程序的管理
  7. 计算机范围广是由于,计算机自动化程度高、应用范围广是由于
  8. 2020 年 “联想杯”全国高校程序设计在线邀请赛暨第三届上海理工大学程序设计竞赛热身赛B. 回文串
  9. Hybrid接口的用法
  10. 从零开始搭建自己的LNMP环境