上一篇已经详细介绍了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;
/**
* Created by xiaoyehai on 2018/3/15 0015.
*/
public class ChildData {
private String title;
public ChildData() {
}
public ChildData(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;
List childList;
for (int i = 0; i < 5; i++) {
groupData = new GroupData();
groupData.setTitle("分组" + i);
childList = new ArrayList<>();
for (int j = 0; j < 3; j++) {
childData = new ChildData("子列表" + j);
childList.add(childData);
}
groupData.setChildDataList(childList);
mGroupDataList.add(groupData);
}
}
Acyivity完整代码:
package com.xiaoyehai.expandablelistviewdemo.activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;
import com.xiaoyehai.expandablelistviewdemo.R;
import com.xiaoyehai.expandablelistviewdemo.adapter.Main2Adapter;
import com.xiaoyehai.expandablelistviewdemo.adapter.Main3Adapter;
import com.xiaoyehai.expandablelistviewdemo.bean.ChildData;
import com.xiaoyehai.expandablelistviewdemo.bean.GroupData;
import java.util.ArrayList;
import java.util.List;
/**
* 使用集合封装数据
*/
public class Main3Activity extends AppCompatActivity {
private ExpandableListView mExpandableListView;
private List mGroupDataList;
private Main3Adapter mMain3Adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
mExpandableListView = (ExpandableListView) findViewById(R.id.expand_listview);
initData();
mMain3Adapter = new Main3Adapter(this, mGroupDataList);
mExpandableListView.setAdapter(mMain3Adapter);
mExpandableListView.expandGroup(0); //设置默认展开
//更新数据
// mMain3Adapter.notifyDataSetChanged();
initListener();
}
private void initData() {
mGroupDataList = new ArrayList<>();
GroupData groupData;
ChildData childData;
List childList;
for (int i = 0; i < 5; i++) {
groupData = new GroupData();
groupData.setTitle("分组" + i);
childList = new ArrayList<>();
for (int j = 0; j < 3; j++) {
childData = new ChildData("子列表" + j);
childList.add(childData);
}
groupData.setChildDataList(childList);
mGroupDataList.add(groupData);
}
}
private void initListener() {
/**
* 设置分组项的点击监听事件
*/
mExpandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView expandableListView, View view, int groupPosition, long id) {
Toast.makeText(getApplicationContext(), mGroupDataList.get(groupPosition).getTitle(), Toast.LENGTH_SHORT).show();
// 请务必返回 false,否则分组不会展开
return false;
}
});
/**
* 设置子选项点击监听事件
*/
mExpandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Toast.makeText(getApplicationContext(),
mGroupDataList.get(groupPosition).getChildDataList().get(childPosition).getTitle(),
Toast.LENGTH_SHORT).show();
return true;
}
});
/**
* 分组展开的监听事件
*/
mExpandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
}
});
/**
* 分组合并的监听事件
*/
mExpandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
}
});
}
}
适配器:
package com.xiaoyehai.expandablelistviewdemo.adapter;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import com.xiaoyehai.expandablelistviewdemo.R;
import com.xiaoyehai.expandablelistviewdemo.bean.GroupData;
import java.util.List;
/**
* Created by xiaoyehai on 2018/3/21 0021.
*/
public class Main3Adapter extends BaseExpandableListAdapter {
private Context mContext;
private List groupDataList;
public Main3Adapter(Context context, List groupDataList) {
mContext = context;
this.groupDataList = groupDataList;
}
/**
* 获取分组的个数
*
* @return
*/
@Override
public int getGroupCount() {
return groupDataList.size();
}
/**
* 获取指定分组中的子选项的个数
*
* @param groupPosition
* @return
*/
@Override
public int getChildrenCount(int groupPosition) {
return groupDataList.get(groupPosition).getChildDataList().size();
}
/**
* 获取指定的分组数据
*
* @param groupPosition
* @return
*/
@Override
public Object getGroup(int groupPosition) {
return groupDataList.get(groupPosition);
}
/**
* 获取指定分组中的指定子选项数据
*
* @param groupPosition
* @param childPosition
* @return
*/
@Override
public Object getChild(int groupPosition, int childPosition) {
return groupDataList.get(groupPosition).getChildDataList().get(childPosition);
}
/**
* 获取指定分组的ID, 这个ID必须是唯一的
*
* @param groupPosition
* @return
*/
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
/**
* 获取子选项的ID, 这个ID必须是唯一的
*
* @param groupPosition
* @param childPosition
* @return
*/
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
/**
* 分组和子选项是否持有稳定的ID, 就是说底层数据的改变会不会影响到它们。
*
* @return
*/
@Override
public boolean hasStableIds() {
return true;
}
/**
* 获取显示指定分组的视图
*
* @param groupPosition
* @param isExpanded 是否展开
* @param convertView
* @param parent
* @return
*/
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
GroupViewHolder groupViewHolder;
if (convertView == null) {
convertView = View.inflate(mContext, R.layout.item_expand_group, null);
groupViewHolder = new GroupViewHolder(convertView);
convertView.setTag(groupViewHolder);
} else {
groupViewHolder = (GroupViewHolder) convertView.getTag();
}
groupViewHolder.tvTitle.setText(groupDataList.get(groupPosition).getTitle());
return convertView;
}
/**
* 获取显示指定分组中的指定子选项的视图
*
* @param groupPosition
* @param childPosition
* @param isLastChild
* @param convertView
* @param parent
* @return
*/
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ChildViewHolder childViewHolder;
if (convertView == null) {
convertView = View.inflate(mContext, R.layout.item_expand_child, null);
childViewHolder = new ChildViewHolder(convertView);
convertView.setTag(childViewHolder);
} else {
childViewHolder = (ChildViewHolder) convertView.getTag();
}
String title = groupDataList.get(groupPosition).getChildDataList().get(childPosition).getTitle();
childViewHolder.tvTitle.setText(title);
return convertView;
}
/**
* 子项是否可选中,如果需要设置子项的点击事件,需要返回true
*
* @param groupPosition
* @param childPosition
* @return
*/
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
static class GroupViewHolder {
private TextView tvTitle;
public GroupViewHolder(View convertView) {
tvTitle = (TextView) convertView.findViewById(R.id.tv_group_title);
}
}
static class ChildViewHolder {
private TextView tvTitle;
public ChildViewHolder(View convertView) {
tvTitle = (TextView) convertView.findViewById(R.id.tv_child_title);
}
}
}