Android ExpandableListView

article/2025/9/26 13:28:04

ExpandableListView可以显示一个视图垂直滚动显示两级列表中的条目,这不同于列表视图(ListView)。ExpandableListView允许有两个层次:一级列表中有二级列表。
比如在手机设置中,对于分类,有很好的效果。手机版QQ也是这样的效果。

完整代码下载

demo

效果图片

使用ExpandableListView的整体思路

(1)给ExpandableListView设置适配器,那么必须先设置数据源。

(2)数据源,就是此处的适配器类ExpandableAdapter,此方法继承了BaseExpandableListAdapter,需要重写里面的10个方法。
数据源中,用到了自定义的View布局,此时根据自己的需求,来设置组和子项的布局样式。
getChildView()和getGroupView()方法设置自定义布局。

(3)数据源设置好,直接给ExpandableListView.setAdapter()即可实现此收缩功能。ExpandableListView的完整代码实现

完整代码:

activity_main.xml:在里面放置一个ExpandableListView控件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"><ExpandableListViewandroid:id="@+id/expandableListView"android:layout_width="match_parent"android:layout_height="wrap_content"/></RelativeLayout>

item_group.xml:一级列表的item的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="#cccccc"android:orientation="horizontal"><TextViewandroid:id="@+id/tv_group"android:layout_width="wrap_content"android:layout_height="30dp"android:gravity="center"android:text="group text"android:textColor="#000000"/></LinearLayout>

item_child.xml:二级列表的item的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:orientation="horizontal"><ImageViewandroid:id="@+id/iv_child"android:layout_width="30dp"android:layout_height="30dp"android:src="@mipmap/ic_launcher"/><TextViewandroid:id="@+id/tv_child"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="item text"android:textColor="#000000"/></LinearLayout>

MainActivity.java:

