2.
3.代码示例:
(1)java代码:
package com.example.test;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ExpandableListView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
ExpandableListView expandableListView;
String[] groupStringArr = {"腾讯", "百度", "阿里巴巴"};
String[] [] childStringArrs = {
{"QQ","微信","QQ浏览器"},
{"百度搜索","百度地图","百度外卖"},
{"淘宝","支付宝","天猫"}
};
List<Map<String, ?>> groupData = new ArrayList<>();
List<List<Map<String,?>>> childData = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
expandableListView = (ExpandableListView) findViewById(R.id.demo_expandable_list_view);
for (int i = 0; i < groupStringArr.length; i++) {
Map<String, String> map = new HashMap<>();
map.put("groupName", groupStringArr[i]);
groupData.add(map);
List<Map<String,?>> itemList = new ArrayList<>();
for (int j = 0; j < childStringArrs[i].length; j++) {
Map<String,String> map1 = new HashMap<>();
map1.put("itemName",childStringArrs[i][j]);
itemList.add(map1);
}
childData.add(itemList);
}
/**this: 是参数context,代表当前活动。**/
/**groupData:数据源List<Map<String, ?>>**/
/**R.layout.item_group:(参数:int groupLayout)调用的布局文件**/
/**String[]{"groupName"}:(参数:String[] groupFrom)数据源List<Map<String, ?>>**/
/**int[]{R.id.tv_group}:(int[] groupTo)数据源List<Map<String, ?>>**/
SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(this, groupData,
R.layout.item_group, new String[]{"groupName"}, new int[]{R.id.tv_group},
childData,R.layout.item_child,new String[]{"itemName"}, new int[]{R.id.tv_child});
expandableListView.setAdapter(adapter);
}
}
(2)布局代码:
//主活动布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ExpandableListView
android:id="@+id/demo_expandable_list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ExpandableListView>
</LinearLayout>
//item_child布局(新建)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:gravity="center_vertical"
android:paddingLeft="50dp"
android:layout_height="60dp">
<TextView
android:id="@+id/tv_child"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
//item_group布局(新建)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:gravity="center_vertical"
android:layout_height="60dp">
<TextView
android:id="@+id/tv_group"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
4.图片: