import java.util.*;
import android.app.Activity;
import android.os.Bundle;
import android.view.*;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
public class ExpandActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_expand);
//效果
overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
ExpandableListView expandableListView = (ExpandableListView)findViewById(R.id.expandableListView);
expandableListView.setAdapter(new ExpandableAdapter(ExpandActivity.this));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.expand, menu);
return true;
}
/** 定義一個expandAdapter類繼承 BaseExpandableListAdapter*/
class ExpandableAdapter extends BaseExpandableListAdapter {
private List<String> groupArray;
private List<ArrayList<String>> childArray;
private Activity activity;
//初始化,這裏可以考慮父節點用數組,子節點用二維數組
private void initList(){
groupArray = new ArrayList<String>();
childArray = new ArrayList<ArrayList<String>>();
groupArray.add("排序算法" );
groupArray.add("Android之路" );
ArrayList<String> tempArray = new ArrayList<String>();
tempArray.add("冒泡排序" );
tempArray.add("插入排序" );
tempArray.add("選擇排序" );
ArrayList<String> tempArray2 = new ArrayList<String>();
tempArray2.add("深入Adapt" );
tempArray2.add("通知管理" );
tempArray2.add("service管理" );
childArray.add(tempArray);
childArray.add(tempArray2);
}
//構造方法
public ExpandableAdapter(Activity a){
activity=a;
initList();
}
@Override
public int getGroupCount() {
return groupArray.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return childArray.get(groupPosition).size();
}
@Override //獲得父節點
public Object getGroup(int groupPosition) {
return groupArray.get(groupPosition);
}
@Override //獲得子節點
public Object getChild(int groupPosition, int childPosition) {
return childArray.get(groupPosition).get(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 string = groupArray.get(groupPosition);
return getGenericView(string,1);
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
String string = childArray.get(groupPosition).get(childPosition);
return getGenericView(string,2);
}
@Override //子節點是否可選
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true ;
}
/** 查看子節點的視圖 */
public TextView getGenericView(String string,int statu)
{
AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 64 );
TextView text = new TextView(activity);
text.setLayoutParams(layoutParams);
if(statu==2)
text.setBackgroundColor(getResources().getColor(R.color.pink));
else
text.setBackgroundColor(getResources().getColor(R.color.gray));
text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
text.setPadding(36 , 0 , 0 , 0 );
text.setText(string);
return text;
}
}
}
佈局文件如下:
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ExpandActivity"
android:orientation="vertical">
<ExpandableListView
android:id ="@+id/expandableListView"
android:layout_width ="match_parent"
android:layout_height ="wrap_content"
/>
</LinearLayout>