package com.example.expandlistview;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.database.DataSetObserver;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
//一级标题的列表
private List<String> groupList;
//二级标题的列表
private List<List<String>> itemList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ExpandableListView exlistview=(ExpandableListView) findViewById(R.id.exlistview);
//初使化数据源
initData();
//设置适配器
exlistview.setAdapter(new MyAdapter());
//一级标题的点击事件
exlistview.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
Toast.makeText(MainActivity.this, "第"+groupPosition+"组被点击", 0).show();
return false;
}
});
//二级标题的点击事件 (注意:需要将适配器中的 isChildSelectable 这个方法的返回值设置成true)
exlistview.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(MainActivity.this, itemList.get(groupPosition).get(childPosition), 0).show();
return false;
}
});
}
private void initData() {
groupList=new ArrayList<String>();
groupList.add("我的好友");
groupList.add("我的家人");
groupList.add("我的同学");
List<String> items=new ArrayList<String>();
items.add("好友1");
items.add("好友2");
List<String> items1=new ArrayList<String>();
items1.add("家人1");
items1.add("家人2");
List<String> items2=new ArrayList<String>();
items2.add("同学1");
items2.add("同学2");
itemList=new ArrayList<List<String>>();
itemList.add(items);
itemList.add(items1);
itemList.add(items2);
}
//构建适配器 实现接口 ExpandableListAdapter--
//还有一种构建适配器的方法是去 继承BaseExpandableListAdapter这个类
//最主要是去 实现四个方法 getGroupCount、getChildrenCount、getGroupView、getChildView
class MyAdapter implements ExpandableListAdapter{
@Override
public int getGroupCount() {
//返回一级标题的个数
return groupList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
//返回一级标题对应的二级标题的个数
return itemList.get(groupPosition).size();
}
//得到一级标题的View
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View vv=View.inflate(MainActivity.this, R.layout.group_layout, null);
TextView tv_group=(TextView)vv.findViewById(R.id.tv_group);
tv_group.setText(groupList.get(groupPosition));
return vv;
}
//得到二级标题的View
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View itemView=View.inflate(MainActivity.this, R.layout.goup_item_layout, null);
TextView tv_group_item=(TextView)itemView.findViewById(R.id.tv_group_item);
tv_group_item.setText(itemList.get(groupPosition).get(childPosition));
return itemView;
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return null;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return 0;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean areAllItemsEnabled() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
@Override
public void onGroupExpanded(int groupPosition) {
// TODO Auto-generated method stub
}
@Override
public void onGroupCollapsed(int groupPosition) {
// TODO Auto-generated method stub
}
@Override
public long getCombinedChildId(long groupId, long childId) {
// TODO Auto-generated method stub
return 0;
}
@Override
public long getCombinedGroupId(long groupId) {
// TODO Auto-generated method stub
return 0;
}
}
}
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.database.DataSetObserver;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
//一级标题的列表
private List<String> groupList;
//二级标题的列表
private List<List<String>> itemList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ExpandableListView exlistview=(ExpandableListView) findViewById(R.id.exlistview);
//初使化数据源
initData();
//设置适配器
exlistview.setAdapter(new MyAdapter());
//一级标题的点击事件
exlistview.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
Toast.makeText(MainActivity.this, "第"+groupPosition+"组被点击", 0).show();
return false;
}
});
//二级标题的点击事件 (注意:需要将适配器中的 isChildSelectable 这个方法的返回值设置成true)
exlistview.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(MainActivity.this, itemList.get(groupPosition).get(childPosition), 0).show();
return false;
}
});
}
private void initData() {
groupList=new ArrayList<String>();
groupList.add("我的好友");
groupList.add("我的家人");
groupList.add("我的同学");
List<String> items=new ArrayList<String>();
items.add("好友1");
items.add("好友2");
List<String> items1=new ArrayList<String>();
items1.add("家人1");
items1.add("家人2");
List<String> items2=new ArrayList<String>();
items2.add("同学1");
items2.add("同学2");
itemList=new ArrayList<List<String>>();
itemList.add(items);
itemList.add(items1);
itemList.add(items2);
}
//构建适配器 实现接口 ExpandableListAdapter--
//还有一种构建适配器的方法是去 继承BaseExpandableListAdapter这个类
//最主要是去 实现四个方法 getGroupCount、getChildrenCount、getGroupView、getChildView
class MyAdapter implements ExpandableListAdapter{
@Override
public int getGroupCount() {
//返回一级标题的个数
return groupList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
//返回一级标题对应的二级标题的个数
return itemList.get(groupPosition).size();
}
//得到一级标题的View
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View vv=View.inflate(MainActivity.this, R.layout.group_layout, null);
TextView tv_group=(TextView)vv.findViewById(R.id.tv_group);
tv_group.setText(groupList.get(groupPosition));
return vv;
}
//得到二级标题的View
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View itemView=View.inflate(MainActivity.this, R.layout.goup_item_layout, null);
TextView tv_group_item=(TextView)itemView.findViewById(R.id.tv_group_item);
tv_group_item.setText(itemList.get(groupPosition).get(childPosition));
return itemView;
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return null;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return 0;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean areAllItemsEnabled() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
@Override
public void onGroupExpanded(int groupPosition) {
// TODO Auto-generated method stub
}
@Override
public void onGroupCollapsed(int groupPosition) {
// TODO Auto-generated method stub
}
@Override
public long getCombinedChildId(long groupId, long childId) {
// TODO Auto-generated method stub
return 0;
}
@Override
public long getCombinedGroupId(long groupId) {
// TODO Auto-generated method stub
return 0;
}
}
}