Adapter:
package com.example.websocketchaifen.activity;
import android.content.Context;
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 com.example.websocketchaifen.R;
import java.util.List;
/**
* author : ZJS
* e-mail : [email protected]
* date : 20-9-4上午11:25
* desc :
* version : 1.0
*/
public class myAdapter extends BaseExpandableListAdapter {
Context context;
LayoutInflater mInflater;
String[] mGroupStrings;
List<List<Item>> mData;
public myAdapter(Context context, List<List<Item>> mData) {
this.context = context;
this.mData = mData;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mGroupStrings = new String[]{
"第1个", "第2个", "第3个", "第4个"};
}
//获取子列表项对应的Item
@Override
public Item getChild(int groupPosition, int childPosition) {
return mData.get(groupPosition).get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
//获得子列表项的数目
@Override
public int getChildrenCount(int groupPosition) {
return mData.get(groupPosition).size();
}
//获得父列表项
@Override
public Object getGroup(int groupPosition) {
return mData.get(groupPosition);
}
//获得父列表项的数目
@Override
public int getGroupCount() {
return mData.size();
}
//获得父列表项的Id
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
//子列表项是否能否触发事件,返回true则为可以响应点击
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.group_item_layout, null);
}
GroupViewHolder holder = new GroupViewHolder();
holder.mGroupName = (TextView) convertView.findViewById(R.id.group_name);
holder.mGroupName.setText(mGroupStrings[groupPosition]);
holder.mGroupCount = (TextView) convertView.findViewById(R.id.group_number);
holder.mGroupCount.setText("[" + mData.get(groupPosition).size() + "]");
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
//如果第一次调用则convertView为null,需要获得对应的layout布局文件:子列表项的布局R.layout.child_item_layout
convertView = mInflater.inflate(R.layout.child_item_layout, null);
}
//子列表项的布局文件的View类,具体看下面的类定义
ChildViewHolder holder = new ChildViewHolder();
holder.mIcon = (ImageView) convertView.findViewById(R.id.img);
holder.mChildName = (TextView) convertView.findViewById(R.id.item_name);
holder.mChildName.setText(getChild(groupPosition, childPosition).getName());
holder.mDetail = (TextView) convertView.findViewById(R.id.item_detail);
holder.mDetail.setText(getChild(groupPosition, childPosition).getDetail());
return convertView;
}
private class GroupViewHolder {
TextView mGroupName;
TextView mGroupCount;
}
private class ChildViewHolder {
ImageView mIcon;
TextView mChildName;
TextView mDetail;
}
}
bean
package com.example.websocketchaifen.activity;
/**
* author : ZJS
* e-mail : [email protected]
* date : 20-9-4上午11:26
* desc :
* version : 1.0
*/
public class Item {
private int resId; //图片(头像)的Id
private String name; //子列表项的名字(QQ昵称)
private String detail; //子列表项的详细信息(QQ签名)
public int getResId() {
return resId;
}
public void setResId(int resId) {
this.resId = resId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
}
main_layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".activity.ceshi">
<ExpandableListView
android:id="@+id/expandableListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
</androidx.constraintlayout.widget.ConstraintLayout>
child_item_layout.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="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_lx_n" />
<TextView
android:text="aaaa"
android:id="@+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="aaaaaaaa"
android:id="@+id/item_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
group_layout
<?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="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:orientation="horizontal">
<TextView
android:id="@+id/group_name"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="AAAA" />
<TextView
android:id="@+id/group_number"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="AAAA" />
</LinearLayout>
</LinearLayout>
main_activity
package com.example.websocketchaifen.activity;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ExpandableListView;
import com.example.websocketchaifen.R;
import java.util.ArrayList;
import java.util.List;
public class ceshi extends AppCompatActivity {
private ExpandableListView listView;
private List<List<Item>> data = new ArrayList<>();
private List<Item> mdata = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ceshi);
Item item = new Item();
item.setName("A");
item.setDetail("A");
mdata.add(item);
for (int i = 0; i < 4; i++) {
data.add(mdata);
}
listView = (ExpandableListView) findViewById(R.id.expandableListView);
myAdapter adapter = new myAdapter(this, data);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}