思路:
activity_main:一个ExpandableListView 类似ListView
expandable_group_item:组的布局文件 类似ListView子控件布局
expandable_childe_item:组内成员的布局文件,类似ListView子控件布局
ListView 添加数据 需设置adapter,ExpandableListView也需要设置adapter.
源代码:
MainActivity.java:
import android.os.Bundle;
import android.app.Activity;
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.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.TextView;
public class MainActivity extends Activity {
private int lastGroupPosition = -1;
private ExpandableListView expandable_lv;
private String group[] = {"A","B","C"};
private String child[][] = {{"aa","ab","ac"},{"ba","bb","bc","bd"},{"ca","cb"}};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
expandable_lv = (ExpandableListView) findViewById(R.id.expandable_lv);
MyExpandableListAdapter myExpandableListAdapter = new MyExpandableListAdapter();
expandable_lv.setAdapter(myExpandableListAdapter);
//indicator 指示器,setGroupIndicator如果组内含有内容,则显示>图标 如果没有内容,则不显示。
//null的意思是不显示这个图标
//null的地方可用图片代替this.getResources().getDrawable(R.drawable.ic_launcher)
expandable_lv.setGroupIndicator(null);
expandable_lv.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
//设置只有一个组是打开状态
if(lastGroupPosition != groupPosition){
expandable_lv.collapseGroup(lastGroupPosition);
}
lastGroupPosition = groupPosition;
}
});
expandable_lv.setOnGroupCollapseListener(new OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
}
});
}
class MyExpandableListAdapter extends BaseExpandableListAdapter{
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) {
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) {
LayoutInflater systemService = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = systemService.inflate(R.layout.expandable_group_item, null);
TextView group_text = (TextView) view.findViewById(R.id.group_text);
group_text.setText(group[groupPosition]);
return view;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
LayoutInflater systemService = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = systemService.inflate(R.layout.expandable_childe_item, null);
TextView child_text = (TextView) view.findViewById(R.id.child_text);
child_text.setText(child[groupPosition][childPosition]);
return view;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
}
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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.mylistview.MainActivity" >
<ExpandableListView
android:id="@+id/expandable_lv"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ExpandableListView>
</RelativeLayout>
expandable_group_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/group_text"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:textSize="25sp"/>
</LinearLayout>
expandable_child_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/child_text"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:textSize="15sp"
android:layout_marginLeft="15sp"/>
</LinearLayout>