一、activity代码
package com.liudan.activity;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import com.liudan.adapter.ExpandableAdapter;
import com.liudan.service.DataApplication;
public class ExpandableActivity extends Activity {
private static final String TAG = "ExpandableActivity";
private ExpandableListView expandableList;
private List<String> groupArray;
private List<List<String>> childArray;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
expandableList = (ExpandableListView)findViewById(R.id.expandableList);
initData();
ExpandableAdapter expandableAdapter = new ExpandableAdapter(this);
expandableList.setAdapter(expandableAdapter);
//设置一级栏目前的小图标为null就是去掉默认的小图标
expandableList.setGroupIndicator(null);
expandableList.setOnChildClickListener(new OnChildClickListener(){
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Log.i(TAG, "ExpandableActivity----------------------groupPosition="+groupPosition);
Log.i(TAG, "ExpandableActivity----------------------childPosition="+childPosition);
// Intent intent = new Intent(ExpandableActivity.this,ChildActivity.class);
// startActivity(intent);
return false;
}
});
}
private void initData(){
groupArray = new ArrayList<String>();
childArray = new ArrayList<List<String>>();
// groupArray.add("第一行");
// groupArray.add("第二行");
// List<String> tempArray = new ArrayList<String>();
// tempArray.add("第一条");
// tempArray.add("第二条");
// tempArray.add("第三条");
// for(int index = 0; index <groupArray.size(); ++index) {
// childArray.add(tempArray);
// }
List<String> hotCityList = DataApplication.getHotCityList();
for (int i = 0; i < hotCityList.size(); i++) {
String[] hotCity = hotCityList.get(i).split(":");
Log.i(TAG, "ExpandableActivity-------------hotCity="+hotCity[0]);
Log.i(TAG, "ExpandableActivity-------------hotCity.length="+hotCity.length);
groupArray.add(hotCity[0]);
List<String> item = new ArrayList<String>();
for (int j = 1; j < hotCity.length; j++) {
Log.i(TAG, "ExpandableActivity-------------hotCity.length="+hotCity.length);
item.add(hotCity[j]);
}
Log.i(TAG, "ExpandableActivity-------------item.size()="+item.size());
childArray.add(item);
}
}
public List<String> getGroupArray() {
return groupArray;
}
public List<List<String>> getChildArray() {
return childArray;
}
}
二、adapter代码
package com.liudan.adapter;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import com.liudan.activity.ExpandableActivity;
import com.liudan.activity.R;
public class ExpandableAdapter extends BaseExpandableListAdapter {
private ExpandableActivity expandable;
public ExpandableAdapter(ExpandableActivity expandable){
this.expandable = expandable;
}
public int getGroupCount() {
// TODO Auto-generated method stub
return expandable.getGroupArray().size();
}
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return expandable.getChildArray().get(groupPosition).size();
}
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return expandable.getGroupArray().get(groupPosition);
}
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return expandable.getChildArray().get(groupPosition).get(childPosition);
}
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
// String string = expandable.getGroupArray().get(groupPosition);
// return getGenericView(string);
View view = null;
// if(convertView == null) {
view = expandable.getLayoutInflater().inflate(R.layout.group, null);
TextView tv = (TextView)view.findViewById(R.id.groupLabel);
String ss = expandable.getGroupArray().get(groupPosition);
tv.setText(ss);
// } else {
// view = convertView;
// }
return view;
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
// String string = expandable.getChildArray().get(groupPosition).get(childPosition);
// return getGenericView(string);
View view = null;
// if(convertView == null) {
view = expandable.getLayoutInflater().inflate(R.layout.group_child, null);
TextView tv = (TextView)view.findViewById(R.id.childLabel);
String ss = expandable.getChildArray().get(groupPosition).get(childPosition);
tv.setText(ss);
// } else {
// view = convertView;
// }
return view;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
public TextView getGenericView(String string)
{
// Layout parameters for the ExpandableListView
AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 64);
TextView text = new TextView(expandable);
text.setLayoutParams(layoutParams);
// Center the text vertically
text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
// Set the text starting position
text.setPadding(36, 0, 0, 0);
text.setText(string);
return text;
}
}
三、 业务逻辑代码——得到数据
package com.liudan.service;
import java.util.ArrayList;
import java.util.List;
public class DataApplication {
private static final String TAG = "DataApplication";
public static List hotCityList = null;
public static List getHotCityList(){
// Parser parser = new Parser(value);
// try {
// hotCityList = (List<ExpandCity>) parser.SAX(SkyApplication.this
// .getAssets().open("data.xml"));
// } catch (Exception e1) {
// e1.printStackTrace();
// }
List hotCityList = new ArrayList();
hotCityList.add("中国:北京:新圳:上海:湖南");
hotCityList.add("美国:北京:新圳:上海:湖南");
hotCityList.add("法国:北京:新圳:上海:湖南");
return hotCityList;
}
}
四、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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ExpandableListView android:id="@+id/expandableList"
android:layout_height="fill_parent" android:layout_width="fill_parent"
android:cacheColorHint="#00000000"></ExpandableListView>
</LinearLayout>
<?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="wrap_content">
<TextView android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="@+id/groupLabel"
android:background="#ff0000ff"
android:textStyle="bold" android:paddingBottom="5dip" android:paddingLeft="5dip" android:paddingRight="5dip" android:paddingTop="5dip"/>
</LinearLayout>
<?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="wrap_content">
<TextView android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="@+id/childLabel"
android:background="#000000ff"
android:textColor="#ff0000ff"
android:textSize="15dip" android:paddingBottom="10dip" android:paddingLeft="10dip" android:paddingRight="10dip" android:paddingTop="10dip"/>
</LinearLayout>