全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

ExpandableListView实现手风琴效果

本文实例为大家分享了ExpandableListView实现手风琴效果的具体代码,供大家参考,具体内容如下

1. 效果示例图

2. 创建方法

(1)第一种方法与ListView等普通控件一样,直接在布局文件中添加ExpandableListView控件即可。

(2)第二种方法则是创建一个Activity继承自ExpandableListActivity,而后通过getExpandableListView()方法可获得一个ExpandableListView对象。

第二种方法仅适用于一个页面中只有一个ExpandableListView的情况。继承的Activity不需要再调用setContentView()方法,在ExpandableListActivity中已经关联了一个系统定义的布局文件。

3. 部分属性和点击事件

android:groupIndicator、android:childIndicator:组条目和子条目前面的图标,默认值为箭头,可设置自定义图片资源。若不显示该图标,则设置为@null。

android:divider、android:childDivider:组和子条目的分隔线。

ExpandableListView的点击事件有两个,分别对应组和子条目的点击事件:

设置组的点击事件:setOnGroupClickListener(OnGroupClickListener listener)

设置子条目的点击事件:setOnChildClickListener(OnChildClickListener listener)

5. 适配器

根据数据源的不同,可使用的适配器有两个:BaseExpandableListAdapter和CursorTreeAdapter,其中,CursorTreeAdapter用于数据源为Cursor对象的情况下,其它情况则使用BaseExpandableListAdapter。

(1)BaseExpandableListAdapter需要重写的方法:

getGroup():从数据源中获取组的数据内容。

getGroupCount():获取组的总数。

getGroupId():获取组的ID。

getGroupView():获取组的视图。

getChild():从数据源中获取子条目的内容。

getChildCount():获取指定组中的子条目总数,并非全部的子条目。

getChildId():获取子条目的ID。

getChildView():获取子条目的视图

hasStableIds():判断id对应的条目是否已经绘制,用于优化列表。

isChildSelectable():子条目是否允许点击,若返回false,则子条目点击事件无效。

(2)CursorTreeAdapter需要重写的方法:

CursorTreeAdapter():构造方法传入组的Cursor对象。

getChildrenCursor():传入组的Cursor对象,获取相应的组的子条目的Cursor对象。

newGroupView():创建组的视图,返回一个新的视图。

bindGroupView():在这里绑定组视图的数据内容,第一个参数即newGroupView()方法的返回值。

newChildView():创建子条目的视图。

bindChildView():绑定子条目视图的数据内容。

6. 简单范例

实现效果图中的例子。

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.studying.expandablelistviewdemo.MainActivity">

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

</LinearLayout>

Activity:

public class MainActivity extends Activity {

  private ExpandableListView elv;

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

    elv = (ExpandableListView) findViewById(R.id.elv_local_data);
    MyBaseExpandableListAdapter adapter = new MyBaseExpandableListAdapter(this, LoadData.getGroupData(), LoadData.getChildData());
    elv.setAdapter(adapter);
  }
}

加载测试数据用的工具类:

public class LoadData {

  // 组的数据内容
  public static List<String> getGroupData() {
    List<String> groupDataList = new ArrayList<>();
    groupDataList.add("计算机基础");
    groupDataList.add("安卓开发");
    return groupDataList;
  }

  // 子条目的数据内容
  public static List<List<String>> getChildData() {
    List<List<String>> childDataList = new ArrayList<>();

    List<String> group1 = new ArrayList<>();
    group1.add("数据结构");
    group1.add("算法");
    group1.add("计算机网络");
    childDataList.add(group1);

    List<String> group2 = new ArrayList<>();
    group2.add("控件使用");
    group2.add("网络操作");
    group2.add("数据存储");
    group2.add("四大组件");
    childDataList.add(group2);

    return childDataList;
  }
}

适配器:

public class MyBaseExpandableListAdapter extends BaseExpandableListAdapter {

  private Context mContext;

  private List<String> groupName;
  private List<List<String>> childName;

  public MyBaseExpandableListAdapter(Context mContext, List<String> groupName, List<List<String>> childName) {
    this.mContext = mContext;
    this.groupName = groupName;
    this.childName = childName;
  }

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

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

  @Override
  public String getGroup(int groupPosition) {
    return groupName.get(groupPosition);
  }

  @Override
  public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
  
    convertView = View.inflate(mContext, R.layout.item_group_name, null);

    TextView groupName = (TextView) convertView.findViewById(R.id.group_name);
    groupName.setText(getGroup(groupPosition));

