预览图:
就如同QQ联系人界面
总的需要准备三个布局文件:
主界面的布局、父布局、子布局
主布局文件:
父布局文件:就放了一个文本控件
子布局文件:左边放一张图片,右边是文字
定义一个子布局的类
package com.example.expandablelistviewtest;
public class Item {
private int imgId;
private String des;
Item (int imgId,String des){
this.imgId=imgId;
this.des=des;
}
public int getImgId() {
return imgId;
}
public void setImgId(int imgId) {
this.imgId = imgId;
}
public String getDes() {
return des;
}
public void setDes(String des) {
this.des = des;
}
}
和ListView一样也需要适配器
package com.example.expandablelistviewtest;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MyExtendableListViewAdapter extends BaseExpandableListAdapter {
private static final String TAG = "pos";
//子列表
private ArrayList>iChildList;
//父列表
private ListparentList;
MyExtendableListViewAdapter(ListparentList,List> iChildList){
this.parentList=parentList;
this.iChildList= (ArrayList>) iChildList;
}
@Override
// 获取分组的个数
public int getGroupCount() {
return parentList.size();
}
//获取指定分组中的子选项的个数
@Override
public int getChildrenCount(int groupPosition) {
Log.d(TAG, "getChildrenCount: "+groupPosition);
return iChildList.size();
}
// 获取指定的分组数据
@Override
public Object getGroup(int groupPosition) {
Log.d(TAG, "getGroup: "+groupPosition);
return parentList.get(groupPosition);
}
//获取指定分组中的指定子选项数据
@Override
public Object getChild(int groupPosition, int childPosition) {
return iChildList.get(groupPosition).get(childPosition);
}
//获取指定分组的ID, 这个ID必须是唯一的
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
//获取子选项的ID, 这个ID必须是唯一的
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
//分组和子选项是否持有稳定的ID, 就是说底层数据的改变会不会影响到它们
@Override
public boolean hasStableIds() {
return false;
}
/**
*
* 获取显示指定组的视图对象
*
* @param groupPosition 组位置
* @param isExpanded 该组是展开状态还是伸缩状态
* @param convertView 重用已有的视图对象
* @param parent 返回的视图对象始终依附于的视图组
*/
// 获取显示指定分组的视图
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
GroupViewHolder groupViewHolder;
if (convertView == null){
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.partent_item,parent,false);
groupViewHolder = new GroupViewHolder(convertView);
convertView.setTag(groupViewHolder);
}else {
groupViewHolder = (GroupViewHolder)convertView.getTag();
}
groupViewHolder.tvTitle.setText(parentList.get(groupPosition));
return convertView;
}
/**
*
* 获取一个视图对象,显示指定组中的指定子元素数据。
*
* @param groupPosition 组位置
* @param childPosition 子元素位置
* @param isLastChild 子元素是否处于组中的最后一个
* @param convertView 重用已有的视图(View)对象
* @param parent 返回的视图(View)对象始终依附于的视图组
* @return
* @see android.widget.ExpandableListAdapter#getChildView(int, int, boolean, android.view.View,
* android.view.ViewGroup)
*/
//取得显示给定分组给定子位置的数据用的视图
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ChildViewHolder childViewHolder;
if (convertView==null){
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false);
childViewHolder = new ChildViewHolder(convertView);
convertView.setTag(childViewHolder);
}else {
childViewHolder = (ChildViewHolder) convertView.getTag();
}
childViewHolder.tvTitle.setText(iChildList.get(groupPosition).get(childPosition).getDes());
childViewHolder.imageView.setImageResource(iChildList.get(groupPosition).get(childPosition).getImgId());
return convertView;
}
//指定位置上的子元素是否可选中
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
static class GroupViewHolder {
TextView tvTitle;
GroupViewHolder(View convertView){
tvTitle = convertView.findViewById(R.id.label_group_normal);
}
}
static class ChildViewHolder {
TextView tvTitle;
ImageView imageView;
ChildViewHolder(View convertView){
tvTitle = convertView.findViewById(R.id.tv_name);
imageView=convertView.findViewById(R.id.img_icon);
}
}
}
- 继承BaseExpandableListAdapter,需要重写十个方法
主活动代码
package com.example.expandablelistviewtest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
//装子项的泛型数据
private ArrayList>iChildList=new ArrayList<>();
private ExpandableListView expandableListView;
private ListparentList=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//准备数据
initData();
expandableListView=findViewById(R.id.expend_list);
expandableListView.setAdapter(new MyExtendableListViewAdapter(parentList,iChildList));
//设置分组的监听
expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView expandableListView, View view, int groupPosition, long l) {
Toast.makeText(getApplicationContext(), parentList.get(groupPosition), Toast.LENGTH_SHORT).show();
return false;
}
});
//设置子项布局监听
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Toast.makeText(getApplicationContext(), iChildList.get(groupPosition).get(childPosition).getDes(), Toast.LENGTH_SHORT).show();
return true;
}
});
//控制他只能打开一个组
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
int count = parentList.size();
for(int i = 0;i < count;i++){
if (i!=groupPosition){
expandableListView.collapseGroup(i);
}
}
}
});
}
//用链表
public void initData(){
parentList.add("索隆 ");
parentList.add("乔巴");
parentList.add("山治");
parentList.add("路飞");
ArrayList- iData = new ArrayList<>();
Item item1=new Item(R.drawable.aaa,"第一");
iData.add(item1);
Item item2=new Item(R.drawable.bbb,"第二");
iData.add(item2);
Item item3=new Item(R.drawable.ccc,"第三");
iData.add(item3);
Item item4=new Item(R.drawable.ddd,"第四");
iData.add(item4);
iChildList.add(iData);
ArrayList
- iData2 = new ArrayList<>();
Item item5=new Item(R.drawable.aaa,"第一");
iData2.add(item5);
Item item6=new Item(R.drawable.bbb,"第二");
iData2.add(item6);
Item item7=new Item(R.drawable.ccc,"第三");
iData2.add(item7);
Item item8=new Item(R.drawable.ddd,"第四");
iData2.add(item8);
iChildList.add(iData2);
ArrayList
- iData3 = new ArrayList<>();
Item item9=new Item(R.drawable.aaa,"第一");
iData3.add(item9);
Item item10=new Item(R.drawable.bbb,"第二");
iData3.add(item10);
Item item11=new Item(R.drawable.ccc,"第三");
iData3.add(item11);
Item item12=new Item(R.drawable.ddd,"第四");
iData3.add(item12);
iChildList.add(iData3);
ArrayList
- iData4 = new ArrayList<>();
Item item13=new Item(R.drawable.aaa,"第一");
iData4.add(item13);
Item item14=new Item(R.drawable.bbb,"第二");
iData4.add(item14);
Item item15=new Item(R.drawable.ccc,"第三");
iData4.add(item15);
Item item16=new Item(R.drawable.ddd,"第四");
iData4.add(item16);
iChildList.add(iData4);
}
}