首先要明确,可折叠列表在每个项是包含子项的,那么肯定会用到集合嵌套!
下面是封装的两个实体类:
package com.yx.pojo;
public class Chid {
private int img;
private String txt;
public int getImg() {
return img;
}
public void setImg(int img) {
this.img = img;
}
public String getTxt() {
return txt;
}
public void setTxt(String txt) {
this.txt = txt;
}
}
package com.yx.pojo;
import java.util.ArrayList;
import java.util.List;
public class Group {
private int img;
private String txt;
private List<Chid> list_child = new ArrayList<Chid>();
public List<Chid> getList_child() {
return list_child;
}
public void setList_child(List<Chid> list_child) {
this.list_child = list_child;
}
public int getImg() {
return img;
}
public void setImg(int img) {
this.img = img;
}
public String getTxt() {
return txt;
}
public void setTxt(String txt) {
this.txt = txt;
}
}
下面,既然是适配器,那一定要有适配器组件,本次用到的是ExpandableListView ,即在布局文件中添加该控件,简单设置width height id 即可
在activity文件中,获取适配器组件并绑定适配器!
package com.yx.android_day1004;
import java.util.ArrayList;
import java.util.List;
import com.example.android_day1004.R;
import com.yx.adapter.MyExpandAdapter;
import com.yx.pojo.Chid;
import com.yx.pojo.Group;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ExpandableListView;
public class SecondActivity extends Activity {
private static final String TAG = "tag";
// 准备数据
private List<Group> list_data;
private ExpandableListView mExpandList;
private int[] images = { R.drawable.headimage01, R.drawable.headimage02, R.drawable.headimage03 };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
initData();
intiView();
}
private void intiView() {
mExpandList = (ExpandableListView) findViewById(R.id.expanded_list);
MyExpandAdapter adapter = new MyExpandAdapter(this, list_data);
mExpandList.setAdapter(adapter);
}
private void initData() {
list_data = new ArrayList<Group>();
for (int i = 0; i < 5; i++) {
Group group = new Group();
group.setImg(R.drawable.ic_launcher);
group.setTxt("组" + (i + 1));
List<Chid> child = new ArrayList<Chid>();
for (int j = 0; j < 3; j++) {
Chid chid = new Chid();
chid.setImg(images[j]);
chid.setTxt("子" + (j + 1));
child.add(chid);
}
group.getList_child().addAll(child);
list_data.add(group);
}
}
}
下面就是最重要的自定义适配器的部分啦。。。。。。。。。
package com.yx.adapter;
import java.util.List;
import com.example.android_day1004.R;
import com.yx.pojo.Group;
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;
/**
* Created by Administrator on 2016/10/4.
*/
public class MyExpandAdapter extends BaseExpandableListAdapter {
private List<Group> list_data;
private Context context;
public MyExpandAdapter(Context context, List<Group> list_data) {
this.context = context;
this.list_data = list_data;
}
@Override
public int getGroupCount() {
return list_data.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return list_data.get(groupPosition).getList_child().size();
}
@Override
public Object getGroup(int groupPosition) {
return list_data.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return list_data.get(groupPosition).getList_child().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 true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
HolderGroup group = null;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.activity_second_group_item, null);
group = new HolderGroup(convertView);
convertView.setTag(group);
} else {
group = (HolderGroup) convertView.getTag();
}
group.img.setImageResource(list_data.get(groupPosition).getImg());
group.tv.setText(list_data.get(groupPosition).getTxt());
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
ViewGroup parent) {
HolderChild child = null;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.activity_second_child_item, null);
child = new HolderChild(convertView);
convertView.setTag(child);
} else {
child = (HolderChild) convertView.getTag();
}
child.img.setImageResource(list_data.get(groupPosition).getList_child().get(childPosition).getImg());
child.tv.setText(list_data.get(groupPosition).getList_child().get(childPosition).getTxt());
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
class HolderGroup {
ImageView img;
TextView tv;
public HolderGroup(View convertView) {
img = (ImageView) convertView.findViewById(R.id.img_second_group_item);
tv = (TextView) convertView.findViewById(R.id.tv_second_group_item);
}
}
class HolderChild {
ImageView img;
TextView tv;
public HolderChild(View convertView) {
img = (ImageView) convertView.findViewById(R.id.img_second_child_item);
tv = (TextView) convertView.findViewById(R.id.tv_second_child_item);
}
}
}
本文中,一共有两个小细节需要注意
1、Group中的List一定要记得new出来,不然在用的时候会报空指针异常,然后又各种找不到就惨啦。。。。。
2、由于我们的ExpandableListView控件默认会在组上面加一个小箭头,记得让你的组布局项向右移一下,给它留出空间,不然不然。。丑的话自己忍。。。
en ,效果在这。。
总之,这个适配器最难的地方就是需要嵌套,保持思路清晰并不是特别难。ok