ExpandableListView修改group图标

2023-12-16 15:08

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

修改group前面的图标有两种方式:

一、通过修改xml文件中groupIndicator更换为@drawable/**

设置selector

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <!-- 下拉状态 -->
    <item android:state_expanded="false"
          android:drawingCacheQuality="auto"
        android:drawable="@drawable/xiala"/>
    <!-- 收起状态 -->
    <item android:state_expanded="true"
        android:drawingCacheQuality="auto"
     android:drawable="@drawable/shouqi"/>
</selector>

ExpandableListView修改group图标 - 无尘 - 无尘的博客
但是,这种方式做的话,图标不能根据内容调整大小。
二、通过禁用系统自带图标,在group中添加一个ImageView来实现,并通过判断是否有子项来控制是否显示此图标
1、main.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="fill_parent"
    >
    <!-- 禁用系统自带图标android:groupIndicator="@null" -->
<ExpandableListView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:groupIndicator="@null"
    android:id="@+id/mExpandableListView"
    />
</LinearLayout>
2、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">
 <ImageView
  android:layout_width = "25dip"
  android:layout_height = "25dip"
  android:layout_marginTop="10dip"
  android:id="@+id/mImageView"
  />
 <TextView
  android:id = "@+id/group_tv"
  android:layout_width = "wrap_content"
  android:layout_height = "wrap_content"
  android:paddingLeft = "30px"
  android:paddingBottom = "10px"
  android:textSize = "26sp"/>
</LinearLayout>
3、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>
4、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);
   
   ImageView imageView = (ImageView) linearLayout.findViewById(R.id.mImageView);
   
   if(getChildrenCount(groupPosition) == 0){//该组下没有子项
    imageView.setVisibility(View.GONE);
   }else{
    if(isExpanded == true){//展开状态
     imageView.setImageResource(R.drawable.xiala);
    }else{//收起状态
     imageView.setImageResource(R.drawable.shouqi);
    }
   }
return linearLayout;
  }
public boolean hasStableIds()
  {
   return false;
  }
public boolean isChildSelectable(int groupPosition, int childPosition)
  {
   return false;
  }
}
}
截图:
ExpandableListView修改group图标 - 无尘 - 无尘的博客
 
 

这篇关于ExpandableListView修改group图标的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

修改若依框架Token的过期时间问题

《修改若依框架Token的过期时间问题》本文介绍了如何修改若依框架中Token的过期时间,通过修改`application.yml`文件中的配置来实现,默认单位为分钟,希望此经验对大家有所帮助,也欢迎... 目录修改若依框架Token的过期时间修改Token的过期时间关闭Token的过期时js间总结修改若依

MySQL修改密码的四种实现方式

《MySQL修改密码的四种实现方式》文章主要介绍了如何使用命令行工具修改MySQL密码,包括使用`setpassword`命令和`mysqladmin`命令,此外,还详细描述了忘记密码时的处理方法,包... 目录mysql修改密码四种方式一、set password命令二、使用mysqladmin三、修改u

使用Python在Excel中插入、修改、提取和删除超链接

《使用Python在Excel中插入、修改、提取和删除超链接》超链接是Excel中的常用功能,通过点击超链接可以快速跳转到外部网站、本地文件或工作表中的特定单元格,有效提升数据访问的效率和用户体验,这... 目录引言使用工具python在Excel中插入超链接Python修改Excel中的超链接Python

MySQL报错sql_mode=only_full_group_by的问题解决

《MySQL报错sql_mode=only_full_group_by的问题解决》本文主要介绍了MySQL报错sql_mode=only_full_group_by的问题解决,文中通过示例代码介绍的非... 目录报错信息DataGrip 报错还原Navicat 报错还原报错原因解决方案查看当前 sql mo

macOS怎么轻松更换App图标? Mac电脑图标更换指南

《macOS怎么轻松更换App图标?Mac电脑图标更换指南》想要给你的Mac电脑按照自己的喜好来更换App图标?其实非常简单,只需要两步就能搞定,下面我来详细讲解一下... 虽然 MACOS 的个性化定制选项已经「缩水」,不如早期版本那么丰富,www.chinasem.cn但我们仍然可以按照自己的喜好来更换

python修改字符串值的三种方法

《python修改字符串值的三种方法》本文主要介绍了python修改字符串值的三种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录第一种方法:第二种方法:第三种方法:在python中,字符串对象是不可变类型,所以我们没办法直接

Mysql8.0修改配置文件my.ini的坑及解决

《Mysql8.0修改配置文件my.ini的坑及解决》使用记事本直接编辑my.ini文件保存后,可能会导致MySQL无法启动,因为MySQL会以ANSI编码读取该文件,解决方法是使用Notepad++... 目录Myhttp://www.chinasem.cnsql8.0修改配置文件my.ini的坑出现的问题

两个月冲刺软考——访问位与修改位的题型(淘汰哪一页);内聚的类型;关于码制的知识点;地址映射的相关内容

1.访问位与修改位的题型(淘汰哪一页) 访问位:为1时表示在内存期间被访问过,为0时表示未被访问;修改位:为1时表示该页面自从被装入内存后被修改过,为0时表示未修改过。 置换页面时,最先置换访问位和修改位为00的,其次是01(没被访问但被修改过)的,之后是10(被访问了但没被修改过),最后是11。 2.内聚的类型 功能内聚:完成一个单一功能,各个部分协同工作,缺一不可。 顺序内聚:

如何在运行时修改serialVersionUID

优质博文:IT-BLOG-CN 问题 我正在使用第三方库连接到外部系统,一切运行正常,但突然出现序列化错误 java.io.InvalidClassException: com.essbase.api.base.EssException; local class incompatible: stream classdesc serialVersionUID = 90314637791991

matlab读取NC文件(含group)

matlab读取NC文件(含group): NC文件数据结构: 代码: % 打开 NetCDF 文件filename = 'your_file.nc'; % 替换为你的文件名% 使用 netcdf.open 函数打开文件ncid = netcdf.open(filename, 'NC_NOWRITE');% 查看文件中的组% 假设我们想读取名为 "group1" 的组groupName