class ListAdapter
package cn.noybzy.explist;
import android.content.Context;
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 cn.noybzy.explist.bean.ListBean;
import cn.noybzy.explist.bean.User;
import cn.noybzy.login.R;
public class ListAdapter extends BaseExpandableListAdapter {
private ArrayList<ListBean> list;
private Context context;
public ListAdapter(ArrayList<ListBean> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public int getGroupCount() {
return list.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return list.get(groupPosition).getUsers().size();
}
@Override
public ListBean getGroup(int groupPosition) {
return list.get(groupPosition);
}
@Override
public User getChild(int groupPosition, int childPosition) {
return list.get(groupPosition).getUsers().get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
GroupHolder groupHolder = null;
if (convertView == null){
convertView = View.inflate(context, R.layout.activity_expandable_listview_group_item,null);
groupHolder = new GroupHolder();
groupHolder.groupTitle = convertView.findViewById(R.id.group_title);
groupHolder.groupOnline = convertView.findViewById(R.id.group_online);
convertView.setTag(groupHolder);
}else {
groupHolder = (GroupHolder) convertView.getTag();
}
ListBean group = getGroup(groupPosition);
groupHolder.groupTitle.setText(group.getGroupTitle());
groupHolder.groupOnline.setText(group.getOnline() + "/" + group.getSum());
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ChildHolder childHolder = null;
if (convertView == null){
convertView = View.inflate(context, R.layout.activity_expandable_listview_child_item,null);
childHolder = new ChildHolder();
childHolder.pic = convertView.findViewById(R.id.pic);
childHolder.username = convertView.findViewById(R.id.username);
convertView.setTag(childHolder);
}else {
childHolder = (ChildHolder) convertView.getTag();
}
User child = getChild(groupPosition, childPosition);
// childHolder.pic.setImageResource(1);
childHolder.username.setText(child.getName());
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
class GroupHolder{
private TextView groupTitle;
private TextView groupOnline;
}
class ChildHolder{
private ImageView pic;
private TextView username;
}
}
class DemoExpandableListView
package cn.noybzy.explist;
import android.os.Bundle;
import android.widget.ExpandableListView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.Random;
import cn.noybzy.explist.bean.ListBean;
import cn.noybzy.explist.bean.User;
import cn.noybzy.login.R;
public class DemoExpandableListView extends AppCompatActivity {
private ExpandableListView listView;
private ArrayList<ListBean> list;
private static final int N = 100;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_expandable_listview);
listView = findViewById(R.id.ExpandableListView);
initData();
listView.setAdapter(new ListAdapter(list,this));
}
private void initData(){
Random random = new Random();
list = new ArrayList<>();
for (int i = 0; i < random.nextInt(N); i++) {
ArrayList<User> users = new ArrayList<>();
for (int j = 0; j < random.nextInt(N); j++) {
users.add(new User("姓名","pic","在线","消息"));
}
ListBean listBean = new ListBean();
listBean.setGroupTitle("分类");
listBean.setSum(users.size());
listBean.setOnline(random.nextInt(users.size()));
listBean.setUsers(users);
list.add(listBean);
}
}
}
class ListBean
package cn.noybzy.explist.bean;
import java.util.ArrayList;
public class ListBean {
private String groupTitle;
private int sum;
private int online;
private ArrayList<User> users;
public ListBean() {
}
public void setGroupTitle(String groupTitle) {
this.groupTitle = groupTitle;
}
public void setSum(int sum) {
this.sum = sum;
}
public void setOnline(int online) {
this.online = online;
}
public void setUsers(ArrayList<User> users) {
this.users = users;
}
public String getGroupTitle() {
return groupTitle;
}
public int getSum() {
return sum;
}
public int getOnline() {
return online;
}
public ArrayList<User> getUsers() {
return users;
}
public ListBean(String groupTitle, int sum, int online, ArrayList<User> users) {
this.groupTitle = groupTitle;
this.sum = sum;
this.online = online;
this.users = users;
}
@Override
public String toString() {
return "ListBean{" +
"groupTitle='" + groupTitle + '\'' +
", sum=" + sum +
", online=" + online +
", users=" + users +
'}';
}
}
class User
package cn.noybzy.explist.bean;
public class User {
private String name;
private String pic;
private String onlineMeg;
private String meg;
public User(String name, String pic, String onlineMeg, String meg) {
this.name = name;
this.pic = pic;
this.onlineMeg = onlineMeg;
this.meg = meg;
}
public User() {
}
public String getName() {
return name;
}
public String getPic() {
return pic;
}
public String getOnlineMeg() {
return onlineMeg;
}
public String getMeg() {
return meg;
}
public void setName(String name) {
this.name = name;
}
public void setPic(String pic) {
this.pic = pic;
}
public void setOnlineMeg(String onlineMeg) {
this.onlineMeg = onlineMeg;
}
public void setMeg(String meg) {
this.meg = meg;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", pic='" + pic + '\'' +
", onlineMeg='" + onlineMeg + '\'' +
", meg='" + meg + '\'' +
'}';
}
}