Android ExpandableListView
ExpandableListView 结合SimpleExpandableListAdapter用法
最终实现效果:

activity_main.xml
<?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:id="@+id/activity_main" 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.zy.expandablelistview.MainActivity"> <ExpandableListView android:id="@+id/expand_list_view" android:layout_width="match_parent" android:layout_height="match_parent"> </ExpandableListView> </RelativeLayout>
父级布局文件group_item.xml
<?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="50dp" android:gravity="center_vertical" android:orientation="vertical"> <TextView android:id="@+id/tv_group" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
子级布局文件child_item.xml
<?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="30dp" android:paddingLeft="30dp" android:gravity="center_vertical" android:orientation="vertical"> <TextView android:id="@+id/tv_child" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
MainActivity.java
package com.zy.expandablelistview; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ExpandableListView; import android.widget.SimpleExpandableListAdapter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class MainActivity extends AppCompatActivity { List<Map<String,?>> groupList = new ArrayList<>(); List<List<Map<String,String>>> itemList = new ArrayList<>(); String[] groupStringArr = {"腾讯","百度","阿里巴巴"}; String[][] itemStringArr = { {"QQ","腾讯云服务","QQ浏览器"}, {"百度地图","百度搜索","百度外卖"}, {"淘宝","天猫","阿里云"} }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.expand_list_view); //父级数组 for (int i = 0; i < groupStringArr.length; i++) { Map<String,String> gMap = new HashMap<>(); gMap.put("groupName",groupStringArr[i]); groupList.add(gMap); //子级数组 List<Map<String,String>> itemFor = new ArrayList<>(); for (int j = 0; j < itemStringArr[i].length; j++) { Map<String,String> iMap = new HashMap<>(); iMap.put("itemName",itemStringArr[i][j]); itemFor.add(iMap); } itemList.add(itemFor); } SimpleExpandableListAdapter simpleExpandableListAdapter = new SimpleExpandableListAdapter(this, groupList,R.layout.group_item,new String[]{"groupName"},new int[]{R.id.tv_group}, itemList,R.layout.child_item,new String[]{"itemName"},new int[]{R.id.tv_child}); expandableListView.setAdapter(simpleExpandableListAdapter); } }

浙公网安备 33010602011771号