ExpandableListView数据适配,子层加载不同布局的方法

ExpandableListView控件类是于ListView控件,其数据的适配显示通过继承BaseExpandableListAdapter来实现,不多说了,看代码:

实体类,用来封装数据:

ContentEntity.java

public class ContentEntity {
private String name;
private String info;
private String time;
private int type;
private String schoolName;
private int image;

public int getImage() {
return image;
}

public void setImage(int image) {
this.image = image;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getInfo() {
return info;
}

public void setInfo(String info) {
this.info = info;
}

public String getTime() {
return time;
}

public void setTime(String time) {
this.time = time;
}

public int getType() {
return type;
}

public void setType(int type) {
this.type = type;
}

public String getSchoolName() {
return schoolName;
}

public void setSchoolName(String schoolName) {
this.schoolName = schoolName;
}

@Override
public String toString() {
return "ContentEntity [name=" + name + ", info=" + info + ", time=" + time + ", type=" + type + ", schoolName="
+ schoolName + ", image=" + image + "]";
}
}

MainActivity布局文件:

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >
    <ExpandableListView
        android:id="@+id/elv"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

MainActivity.java文件:

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ExpandableListView;

public class MainActivity extends Activity {
private ExpandableListView m_elv;
private List<String> father_List;// 父层数据
private List<List<ContentEntity>> list_Son;// 子层数据
private int[] img = { R.drawable.zf, R.drawable.wk };

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m_elv = (ExpandableListView) findViewById(R.id.elv);
list_Son = new ArrayList<>();
ContentEntity contentEntity01 = new ContentEntity();
contentEntity01.setName("张飞");
contentEntity01.setInfo("车骑将军");
contentEntity01.setTime("公元184年(中平元年)");
contentEntity01.setImage(img[0]);
contentEntity01.setType(1);

ContentEntity contentEntity02 = new ContentEntity();
contentEntity02.setName("武松");
contentEntity02.setInfo("行者");
contentEntity02.setTime("公元1085年");
contentEntity02.setSchoolName("武松打虎");
contentEntity02.setType(3);

ContentEntity contentEntity03 = new ContentEntity();
contentEntity03.setName("孙悟空");
contentEntity03.setInfo("美猴王");
contentEntity03.setTime("娑婆世界东胜神洲傲来国花果山");
contentEntity03.setImage(img[1]);
contentEntity03.setType(1);

                ContentEntity contentEntity04 = new ContentEntity();
contentEntity04.setName("贾宝玉");
contentEntity04.setInfo("宝二爷,怡红公子,绛洞花主");
contentEntity04.setTime("康熙四十五年丙戌");
contentEntity04.setSchoolName("大观园女儿国中唯一的男性居民");
contentEntity04.setType(3);

List<ContentEntity> son_List01 = new ArrayList<>();
son_List01.add(contentEntity01);
List<ContentEntity> son_List02 = new ArrayList<>();
son_List02.add(contentEntity02);
List<ContentEntity> son_List03 = new ArrayList<>();
son_List03.add(contentEntity03);
List<ContentEntity> son_List04 = new ArrayList<>();
son_List04.add(contentEntity04);
// 添加子目录数据
list_Son.add(son_List01);
list_Son.add(son_List02);
list_Son.add(son_List03);
list_Son.add(son_List04);
// 添加父目录数据
father_List = new ArrayList<>();
father_List.add("三国");
father_List.add("水浒");
father_List.add("西游");
father_List.add("红楼梦");
// 数据适配
ContentAdapter adapter = new ContentAdapter(this, father_List, list_Son);
m_elv.setAdapter(adapter);
}
}

适配器ContentAdapter.java文件:

import java.util.List;
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;


