ExpandableListView简单用法

2023-12-16 15:08

本文主要是介绍ExpandableListView简单用法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、ExpandableListView

1.简单介绍

       ExpandableListView是一个垂直滚动的显示两个级别(group,child)列表项的视图。列表项来自ExpandableListAdapter,gourp可以单独展开。

2.常用方法

      expandGroup(int groupPos):在分组列表视图中展开一组

     setSelectedGroup(int groupPosition):设置选择指定的组

    setSelectedChild(ing groupPosition,int childPosition,boolean shouldExpandGroup):设置选择指定的子项

    getPackedPositionGroup(long packedPosition):返回所选择的组

    getPackedPositionForChild(ing groupPosition,int childPosition):返回所选择的子项

    getPackedPositionType(long packedPosition):返回所选择的类型(group.child)

    isGroupExpanded(int groupPosition):判断此组是否展开

    ExpandableListContextMenuInfo info = (ExpandableListMenuInfo)item.getMenuInfo();

    String title = ((TextView)info.targetView).getText().toString();

    int type = ExpandableListView.getPackedPositionType(info.packedPosition);

    if(type == ExpandableListView.PACKED_POSITION_TYPE_CHILD){

        int gourpPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);

        int childPos = ExpandableListView.getPackedPositiionChild(info.packedPosition);

    }

二、ExpandableListAdapter

    一个接口,将基础数据链接到ExpandableListView.此接口的实现将提供访问child项的数据(由group分类)并实例化child和group。

三、简单实现代码

main.xml:定义Activity的界面布局:
<?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="fill_parent"
    >
<ExpandableListView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/mExpandableListView"
    />
</LinearLayout>


group.xml:定义一级列表的布局方式:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation = "horizontal"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <TextView
  android:id = "@+id/group_tv"
  android:layout_width = "match_parent"
  android:layout_height = "match_parent"
  android:paddingLeft = "60px"
  android:paddingTop = "10px"
  android:paddingBottom = "10px"
  android:textSize = "26sp"/>
</LinearLayout>

 

child.xml:定义二级列表的布局方式:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation = "horizontal"
 android:layout_width="match_parent"
 android:layout_height="match_parent">
 <ImageView
  android:layout_gravity = "center_vertical"
  android:id = "@+id/child_iv"
  android:layout_width = "70px"
  android:layout_height = "70px"
  android:paddingLeft = "30px"
  android:paddingTop = "2px"
  android:paddingBottom = "5px"
  android:src = "@drawable/icon"/>
 <TextView
  android:layout_gravity = "center_vertical"
  android:id = "@+id/child_tv"
  android:layout_width = "fill_parent"
  android:layout_height = "fill_parent"
  android:paddingLeft = "30px"
  android:paddingTop = "10px"
  android:paddingBottom = "5px"
  android:textSize = "30sp"/>
</LinearLayout>
   

Activity代码:

package test.expandableListView;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TestExpandableListView extends Activity
{
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  ExpandableListView elv = (ExpandableListView)findViewById(R.id.mExpandableListView);

  //准备一级列表中显示的数据:2个一级列表,分别显示"group1"和"group2"
  List<Map<String, String>> groups = new ArrayList<Map<String, String>>();
  Map<String, String> group1 = new HashMap<String, String>();
  group1.put("group", "group1");
  Map<String, String> group2 = new HashMap<String, String>();
  group2.put("group", "group2");
  groups.add(group1);
  groups.add(group2);

  //准备第一个一级列表中的二级列表数据:两个二级列表,分别显示"childData1"和"childData2"
  List<Map<String, String>> child1 = new ArrayList<Map<String, String>>();
  Map<String, String> child1Data1 = new HashMap<String, String>();
  child1Data1.put("child", "child1Data1");
  Map<String, String> child1Data2 = new HashMap<String, String>();
  child1Data2.put("child", "child1Data2");
  child1.add(child1Data1);
  child1.add(child1Data2);

  //准备第二个一级列表中的二级列表数据:一个二级列表,显示"child2Data1"
  List<Map<String, String>> child2 = new ArrayList<Map<String, String>>();
  Map<String, String> child2Data1 = new HashMap<String, String>();
  child2Data1.put("child", "child2Data1");
  child2.add(child2Data1);

  //用一个list对象保存所有的二级列表数据
  List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>();
  childs.add(child1);
  childs.add(child2);

  ExpandableAdapter viewAdapter = new ExpandableAdapter(this, groups, childs);
  elv.setAdapter(viewAdapter);
 }

 //自定义的ExpandListAdapter
 class ExpandableAdapter extends BaseExpandableListAdapter
 {
  private Context context;
  List<Map<String, String>> groups;
  List<List<Map<String, String>>> childs;

  /*
   * 构造函数:
   * 参数1:context对象
   * 参数2:一级列表数据源
   * 参数3:二级列表数据源
   */
  public ExpandableAdapter(Context context, List<Map<String, String>> groups, List<List<Map<String, String>>> childs)
  {
   this.groups = groups;
   this.childs = childs;
   this.context = context;
  }

  public Object getChild(int groupPosition, int childPosition)
  {
   return childs.get(groupPosition).get(childPosition);
  }

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

  //获取二级列表的View对象
  public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
    ViewGroup parent)
  {
   @SuppressWarnings("unchecked")
   String text = ((Map<String, String>) getChild(groupPosition, childPosition)).get("child");
   LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

   //获取二级列表对应的布局文件, 并将其各元素设置相应的属性
   LinearLayout linearLayout = (LinearLayout) layoutInflater.inflate(R.layout.child, null);
   TextView tv = (TextView) linearLayout.findViewById(R.id.child_tv);
   tv.setText(text);
   ImageView imageView = (ImageView)linearLayout.findViewById(R.id.child_iv);
   imageView.setImageResource(R.drawable.icon);

   return linearLayout;
  }

  public int getChildrenCount(int groupPosition)
  {
   return childs.get(groupPosition).size();
  }

  public Object getGroup(int groupPosition)
  {
   return groups.get(groupPosition);
  }

  public int getGroupCount()
  {
   return groups.size();
  }

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

  //获取一级列表View对象
  public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
  {
   String text = groups.get(groupPosition).get("group");
   LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

   //获取一级列表布局文件,设置相应元素属性
   LinearLayout linearLayout = (LinearLayout) layoutInflater.inflate(R.layout.group, null);
   TextView textView = (TextView)linearLayout.findViewById(R.id.group_tv);
   textView.setText(text);

   return linearLayout;
  }

  public boolean hasStableIds()
  {
   return false;
  }

  public boolean isChildSelectable(int groupPosition, int childPosition)
  {
   return false;
  }

 }
}

 