package com.example.recyview;import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;public class MainActivity extends Activity {//Viewprivate ExpandableListView expandableListView;//Model:定义的数据private String[] groups = {"A", "B", "C"};//注意,字符数组不要写成{{"A1,A2,A3,A4"}, {"B1,B2,B3,B4,B5"}, {"C1,C2,C3,C4"}}private String[][] childs = {{"A1", "A2", "A3", "A4"}, {"A1", "A2", "A3", "B4"}, {"A1", "A2", "A3", "C4"}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);expandableListView.setAdapter(new MyExpandableListView());expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {@Overridepublic boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {Toast.makeText(MainActivity.this, "子条目"+childPosition, Toast.LENGTH_SHORT).show();return true;}});}//为ExpandableListView自定义适配器class MyExpandableListView extends BaseExpandableListAdapter {//返回一级列表的个数@Overridepublic int getGroupCount() {return groups.length;}//返回每个二级列表的个数@Overridepublic int getChildrenCount(int groupPosition) { //参数groupPosition表示第几个一级列表Log.d("smyhvae", "-->" + groupPosition);return childs[groupPosition].length;}//返回一级列表的单个item(返回的是对象)@Overridepublic Object getGroup(int groupPosition) {return groups[groupPosition];}//返回二级列表中的单个item(返回的是对象)@Overridepublic Object getChild(int groupPosition, int childPosition) {return childs[groupPosition][childPosition];  //不要误写成groups[groupPosition][childPosition]}@Overridepublic long getGroupId(int groupPosition) {return groupPosition;}@Overridepublic long getChildId(int groupPosition, int childPosition) {return childPosition;}//每个item的id是否是固定?一般为true@Overridepublic boolean hasStableIds() {return true;}//【重要】填充一级列表@Overridepublic View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {if (convertView == null) {convertView = getLayoutInflater().inflate(R.layout.item_group, null);} else {}TextView tv_group = (TextView) convertView.findViewById(R.id.tv_group);tv_group.setText(groups[groupPosition]);return convertView;}//【重要】填充二级列表@Overridepublic View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {if (convertView == null) {convertView = getLayoutInflater().inflate(R.layout.item_child, null);}ImageView iv_child = (ImageView) convertView.findViewById(R.id.iv_child);TextView tv_child = (TextView) convertView.findViewById(R.id.tv_child);//iv_child.setImageResource(resId);tv_child.setText(childs[groupPosition][childPosition]);return convertView;}//二级列表中的item是否能够被选中?可以改为true@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {return true;}}}

 


http://chatgpt.dhexx.cn/article/lL6u6TTW.shtml

相关文章

ExpandableListView用法

先上个效果图&#xff1a; 1&#xff0c;我用的fragment import java.util.ArrayList; import java.util.Collections; import java.util.List; import com.dami.student.ui.chatui.adapter.ContactsExpandableListAdapter; import com.dami.student.R; import android.conten…

android expandablelistview简单应用,android ExpandableListView简单例子

android中常常要用到ListView&#xff0c;有时也要用到ExpandableListView&#xff0c;如在手机设置中&#xff0c;对于分类有很好的效果&#xff0c;会用ListView的人一定会用ExpandableListView&#xff0c;因为 ExpandableListView extends ListView的&#xff0c;下面来看个…

android expandablelistview横向,完美实现ExpandableListView二级分栏效果

本文实例为大家分享了ExpandableListView二级分栏效果的具体代码&#xff0c;供大家参考&#xff0c;具体内容如下 对ExpandableListView控件进行封装(未自定义)直接上代码&#xff1a; 通用ViewHolder类&#xff0c;仅在setImageResource中添加代码 package com.svp.haoyan.ex…

android expandablelistview横向,expandableListView 总结

实现效果图&#xff1a; expandableListView groupIndicator 图片默认是在左边&#xff0c;而且比较难看&#xff0c;而我要的是实现groupIndicator 在右边自定义图片&#xff0c; 换图片 最简单的就是直接copy 系统 android:drawable/expander_group ?android:attr/expandab…

Android学习之ExpandableListView

什么是ExpandableListView ExpandableListView是扩展的ListView&#xff0c;继承自ListView&#xff1b;ExpandableListView可以实现点击展开列表&#xff0c;再点击收缩回去的效果。 ExpandableListView的使用 首先需要在主布局文件中声明ExpandableListView&#xff1b; …

ExpandableListView详解

文章目录 效果图ExpandableListView的简介与使用去掉ExpandableListView的箭头以及自定义Indicator解决setOnChildClickListener失效问题解决collapseGroup(i)崩溃问题解决group_item.xml中包含CheckBox、EditText等&#xff0c;点击不能展开的问题 1.效果图 2.ExpandableLi…

values_list()

转载&#xff1a;https://www.cnblogs.com/chenchao1990/p/5311531.html?utm_sourcetuicool&utm_mediumreferral

列表(lists)

Lists and the things you can do with them.Includes indexing(索引&#xff09;,slicing &#xff08;切片&#xff09;and mutating&#xff08;变异&#xff09;. 1.Python 中的列表表示有序的值序列。 以下是如何创建它们的示例&#xff1a; primes [2,3,5,7] #我们可以…

Android Preference API 用法--ListPreference(一)

一&#xff0e;ListPreference简介 我们都只知道SharedPreference非常适合于参数设置功能&#xff0c;在此处的preference 也是代表SharedPreference的意思&#xff0c;在SharedPreference中&#xff0c;我们可以迅速的将某些值保存进xml文件中&#xff0c;然后我们可以读取这…

android entries属性,ListPreference需要设置两个属性:android:entries和android:entryValues...

android:defaultValue"black" android:entries"array/setting_skintheme" android:entryValues"array/setting_skintheme_value" android:key"SkinTheme" android:summary"请选择您喜欢的软件皮肤颜色" android:title"…

Android ListPreference的用法

首先&#xff0c;我们明确&#xff0c;preference是和数据存储相关的。 其次&#xff0c;它能帮助我们方便的进行数据存储&#xff01;为什么这个地方一定要强调下方便的这个词呢&#xff1f;原因是&#xff0c;我们可以根本就不使用&#xff0c;我们有另外的N种办法可以实现同…

List总结

ArrayList与LinkedList的区别是什么&#xff1f; 从继承树&#xff0c;底层数据结构&#xff0c;线程安全&#xff0c;执行效率来进行分析。 1.底层使用的数据结构 ArrayList 底层使用的是Object数组&#xff0c;初始化时就会指向的会是一个static修饰的空数组&#xff0c;数…

android Preference ListPreference EditTextPreference

android中包含Preference ListPreference EditTextPreference等控件布局的写法&#xff0c;已经操作各个控件的事件介绍&#xff0c;如下 <?xml version"1.0" encoding"utf-8"?> <PreferenceScreen xmlns:android"http://schemas.android…

Android中ListPreference的使用

这篇主要是具体例子&#xff0c;可以先看一下理论&#xff0c;网址是&#xff1a;Android中Preference的使用以及监听事件分析 我们可以先看一下效果图 我们先截取不小段布局&#xff0c;代码如下&#xff1a; <ListPreferenceandroid:defaultValue"string/usb_defaul…

list列表的用法

List&#xff08;列表&#xff09;是 Python中使用最频繁的数据类型。列表可以完成大多数集合类的数据结构实现。列表中元素的类型可以不相同&#xff0c;它支持数字&#xff0c;字符串甚至可以包含列表&#xff08;所谓嵌套&#xff09;。列表是写在方括号 [ ] 之间&#xff0…

List 列表的用法

List&#xff08;列表&#xff09; 是 Python 中使用最频繁的数据类型。列表可以完成大多数集合类的数据结构实现。列表中元素的类型可以不相同&#xff0c;它支持数字&#xff0c;字符串甚至可以包含列表&#xff08;所谓嵌套&#xff09;。列表是写在方括号 [ ] 之间、用逗号…

自定义ListPreference弹出Dialog背景

公司最近项目需求是用实体键来在应用内操作,这就需要对那些可点击的widget的背景进行自定义,使其响应focus状态随即变化。大部分的layout改动都是挺简单的。 但是遇到一个主要的问题就是自带的PreferenceFragment,里面的layout不是通过平时常用的Button ImageView那些来写的…

android之ListPreference的用法_PreferenceActivity用法

首先&#xff0c;我们明确&#xff0c;preference是和数据存储相关的。 其次&#xff0c;它能帮助我们方便的进行数据存储&#xff01;为什么这个地方一定要强调下方便的这个词呢&#xff1f;原因是&#xff0c;我们可以根本就不使用&#xff0c;我们有另外的N种办法可…

ListPreference详解与使用

listprefenence比switchpreference多了一个arrays.xml&#xff0c;这个arrays.xml就是用来写我们需要的list的内容。 以切换mode功能为例&#xff0c;就是切换协议的mode&#xff0c;一共需要五个选项。除了switchpreference中的key&#xff0c;title&#xff0c;summary和pers…

互联网协议 — TCP — 流量控制

目录 文章目录 目录TCP 流量控制流量控制处理流程 TCP 流量控制 TCP 流量控制由滑动窗口&#xff08;Sliding Window&#xff09;技术支撑。Sender 根据 Receiver 返回 ACK 中包含的 Window Size 字段来动态调整自身发送 Segments 的速率&#xff0c;以此保证收发双方不会因为…