Android UI控件ExpandableListView基本用法详解

作者:生命壹号 时间:2021-12-26 22:20:50 

ExpandableListView介绍 

ExpandableListView的引入 

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

Android UI控件ExpandableListView基本用法详解 

使用ExpandableListView的整体思路 

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

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

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

ExpandableListView的完整代码实现
 (1)activity_main.xml:在里面放置一个ExpandableListView控件 


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.smyhvae.expandablelistviewdemo.MainActivity">

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

</RelativeLayout>

(2)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">

<TextView
 android:id="@+id/tv_group"
 android:layout_width="wrap_content"
 android:layout_height="30dp"
 android:gravity="center"
 android:text="group text"
 android:textColor="#000000"
 />

</LinearLayout>

(3)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">

<ImageView
 android:id="@+id/iv_child"
 android:layout_width="30dp"
 android:layout_height="30dp"
 android:src="@mipmap/ic_launcher"/>

<TextView
 android:id="@+id/tv_child"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="item text"
 android:textColor="#000000"/>

</LinearLayout>

(4)MainActivity.java: 


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;

public class MainActivity extends Activity {

//View
private 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"}};

@Override
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);

expandableListView.setAdapter(new MyExpandableListView());

}

//为ExpandableListView自定义适配器
class MyExpandableListView extends BaseExpandableListAdapter {

//返回一级列表的个数
 @Override
 public int getGroupCount() {
  return groups.length;
 }

//返回每个二级列表的个数
 @Override
 public int getChildrenCount(int groupPosition) { //参数groupPosition表示第几个一级列表
  Log.d("smyhvae", "-->" + groupPosition);
  return childs[groupPosition].length;
 }

//返回一级列表的单个item(返回的是对象)
 @Override
 public Object getGroup(int groupPosition) {
  return groups[groupPosition];
 }

//返回二级列表中的单个item(返回的是对象)
 @Override
 public Object getChild(int groupPosition, int childPosition) {
  return childs[groupPosition][childPosition]; //不要误写成groups[groupPosition][childPosition]
 }

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

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

//每个item的id是否是固定?一般为true
 @Override
 public boolean hasStableIds() {
  return true;
 }

//【重要】填充一级列表
 @Override
 public 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;
 }

//【重要】填充二级列表
 @Override
 public 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
 @Override
 public boolean isChildSelectable(int groupPosition, int childPosition) {
  return true;
 }
}

}

注:请自行完成ConvertView和ViewHolder的优化。 

工程文件:(Android Studio 2.1)http://xiazai.jb51.net/201609/yuanma/AndroidExpandableListView(jb51.net).rar

标签:android,控件,expandableListView
0
投稿

猜你喜欢

  • 详解Android App中ViewPager使用PagerAdapter的方法

    2021-12-06 06:11:52
  • 详解C# partial 关键字的使用

    2023-06-07 20:46:09
  • SpringBoot Admin用法实例讲解

    2021-07-26 17:57:58
  • C#基于TimeSpan实现倒计时效果的方法

    2021-10-19 00:06:04
  • C#实现自定义定时组件的方法

    2023-06-04 12:36:26
  • Java多种经典排序算法(含动态图)

    2023-09-24 00:45:02
  • Java数据结构之二叉排序树的实现

    2023-07-05 02:27:25
  • C#实现快速排序算法

    2023-03-20 08:41:51
  • java8实现List中对象属性的去重方法

    2023-08-30 20:50:48
  • Android使用系统相机进行拍照的步骤

    2023-10-07 16:05:05
  • springboot vue前后端接口测试树结点添加功能

    2022-05-22 18:51:09
  • 对Mapper 中几种update的区别说明

    2023-05-19 04:39:44
  • SpringBoot集成POI实现Excel导入导出的示例详解

    2022-12-05 19:09:10
  • Spring Boot如何防止重复提交

    2021-11-10 16:23:42
  • Java数据结构之栈与队列实例详解

    2021-05-29 03:25:13
  • 基于C#对用户密码使用MD5加密与解密

    2022-11-24 23:05:23
  • Java动态添加view的方法

    2023-06-11 04:20:38
  • BeanUtils.copyProperties在拷贝属性时忽略空值的操作

    2022-07-11 05:22:04
  • 查找算法之二分查找的C++实现

    2022-05-27 07:24:17
  • java多线程并发中使用Lockers类将多线程共享资源锁定

    2021-11-14 11:08:37
  • asp之家 软件编程 m.aspxhome.com