public class MainActivity extends Activity {
private ExpandableListView expandLv;
//初始化数据
private String[] group = {"西游记","红楼梦","三国","水浒传"};
private String[][] child = {{"孙悟空","唐僧","猪八戒","沙悟净"},
{"贾宝玉","林黛玉","王熙凤","刘姥姥"},
{"张飞","关羽","刘备","曹操"},{"潘金莲","武大郎","西门庆","武松"}};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
/**
* 初始化View
*/
private void initView() {
expandLv = (ExpandableListView) findViewById(R.id.expandLv);
expandLv.setAdapter(new MyExpandAdapter());
//设置组的指示器
//expandLv.setGroupIndicator(null);
//设置组展开监,用来监听当前这个组是否展开
expandLv.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(MainActivity.this, group[groupPosition], 1).show();
}
});
//组的点击事件
/*expandLv.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
Toast.makeText(MainActivity.this, group[groupPosition], 1).show();
return false;
}
});*/
//设置子条目点击事件
expandLv.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(MainActivity.this, child[groupPosition][childPosition], 1).show();
return false;
}
});
}
//JDK 7 之前接口只能有方法的声明,不能有方法的是实现 JDK 8 中,接口中存在实例方法,这是可以的
class MyExpandAdapter extends BaseExpandableListAdapter{
@Override
public int getGroupCount() {
//获取组的条目数
return group.length;
}
@Override
public int getChildrenCount(int groupPosition) {
//获取指定组中的子条目数
return child[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition) {
//获取指定组的条目数据
return group[groupPosition];
}
@Override
public Object getChild(int groupPosition, int childPosition) {
//获取指定组中的子条目数据
return child[groupPosition][childPosition];
}
@Override
public long getGroupId(int groupPosition) {
//获取指定组中的ID ID 是唯一 相当于数组或者集合中角标
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
//获取指定组中的子条目的ID ID 是唯一 相当于数组或者集合中角标
return childPosition;
}
@Override
public boolean hasStableIds() {
//判断子条目数是属于那个组的
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
//获取指定组的视图 getView()
if(convertView == null){
convertView = convertView.inflate(MainActivity.this, R.layout.item, null);
}
TextView tvContent = (TextView) convertView.findViewById(R.id.tvContent);
tvContent.setText(group[groupPosition]);
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
//获取指定组的子条目视图
if(convertView == null){
convertView = convertView.inflate(MainActivity.this, R.layout.item2, null);
}
TextView tvContent = (TextView) convertView.findViewById(R.id.tv);
tvContent.setText(child[groupPosition][childPosition]);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
//判断指定组中的子条目是否可以被选中
return true;
}
}
private ExpandableListView expandLv;
//初始化数据
private String[] group = {"西游记","红楼梦","三国","水浒传"};
private String[][] child = {{"孙悟空","唐僧","猪八戒","沙悟净"},
{"贾宝玉","林黛玉","王熙凤","刘姥姥"},
{"张飞","关羽","刘备","曹操"},{"潘金莲","武大郎","西门庆","武松"}};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
/**
* 初始化View
*/
private void initView() {
expandLv = (ExpandableListView) findViewById(R.id.expandLv);
expandLv.setAdapter(new MyExpandAdapter());
//设置组的指示器
//expandLv.setGroupIndicator(null);
//设置组展开监,用来监听当前这个组是否展开
expandLv.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(MainActivity.this, group[groupPosition], 1).show();
}
});
//组的点击事件
/*expandLv.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
Toast.makeText(MainActivity.this, group[groupPosition], 1).show();
return false;
}
});*/
//设置子条目点击事件
expandLv.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(MainActivity.this, child[groupPosition][childPosition], 1).show();
return false;
}
});
}
//JDK 7 之前接口只能有方法的声明,不能有方法的是实现 JDK 8 中,接口中存在实例方法,这是可以的
class MyExpandAdapter extends BaseExpandableListAdapter{
@Override
public int getGroupCount() {
//获取组的条目数
return group.length;
}
@Override
public int getChildrenCount(int groupPosition) {
//获取指定组中的子条目数
return child[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition) {
//获取指定组的条目数据
return group[groupPosition];
}
@Override
public Object getChild(int groupPosition, int childPosition) {
//获取指定组中的子条目数据
return child[groupPosition][childPosition];
}
@Override
public long getGroupId(int groupPosition) {
//获取指定组中的ID ID 是唯一 相当于数组或者集合中角标
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
//获取指定组中的子条目的ID ID 是唯一 相当于数组或者集合中角标
return childPosition;
}
@Override
public boolean hasStableIds() {
//判断子条目数是属于那个组的
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
//获取指定组的视图 getView()
if(convertView == null){
convertView = convertView.inflate(MainActivity.this, R.layout.item, null);
}
TextView tvContent = (TextView) convertView.findViewById(R.id.tvContent);
tvContent.setText(group[groupPosition]);
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
//获取指定组的子条目视图
if(convertView == null){
convertView = convertView.inflate(MainActivity.this, R.layout.item2, null);
}
TextView tvContent = (TextView) convertView.findViewById(R.id.tv);
tvContent.setText(child[groupPosition][childPosition]);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
//判断指定组中的子条目是否可以被选中
return true;
}
}