MAinActivity的代码如下所示:
public class MainActivity extends Activity {
private ExpandableListView mExpandableListView;
private ArrayList<String> list;
private ArrayList<String[]> arrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mExpandableListView = (ExpandableListView) findViewById(R.id.activity_main_expandableListView);
//外层布局数据
list = new ArrayList<String>();
//内层布局数据
arrayList = new ArrayList<String[]>();
list.add("我的好友");
list.add("黑名单");
list.add("陌生人");
list.add("同事");
list.add("老师");
arrayList.add(new String[]{"欣哥","张三","李四","王五"});
arrayList.add(new String[]{"欣哥","张三","李四","王五"});
arrayList.add(new String[]{"张三","李四","王五"});
arrayList.add(new String[]{"欣哥","张三","李四","王五"});
arrayList.add(new String[]{"张三","李四","王五"});
MyExpandableListViewAdapter adapter = new MyExpandableListViewAdapter();
mExpandableListView.setAdapter(adapter);
mExpandableListView.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
}
});
}
//ExpandableListViewAdapter组件的父类适配是BaseExpandableListAdapter
class MyExpandableListViewAdapter extends BaseExpandableListAdapter{
//外层Item数量
@Override
public int getGroupCount() {
return list.size();
}
//当前内层Item数量
@Override
public int getChildrenCount(int groupPosition) {
return arrayList.get(groupPosition).length;
}
//当前外层Item内容
@Override
public Object getGroup(int groupPosition) {
return list.get(groupPosition);
}
//当前内层Item内容
@Override
public Object getChild(int groupPosition, int childPosition) {
return arrayList.get(groupPosition)[childPosition];
}
//当前外层Item Id
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
//当前内层Item Id
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
//是否使用同一个id
@Override
public boolean hasStableIds() {
return false;
}
//获得当前外层Item的视图
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
//创建一个TextView的对象
TextView text = new TextView(MainActivity.this);
//创建一个矩形布局
LayoutParams params=new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
//给TextView对象设置宽高
text.setLayoutParams(params);
//给TextView对象设置padding属性(左,上,右,下)
text.setPadding(10, 5, 0, 5);
text.setText(list.get(groupPosition));
//给TextView设置重心
text.setGravity(Gravity.CENTER_HORIZONTAL);
return text;
}
//获得当前内层Item的视图
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView text = new TextView(MainActivity.this);
LayoutParams params=new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
text.setLayoutParams(params);
text.setPadding(10, 5, 0, 5);
text.setTextColor(Color.RED);
text.setGravity(Gravity.CENTER_HORIZONTAL);
text.setText(arrayList.get(groupPosition)[childPosition]);
return text;
}
//设置内层视图是否可点击
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ExpandableListView
android:id="@+id/activity_main_expandableListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>