    return convertView;
  }

  @Override
  public int getChildrenCount(int groupPosition) {
    return childName.get(groupPosition).size();
  }

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

  @Override
  public String getChild(int groupPosition, int childPosition) {
    return childName.get(groupPosition).get(childPosition);
  }

  @Override
  public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
  
    convertView = View.inflate(mContext, R.layout.item_child_name, null);

    TextView childName = (TextView) convertView.findViewById(R.id.child_name);
    childName.setText(getChild(groupPosition, childPosition));

    return convertView;
  }

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

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


# ExpandableListView  # 手风琴  # Android之带group指示器的ExpandableListView(自写)  # Android ExpandableListView展开列表控件使用实例  # Android ExpandableListView长按事件的完美解决办法  # Android之IphoneTreeView带组指示器的ExpandableListView效果  # Android UI控件ExpandableListView基本用法详解  # Android中ExpandableListView的用法实例  # Android 关于ExpandableListView刷新问题的解决方法  # android使用ExpandableListView控件实现小说目录效果的例子  # Android改变ExpandableListView的indicator图标实现方法  # Android 关于ExpandableListView去掉里头分割线的方法  # 种方法  # 重写  # 有两个  # 在这里  # 第一个  # 则是  # 适用于  # 数据结构  # 自定义  # 只有一个  # 不需  # 设置为  # 大家分享  # 若不  # 绑定  # 创建一个  # 图中  # 具体内容  # 值为  # 要再 


相关文章: 深圳网站制作的公司有哪些,dido官方网站?  如何快速搭建个人网站并优化SEO?  如何选择适配移动端的WAP自助建站平台?  如何选择美橙互联多站合一建站方案?  香港服务器建站指南:免备案优势与SEO优化技巧全解析  如何用y主机助手快速搭建网站?  PHP正则匹配日期和时间(时间戳转换)的实例代码  香港服务器选型指南:免备案配置与高效建站方案解析  常州企业建站如何选择最佳模板?  昆明高端网站制作公司,昆明公租房申请网上登录入口?  ,石家庄四十八中学官网?  保定网站制作方案定制,保定招聘的渠道有哪些?找工作的人一般都去哪里看招聘信息?  详解免费开源的DotNet二维码操作组件ThoughtWorks.QRCode(.NET组件介绍之四)  大同网页,大同瑞慈医院官网?  微信小程序 input输入框控件详解及实例(多种示例)  Android使用GridView实现日历的简单功能  建站之星会员如何解锁更多建站功能?  建站主机SSH密钥生成步骤及常见问题解答?  如何选择CMS系统实现快速建站与SEO优化?  中山网站制作网页,中山新生登记系统登记流程?  新网站制作渠道有哪些,跪求一个无线渠道比较强的小说网站,我要发表小说?  如何通过IIS搭建网站并配置访问权限?  如何通过商城免费建站系统源码自定义网站主题?  山东网站制作公司有哪些,山东大源集团官网?  如何通过云梦建站系统实现SEO快速优化?  ,制作一个手机app网站要多少钱?  如何通过FTP空间快速搭建安全高效网站?  建站之星2.7模板:企业网站建设与h5定制设计专题  如何通过wdcp面板快速创建网站?  简单实现Android文件上传  Python如何创建带属性的XML节点  免费的流程图制作网站有哪些,2025年教师初级职称申报网上流程?  模具网站制作流程,如何找模具客户?  详解jQuery中基本的动画方法  ,交易猫的商品怎么发布到网站上去?  如何在景安服务器上快速搭建个人网站?  如何在Golang中指定模块版本_使用go.mod控制版本号  深圳网站制作公司好吗,在深圳找工作哪个网站最好啊?  重庆市网站制作公司,重庆招聘网站哪个好?  定制建站模板如何实现SEO优化与智能系统配置?18字教程  SAX解析器是什么,它与DOM在处理大型XML文件时有何不同?  c++ stringstream用法详解_c++字符串与数字转换利器  在线ppt制作网站有哪些软件,如何把网页的内容做成ppt?  网站网页制作专业公司,怎样制作自己的网页?  手机钓鱼网站怎么制作视频,怎样拦截钓鱼网站。怎么办?  香港服务器网站推广:SEO优化与外贸独立站搭建策略  建站之星下载版如何获取与安装?  网站代码制作软件有哪些,如何生成自己网站的代码?  如何在搬瓦工VPS快速搭建网站?  网站制作说明怎么写,简述网页设计的流程并说明原因? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。