一、直接使用ExpandableListView实现
效果图如下:
1.布局文件
activity_main.xml
<LinearLayout 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/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
2.MyExpandableListAdapter.java
package com.example.listviewdemo;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
public String[] groups = { "我的好友", "大学同学", "高中同学" };
public String[][] childrens = { { "小张", "小李", "小丽", "向明" }, { "向明1", "向明2", "向明3", "向明4" }, { "天明1", "天明2", "天明3", "天明4" } };
private Context context;
public MyExpandableListAdapter(Context context) {
this.context = context;
}
public Object getChild(int groupPosition, int childPosition) {
return childrens[groupPosition][childPosition];
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getChild(groupPosition, childPosition).toString());
return textView;
}
// 新建一个TextView
public TextView getGenericView() {
// Layout parameters for the ExpandableListView
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 64);
TextView textView = new TextView(context);
textView.setLayoutParams(lp);
// Center the text vertically
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
// Set the text starting position
textView.setPadding(42, 0, 0, 0);
return textView;
}
public int getChildrenCount(int groupPosition) {
return childrens[groupPosition].length;
}
public Object getGroup(int groupPosition) {
return groups[groupPosition];
}
public int getGroupCount() {
return groups.length;
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getGroup(groupPosition).toString());
return textView;
}
public boolean hasStableIds() {
return true;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
3.MainActivity2.java
package com.example.listviewdemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.TextView;
public class MainActivity2 extends Activity {
private ExpandableListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ExpandableListView) this.findViewById(R.id.listView);
MyExpandableListAdapter adapter = new MyExpandableListAdapter(this);
listView.setAdapter(adapter);
listView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
TextView tv = (TextView) v;
Toast.makeText(MainActivity2.this, groupPosition + ":" + childPosition + "--->" + tv.getText().toString(), 0).show();
return false;
}
});
}
}
这种方式比较独立,使用起来和ListView差不多。
二、使用ExpandableListActivity实现
这种形式,有点像ListActivity。效果如下:
1.MainActiivty.java
这里只需要一个类即可实现
package com.example.listviewdemo;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class MainActivity extends ExpandableListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyExpandableListAdapter adapter = new MyExpandableListAdapter();
setListAdapter(adapter);
}
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
public String[] groups = { "我的好友", "大学同学", "高中同学" };
public String[][] childrens = { { "小张小张小张", "小李", "小丽", "向明" }, { "向明", "向明", "向明", "向明" }, { "向明", "向明", "向明", "向明" } };
public Object getChild(int groupPosition, int childPosition) {
return childrens[groupPosition][childPosition];
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getChild(groupPosition, childPosition).toString());
return textView;
}
// 新建一个TextView
public TextView getGenericView() {
// Layout parameters for the ExpandableListView
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
TextView textView = new TextView(MainActivity.this);
textView.setLayoutParams(lp);
// Center the text vertically
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
// Set the text starting position
textView.setPadding(42, 5, 5, 5);
return textView;
}
public int getChildrenCount(int groupPosition) {
return childrens[groupPosition].length;
}
public Object getGroup(int groupPosition) {
return groups[groupPosition];
}
public int getGroupCount() {
return groups.length;
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getGroup(groupPosition).toString());
return textView;
}
public boolean hasStableIds() {
return true;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
}
适用于只展示可扩展ListView的场合。