效果图:
布局文件:expandable_list_view.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">
<include layout="@layout/title_layout"/>
<ExpandableListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/expandable_list"></ExpandableListView>
</LinearLayout>
自定义适配器:MyExpandableListAdapter.java
public class MyExpandableListAdapter implements ExpandableListAdapter {
private Context context = null;
private String[] group = null;
private List<String[]> child = null;
public MyExpandableListAdapter(Context context, String[] group, List<String[]> child){
this.context = context;
this.group = group;
this.child = child;
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
}
@Override
public int getGroupCount() {
return group.length;
}
@Override
public int getChildrenCount(int groupPosition) {
return child.get(groupPosition).length;
}
@Override
public Object getGroup(int groupPosition) {
return group[groupPosition];
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return child.get(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 false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String str = group[groupPosition];
AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
TextView text = new TextView(context);
text.setLayoutParams(layoutParams);
text.setPadding(30,15,0,15);
text.setText(str);
text.setTextSize(20f);
return text;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
String str = child.get(groupPosition)[childPosition];
AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
TextView text = new TextView(context);
text.setLayoutParams(layoutParams);
text.setText(str);
text.setTextSize(18f);
text.setPadding(15, 10, 15, 10);
return text;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
@Override
public boolean areAllItemsEnabled() {
return true;
}
@Override
public boolean isEmpty() {
return (group == null || group.length < 1);
}
@Override
public void onGroupExpanded(int groupPosition) {
}
@Override
public void onGroupCollapsed(int groupPosition) {
}
@Override
public long getCombinedChildId(long groupId, long childId) {
return 0;
}
@Override
public long getCombinedGroupId(long groupId) {
return 0;
}
}
Activity文件:ExpandableListViewActivity.java
public class ExpandableListViewActivity extends Activity {
private ExpandableListView listView;
private ExpandableListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.expandable_list_view);
/**
* 初始化数据
*/
String[] group = {"列表一","列表二","列表三"};
String[] item = { "子列表一","子列表二","子列表三"};
List<String[]> child = new ArrayList<>();
for (int i=0;i<group.length;i++){
child.add(item);
}
adapter = new MyExpandableListAdapter(this, group, child);
listView = (ExpandableListView)findViewById(R.id.expandable_list);
listView.setAdapter(adapter);
/**
* 设置ExpandableListView中图标的位置到右边
*/
int windowWidth = getWindowManager().getDefaultDisplay().getWidth();
listView.setIndicatorBounds(windowWidth - 50, windowWidth - 30);
/**
* 设置自动收缩其他的group
*/
listView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
for (int i=0;i<adapter.getGroupCount();i++){
if (groupPosition != i){
listView.collapseGroup(i);
}
}
}
});
}
}