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

相关文章

Docker镜像修改hosts及dockerfile修改hosts文件的实现方式

《Docker镜像修改hosts及dockerfile修改hosts文件的实现方式》:本文主要介绍Docker镜像修改hosts及dockerfile修改hosts文件的实现方式,具有很好的参考价... 目录docker镜像修改hosts及dockerfile修改hosts文件准备 dockerfile 文

macOS无效Launchpad图标轻松删除的4 种实用方法

《macOS无效Launchpad图标轻松删除的4种实用方法》mac中不在appstore上下载的应用经常在删除后它的图标还残留在launchpad中,并且长按图标也不会出现删除符号,下面解决这个问... 在 MACOS 上,Launchpad(也就是「启动台」)是一个便捷的 App 启动工具。但有时候,应

Python实现无痛修改第三方库源码的方法详解

《Python实现无痛修改第三方库源码的方法详解》很多时候,我们下载的第三方库是不会有需求不满足的情况,但也有极少的情况,第三方库没有兼顾到需求,本文将介绍几个修改源码的操作,大家可以根据需求进行选择... 目录需求不符合模拟示例 1. 修改源文件2. 继承修改3. 猴子补丁4. 追踪局部变量需求不符合很

Linux修改pip和conda缓存路径的几种方法

《Linux修改pip和conda缓存路径的几种方法》在Python生态中,pip和conda是两种常见的软件包管理工具,它们在安装、更新和卸载软件包时都会使用缓存来提高效率,适当地修改它们的缓存路径... 目录一、pip 和 conda 的缓存机制1. pip 的缓存机制默认缓存路径2. conda 的缓

Linux修改pip临时目录方法的详解

《Linux修改pip临时目录方法的详解》在Linux系统中,pip在安装Python包时会使用临时目录(TMPDIR),但默认的临时目录可能会受到存储空间不足或权限问题的影响,所以本文将详细介绍如何... 目录引言一、为什么要修改 pip 的临时目录?1. 解决存储空间不足的问题2. 解决权限问题3. 提

Linux文件名修改方法大全

《Linux文件名修改方法大全》在Linux系统中,文件名修改是一个常见且重要的操作,文件名修改可以更好地管理文件和文件夹,使其更具可读性和有序性,本文将介绍三种在Linux系统下常用的文件名修改方法... 目录一、引言二、使用mv命令修改文件名三、使用rename命令修改文件名四、mv命令和rename命

mybatis-plus 实现查询表名动态修改的示例代码

《mybatis-plus实现查询表名动态修改的示例代码》通过MyBatis-Plus实现表名的动态替换,根据配置或入参选择不同的表,本文主要介绍了mybatis-plus实现查询表名动态修改的示... 目录实现数据库初始化依赖包配置读取类设置 myBATis-plus 插件测试通过 mybatis-plu

Linux下修改hostname的三种实现方式

《Linux下修改hostname的三种实现方式》:本文主要介绍Linux下修改hostname的三种实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下修改ho编程stname三种方式方法1:修改配置文件方法2:hFvEWEostnamectl命

Git如何修改已提交人的用户名和邮箱

《Git如何修改已提交人的用户名和邮箱》文章介绍了如何修改Git已提交人的用户名和邮箱,包括注意事项和具体步骤,确保操作正确无误... 目录git修改已提交人的用户名和邮箱前言第一步第二步总结git修改已提交人的用户名和邮箱前言需注意以下两点内容:需要在顶层目录下(php就是 .git 文件夹所在的目

Python使用PIL库将PNG图片转换为ICO图标的示例代码

《Python使用PIL库将PNG图片转换为ICO图标的示例代码》在软件开发和网站设计中,ICO图标是一种常用的图像格式,特别适用于应用程序图标、网页收藏夹图标等场景,本文将介绍如何使用Python的... 目录引言准备工作代码解析实践操作结果展示结语引言在软件开发和网站设计中,ICO图标是一种常用的图像