截图:

ExpandableListView简单用法 - 无尘 - 无尘的博客

这篇关于ExpandableListView简单用法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/500902

相关文章

MySQL 中的 CAST 函数详解及常见用法

《MySQL中的CAST函数详解及常见用法》CAST函数是MySQL中用于数据类型转换的重要函数,它允许你将一个值从一种数据类型转换为另一种数据类型,本文给大家介绍MySQL中的CAST... 目录mysql 中的 CAST 函数详解一、基本语法二、支持的数据类型三、常见用法示例1. 字符串转数字2. 数字

Python中你不知道的gzip高级用法分享

《Python中你不知道的gzip高级用法分享》在当今大数据时代,数据存储和传输成本已成为每个开发者必须考虑的问题,Python内置的gzip模块提供了一种简单高效的解决方案,下面小编就来和大家详细讲... 目录前言:为什么数据压缩如此重要1. gzip 模块基础介绍2. 基本压缩与解压缩操作2.1 压缩文

解读GC日志中的各项指标用法

《解读GC日志中的各项指标用法》:本文主要介绍GC日志中的各项指标用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、基础 GC 日志格式(以 G1 为例)1. Minor GC 日志2. Full GC 日志二、关键指标解析1. GC 类型与触发原因2. 堆

MySQL数据库中ENUM的用法是什么详解

《MySQL数据库中ENUM的用法是什么详解》ENUM是一个字符串对象,用于指定一组预定义的值,并可在创建表时使用,下面:本文主要介绍MySQL数据库中ENUM的用法是什么的相关资料,文中通过代码... 目录mysql 中 ENUM 的用法一、ENUM 的定义与语法二、ENUM 的特点三、ENUM 的用法1

JavaSE正则表达式用法总结大全

《JavaSE正则表达式用法总结大全》正则表达式就是由一些特定的字符组成,代表的是一个规则,:本文主要介绍JavaSE正则表达式用法的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录常用的正则表达式匹配符正则表China编程达式常用的类Pattern类Matcher类PatternSynta

MySQL之InnoDB存储引擎中的索引用法及说明

《MySQL之InnoDB存储引擎中的索引用法及说明》:本文主要介绍MySQL之InnoDB存储引擎中的索引用法及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录1、背景2、准备3、正篇【1】存储用户记录的数据页【2】存储目录项记录的数据页【3】聚簇索引【4】二

mysql中的数据目录用法及说明

《mysql中的数据目录用法及说明》:本文主要介绍mysql中的数据目录用法及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、版本3、数据目录4、总结1、背景安装mysql之后,在安装目录下会有一个data目录,我们创建的数据库、创建的表、插入的

深度解析Python装饰器常见用法与进阶技巧

《深度解析Python装饰器常见用法与进阶技巧》Python装饰器(Decorator)是提升代码可读性与复用性的强大工具,本文将深入解析Python装饰器的原理,常见用法,进阶技巧与最佳实践,希望可... 目录装饰器的基本原理函数装饰器的常见用法带参数的装饰器类装饰器与方法装饰器装饰器的嵌套与组合进阶技巧

Mysql中isnull,ifnull,nullif的用法及语义详解

《Mysql中isnull,ifnull,nullif的用法及语义详解》MySQL中ISNULL判断表达式是否为NULL,IFNULL替换NULL值为指定值,NULLIF在表达式相等时返回NULL,用... 目录mysql中isnull,ifnull,nullif的用法1. ISNULL(expr) → 判

Java中的for循环高级用法

《Java中的for循环高级用法》本文系统解析Java中传统、增强型for循环、StreamAPI及并行流的实现原理与性能差异,并通过大量代码示例展示实际开发中的最佳实践,感兴趣的朋友一起看看吧... 目录前言一、基础篇:传统for循环1.1 标准语法结构1.2 典型应用场景二、进阶篇:增强型for循环2.