ExpandableListActivity是可扩展的list,单击某个item后,又可显示一个子list。它的数据通过绑定到ExpandableListAdapter或者ExpandableListAdapter的子类上。
顺带提一下ExpandableListActivity 与ExpandableListView的关系就向 ListActivity与ListView一样总是一起出现的。
1.首先在main.xml添加ExpandableListView
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ExpandableListView android:id="@id/android:list" //ExpandableListView的ID为Android自定义,不可改变
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
</LinearLayout>
2.为父目录创建布局文件
res/layout/group.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<TextView
android:id="@+id/group"
android:layout_width="wrap_content"
android:layout_height="60px"
android:gravity="center"
android:textSize="25px"
android:textStyle="italic"
android:paddingLeft="50px"
/>
</LinearLayout>
3.为子目录创建布局文件
res/layout/layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<ImageView
android:id = "@+id/imageview"
android:padding="4.0dip"
android:layout_width="48.0dip"
android:layout_height="48.0dip"
android:scaleType="fitCenter"
android:layout_marginLeft="10.0dip"
android:layout_marginTop="5.0dip"
android:layout_marginBottom="5.0dip"
android:src="@drawable/h012"
android:layout_weight="0.25">
</ImageView>
<TextView
android:id = "@+id/textview"
android:layout_width="wrap_content"
android:textSize="20px"
android:textStyle="bold"
android:gravity="center_vertical"
android:paddingLeft="20px"
android:layout_height="52dp" android:layout_weight="0.77">
</TextView>
<ImageView
android:id="@+id/button"
android:layout_width="43.0dip"
android:layout_height="58.0dip"
android:clickable="true"
android:src="@drawable/button_state_image"
/>
</LinearLayout>
4.ExpandableList.java
package com.example;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
public class ExpandableList extends ExpandableListActivity {
/** Calledwhen the activity is first created. */
private final String TAG = "Expand";
private ImageButtoncallBtn;
private ImageButtonsmsBtn;
private ImageButtonemailBtn;
PopupWindow popup;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initPopupWindow();
this.getExpandableListView().setBackgroundDrawable(getResources().getDrawable(R.drawable.default_bg));
this.getExpandableListView().setDivider(this.getResources().getDrawable(R.drawable.divider));
ExpandableListAdapter adapter = new MyExpandableListAdapter();
this.setListAdapter(adapter);
}
private void initPopupWindow()
{
LayoutInflater inflater = (LayoutInflater)ExpandableList.this.getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.popupwindow,null);
popup = new PopupWindow(view, ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popup.setOutsideTouchable(true);
if(popup !=null)
{
Log.v(TAG,"Popupinitial success");
}
callBtn = (ImageButton)view.findViewById(R.id.callbtn);
smsBtn = (ImageButton)view.findViewById(R.id.smsbtn);
emailBtn = (ImageButton)view.findViewById(R.id.emailbtn);
Log.v(TAG, "Popupinitial success11111");
}
private String[]group= {"好友","同学","陌生人"};
private String [][]child =
{
{"彭宇","周琳","王乐"},
{"余洁","莉莉","王刚"},
{"李晨","吴良"}
};
private class MyExpandableListAdapter extends BaseExpandableListAdapter
{
LayoutInflater inflate = (LayoutInflater)ExpandableList.this.getSystemService(LAYOUT_INFLATER_SERVICE);
@Override
public Object getChild(int groupPosition,int childPosition) {
Log.v(TAG,"(getChild)groupPosition : "+groupPosition + "childPosition : " + childPosition + " "+child[groupPosition][childPosition]);
//returnchild[groupPosition][childPosition];
return null;
}
@Override
public long getChildId(int groupPosition,int childPosition) {
// TODO Auto-generatedmethod stub
Log.v(TAG,"(getChildId)childPosition : "+childPosition);
return childPosition;
}
@Override
public View getChildView(int groupPosition,int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
Log.v(TAG,"(getChildView)== ");
View view = inflate.inflate(R.layout.layout,null); // 不太理解
ImageView imageView = (ImageView)view.findViewById(R.id.imageview);
TextView textView = (TextView)view.findViewById(R.id.textview);
textView.setText(child[groupPosition][childPosition]);
ImageView button = (ImageView)view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.v(TAG,"Popupinit faild");
if(popup.isShowing())
{
popup.dismiss();
}
else
{
popup.showAsDropDown(v);
}
}
});
return view;
}
@Override
public int getChildrenCount(int groupPosition) {
Log.v(TAG,"(getChildrenCount)length : " + child[groupPosition].length);
returnchild[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generatedmethod stub
Log.v(TAG,"(getGroup): " + group[groupPosition]);
//returngroup[groupPosition];
return null;
}
@Override
public int getGroupCount() {
// TODO Auto-generatedmethod stub
Log.v(TAG,"(getGroupCount)length : " + group.length);
returngroup.length;
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generatedmethod stub
Log.v(TAG,"(getGroupId)groupPosition : " + groupPosition);
return groupPosition;
}
@Override
public View getGroupView(int groupPosition,boolean isExpanded,
View convertView, ViewGroup parent) {
View view = inflate.inflate(R.layout.group,null);
TextView textView = (TextView)view.findViewById(R.id.group);
textView.setText(group[groupPosition]);
return view;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generatedmethod stub
return true;
}
@Override
public boolean isChildSelectable(int groupPosition,int childPosition) {
// TODO Auto-generatedmethod stub
return true;
}
}
}