package com.expandablelistviewdemo;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
private ExpandableListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ExpandableListView) findViewById(R.id.expandableListView1);
listView.setAdapter(new ExpandableAdapter());
}
class ExpandableAdapter extends BaseExpandableListAdapter {
private String[] group = { "加入", "目標", "參觀" };
private String[][] childs = { { "搜索", "可是" }, { "尺寸", "尺寸" },
{ "dd", "充斥", "ff" } };
// 返回每組中的每一個子列表對象
@Override
public Object getChild(int arg0, int arg1) {
return childs[arg0][arg1];
}
@Override
public long getChildId(int arg0, int arg1) {
return 0;
}
// 每一個分組中,二級列表的數量
@Override
public int getChildrenCount(int arg0) {
return childs[arg0].length;
}
// 返回每組對象
@Override
public Object getGroup(int arg0) {
return group[arg0];
}
// 分組的數量
@Override
public int getGroupCount() {
return group.length;
}
@Override
public long getGroupId(int arg0) {
return 0;
}
@Override
public View getGroupView(int arg0, boolean arg1, View arg2,
ViewGroup arg3) {
if(arg2==null){
arg2=getLayoutInflater().inflate(R.layout.griup_item, null);
}
ImageView iv= (ImageView) arg2.findViewById(R.id.imageView1);
TextView tv=(TextView) arg2.findViewById(R.id.textView1);
tv.setText(group[arg0]);
// iv.setImageResource(R.drawable.ic_launcher);
return arg2;
}
@Override
public View getChildView(int arg0, int arg1, boolean arg2, View arg3,
ViewGroup arg4) {
if(arg3==null){
arg3=getLayoutInflater().inflate(R.layout.childs_item, null);
}
ImageView iv=(ImageView) arg3.findViewById(R.id.imageView2);
TextView tv=(TextView) arg3.findViewById(R.id.textView2);
// iv.setImageResource(R.drawable.ic_launcher);
tv.setText(childs[arg0][arg1]);
return arg3;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int arg0, int arg1) {
return true;
}
}
}
<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/expandableListView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ExpandableListView>
</RelativeLayout>
<?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="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical">
<ImageView
android:layout_marginLeft="30dp"
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
<?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="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
android