ExpandableListView 使用解析(三级列表的实现)
在往常的设计中,往往有类似于QQ的二级列表的样式,而使用ExpandableListView变可以实现这种类似的效果。
当然,如果ExpandableListView嵌套ExpandableListView便可以实现三级列表,甚至多级条目,而本博客最终的实现效果便是三级条目。
实体类:
package com.xiaoyehai.expandablelistviewdemo.bean;
import java.util.List;
/**
* Created by xiaoyehai on 2018/3/15 0015.
*/
public class GroupData {
private String title;
private List childDataList;
public GroupData() {
}
public GroupData(String title, List childDataList) {
this.title = title;
this.childDataList = childDataList;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public List getChildDataList() {
return childDataList;
}
public void setChildDataList(List childDataList) {
this.childDataList = childDataList;
}
}
package com.xiaoyehai.expandablelistviewdemo.bean;
import java.util.List;
/**
* Created by xiaoyehai on 2018/3/15 0015.
*/
public class ChildData {
private String title;
private List thirdDataList;
public ChildData() {
}
public ChildData(String title) {
this.title = title;
}
public ChildData(String title, List thirdDataList) {
this.title = title;
this.thirdDataList = thirdDataList;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public List getThirdDataList() {
return thirdDataList;
}
public void setThirdDataList(List thirdDataList) {
this.thirdDataList = thirdDataList;
}
}
package com.xiaoyehai.expandablelistviewdemo.bean;
/**
* Created by xiaoyehai on 2018/3/21 0021.
*/
public class ThirdData {
private String title;
public ThirdData() {
}
public ThirdData(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
封装数据:
private void initData() {
mGroupDataList = new ArrayList<>();
GroupData groupData;
ChildData childData;
ThirdData thirdData;
List childList;
List thirdList;
for (int i = 0; i < 3; i++) {
groupData = new GroupData();
groupData.setTitle("分组" + i);
childList = new ArrayList<>();
for (int j = 0; j < 3; j++) {
childData = new ChildData();
childData.setTitle("二级子列表" + j);
thirdList = new ArrayList<>();
for (int z = 0; z < 3; z++) {
thirdData = new ThirdData("三级列表" + z);
thirdList.add(thirdData);
}
childData.setThirdDataList(thirdList);
childList.add(childData);
}
groupData.setChildDataList(childList);
mGroupDataList.add(groupData);
}
}
Activity:
package com.xiaoyehai.expandablelistviewdemo.activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ExpandableListView;
import com.xiaoyehai.expandablelistviewdemo.R;
import com.xiaoyehai.expandablelistviewdemo.adapter.GroupAdapter;
import com.xiaoyehai.expandablelistviewdemo.bean.ChildData;
import com.xiaoyehai.expandablelistviewdemo.bean.GroupData;
import com.xiaoyehai.expandablelistviewdemo.bean.ThirdData;
import java.util.ArrayList;
import java.util.List;
/**
* ExpandableListView嵌套ExpandableListView
*/
public class Main4Activity extends AppCompatActivity {
private ExpandableListView mExpandableListView;
private ArrayList mGroupDataList;
private GroupAdapter mGroupAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
mExpandableListView = (ExpandableListView) findViewById(R.id.expand_listview);
initData();
mGroupAdapter = new GroupAdapter(this, mGroupDataList);
mExpandableListView.setAdapter(mGroupAdapter);
}
private void initData() {
mGroupDataList = new ArrayList<>();
GroupData groupData;
ChildData childData;
ThirdData thirdData;
List childList;
List thirdList;
for (int i = 0; i < 3; i++) {
groupData = new GroupData();
groupData.setTitle("分组" + i);
childList = new ArrayList<>();
for (int j = 0; j < 3; j++) {
childData = new ChildData();
childData.setTitle("二级子列表" + j);
thirdList = new ArrayList<>();
for (int z = 0; z < 3; z++) {
thirdData = new ThirdData("三级列表" + z);
thirdList.add(thirdData);
}
childData.setThirdDataList(thirdList);
childList.add(childData);
}
groupData.setChildDataList(childList);
mGroupDataList.add(groupData);
}
}
}
适配器:
package com.xiaoyehai.expandablelistviewdemo.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import com.xiaoyehai.expandablelistviewdemo.R;
import com.xiaoyehai.expandablelistviewdemo.activity.Main4Activity;
import com.xiaoyehai.expandablelistviewdemo.bean.GroupData;
import com.xiaoyehai.expandablelistviewdemo.widget.CustomExpandableListView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by xiaoyehai on 2018/3/21 0021.
*/
public class GroupAdapter extends BaseExpandableListAdapter {
private Context mContext;
private List groupDataList;
public GroupAdapter(Context context, List groupDataList) {
mContext = context;
this.groupDataList = groupDataList;
}
@Override
public int getGroupCount() {
return groupDataList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return groupDataList.get(groupPosition).getChildDataList().size();
}
@Override
public Object getGroup(int groupPosition) {
return groupDataList.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return groupDataList.get(groupPosition).getChildDataList().size();
}
@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) {
GroupHolder holder = null;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.parent_group_item, null);
holder = new GroupHolder(convertView);
convertView.setTag(holder);
} else {
holder = (GroupHolder) convertView.getTag();
}
holder.tvParent.setText(groupDataList.get(groupPosition).getTitle());
//设置指示器
if (isExpanded) {
holder.ivArrow.setImageResource(R.drawable.tree_expand);
} else {
holder.ivArrow.setImageResource(R.drawable.tree_econpand);
}
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ExpandableListView eListView = getExpandableListView();
ChildAdapter childAdapter = new ChildAdapter(mContext, groupDataList.get(groupPosition).getChildDataList());
eListView.setAdapter(childAdapter);
return eListView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
/**
* 动态创建子ExpandableListView
*/
public ExpandableListView getExpandableListView() {
CustomExpandableListView mExpandableListView = new CustomExpandableListView(mContext);
//mExpandableListView.setDividerHeight(0);// 取消group项的分割线
//mExpandableListView.setChildDivider(null);// 取消child项的分割线
mExpandableListView.setGroupIndicator(null);// 取消展开折叠的指示图标
return mExpandableListView;
}
static class GroupHolder {
private TextView tvParent;
private ImageView ivArrow;
public GroupHolder(View v) {
tvParent = (TextView) v.findViewById(R.id.tv_parent);
ivArrow = (ImageView) v.findViewById(R.id.iv_arrow);
}
}
}
package com.xiaoyehai.expandablelistviewdemo.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import com.xiaoyehai.expandablelistviewdemo.R;
import com.xiaoyehai.expandablelistviewdemo.bean.ChildData;
import com.xiaoyehai.expandablelistviewdemo.bean.GroupData;
import java.util.List;
/**
* Created by xiaoyehai on 2018/3/21 0021.
*/
public class ChildAdapter extends BaseExpandableListAdapter {
private Context mContext;
private List dataList;
public ChildAdapter(Context context, List dataList) {
mContext = context;
this.dataList = dataList;
}
@Override
public int getGroupCount() {
return dataList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return dataList.get(groupPosition).getThirdDataList().size();
}
@Override
public Object getGroup(int groupPosition) {
return dataList.get(groupPosition).getTitle();
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return dataList.get(groupPosition).getThirdDataList().get(childPosition).getTitle();
}
@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) {
GroupHolder holder = null;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.child_group_item, null);
holder = new GroupHolder(convertView);
convertView.setTag(holder);
} else {
holder = (GroupHolder) convertView.getTag();
}
holder.tvTitle.setText(dataList.get(groupPosition).getTitle());
//设置指示器
if (isExpanded) {
holder.ivArrow.setImageResource(R.drawable.tree_expand);
} else {
holder.ivArrow.setImageResource(R.drawable.tree_econpand);
}
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ChildHolder holder = null;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.child_child_item, null);
holder = new ChildHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ChildHolder) convertView.getTag();
}
holder.tvTitle.setText(dataList.get(groupPosition).getThirdDataList().get(childPosition).getTitle());
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
static class GroupHolder {
private TextView tvTitle;
private ImageView ivArrow;
public GroupHolder(View v) {
tvTitle = (TextView) v.findViewById(R.id.tv_title);
ivArrow = (ImageView) v.findViewById(R.id.iv_arrow);
}
}
static class ChildHolder {
private TextView tvTitle;
public ChildHolder(View v) {
tvTitle = (TextView) v.findViewById(R.id.tv_title);
}
}
}
自定义ExpandableListView 解决嵌套之下显示不全的问题:
package com.xiaoyehai.expandablelistviewdemo.widget;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ExpandableListView;
/**
* 自定义ExpandableListView 解决嵌套之下显示不全的问题
* Created by xiaoyehai on 2018/3/1 0001.
*/
public class CustomExpandableListView extends ExpandableListView {
public CustomExpandableListView(Context context) {
super(context);
}
public CustomExpandableListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomExpandableListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// 解决显示不全的问题
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}