public class ContentAdapter extends BaseExpandableListAdapter {
private List<List<ContentEntity>> son_List;
private List<String> father_List;
private Context context;
private final int TYPE_1 = 0;
private final int TYPE_2 = 1;

public ContentAdapter(Context context, List<String> father_List, List<List<ContentEntity>> list_Son) {
super();
this.context = context;
this.father_List = father_List;
this.son_List = list_Son;
}

@Override
public int getGroupCount() {
return father_List != null ? father_List.size() : 0;
}

@Override
public int getChildrenCount(int groupPosition) {
return son_List != null ? son_List.get(groupPosition).size() : 0;
}

@Override
public Object getGroup(int groupPosition) {
return father_List.get(groupPosition);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
return son_List.get(groupPosition).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 boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}

@Override
public int getChildTypeCount() {
return 2;
}

@Override
public int getChildType(int groupPosition, int childPosition) {
int type = son_List.get(groupPosition).get(childPosition).getType();
if (type == 1) {
return TYPE_1;
}
return TYPE_2;
}

// 父层控件视图加载
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
ViewHolderFather father_vh;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.father_item, null);
father_vh = new ViewHolderFather();
father_vh.tvFather = (TextView) convertView.findViewById(R.id.tv_father);
convertView.setTag(father_vh);
} else {
father_vh = (ViewHolderFather) convertView.getTag();
}
father_vh.tvFather.setText(father_List.get(groupPosition));
return convertView;
}

// 子层控件视图加载
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
ViewGroup parent) {
ContentEntity contentEntity = son_List.get(groupPosition).get(childPosition);
int type = getChildType(groupPosition, childPosition);
switch (type) {
case TYPE_1:
ViewHolderSon01 son01;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.son_item01, null);
son01 = new ViewHolderSon01();
son01.tv_info01 = (TextView) convertView.findViewById(R.id.tv_info01);
son01.tv_name01 = (TextView) convertView.findViewById(R.id.tv_name01);
son01.tv_time01 = (TextView) convertView.findViewById(R.id.tv_time01);
son01.iv_pic = (ImageView) convertView.findViewById(R.id.iv_pic);
convertView.setTag(son01);
} else {
son01 = (ViewHolderSon01) convertView.getTag();
}
son01.tv_time01.setText(contentEntity.getTime());
son01.tv_name01.setText(contentEntity.getName());
son01.tv_info01.setText(contentEntity.getInfo());
son01.iv_pic.setImageResource(contentEntity.getImage());
break;
case TYPE_2:
ViewHolderSon03 son03;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.son_item02, null);
son03 = new ViewHolderSon03();
son03.tv_info03 = (TextView) convertView.findViewById(R.id.tv_info03);
son03.tv_name03 = (TextView) convertView.findViewById(R.id.tv_name03);
son03.tv_time03 = (TextView) convertView.findViewById(R.id.tv_time03);
son03.tv_scname03 = (TextView) convertView.findViewById(R.id.tv_scname03);
convertView.setTag(son03);
} else {
son03 = (ViewHolderSon03) convertView.getTag();
}
son03.tv_time03.setText(contentEntity.getTime());
son03.tv_name03.setText(contentEntity.getName());
son03.tv_info03.setText(contentEntity.getInfo());
son03.tv_scname03.setText(contentEntity.getSchoolName());
break;
}
return convertView;
}

class ViewHolderFather {
TextView tvFather;
}

class ViewHolderSon01 {
TextView tv_info01, tv_time01, tv_name01;
ImageView iv_pic;
}

class ViewHolderSon03 {
TextView tv_info03, tv_time03, tv_name03, tv_scname03;
}
}

son_item01.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" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <ImageView
            android:id="@+id/iv_pic"
            android:layout_width="60dp"
            android:layout_height="60dp"/>
        <TextView
            android:id="@+id/tv_name01"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/iv_pic"
            android:paddingLeft="15dp" />
        <TextView
            android:id="@+id/tv_info01"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tv_name01"
            android:layout_toRightOf="@+id/iv_pic"
            android:paddingLeft="15dp" />
        <TextView
            android:id="@+id/tv_time01"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tv_info01"
            android:layout_toRightOf="@+id/iv_pic"
            android:paddingLeft="15dp"/>
    </RelativeLayout>
</LinearLayout>

son_item02.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" >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <TextView
            android:id="@+id/tv_name03"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="25dp" />
        <TextView
            android:id="@+id/tv_info03"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tv_name03"
            android:paddingLeft="25dp" />
        <TextView
            android:id="@+id/tv_time03"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tv_info03"
            android:paddingLeft="25dp" />
        <TextView
            android:id="@+id/tv_scname03"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tv_time03"
            android:paddingLeft="25dp"/>
    </RelativeLayout>
</LinearLayout>


运行效果:


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值