package com.example.liguofeng.launcheractivitytest;
import android.app.ExpandableListActivity;
import android.graphics.Color;
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.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ExpandableListActivityTest extends ExpandableListActivity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// BaseExpandableListAdapter生成
ExpandableListAdapter adapter = new BaseExpandableListAdapter() {
int[] logos = new int[]{R.drawable.logo1, R.drawable.logo2, R.drawable.logo3};
private String[] type = {"logo1", "logo2", "logo3"};
private String[][] detail = {
{"logo1-1", "logo1-2", "logo1-3",},
{"logo2-1", "logo2-2", "logo2-3",},
{"logo3-1", "logo3-2", "logo3-3",}
};
@Override
public int getGroupCount() {
return type.length;
}
@Override
public int getChildrenCount(int groupPosition) {
return detail[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition) {
return type[groupPosition];
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return detail[groupPosition][childPosition];
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView textView = getTextView();
textView.setText(getChild(groupPosition, childPosition).toString());
return textView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
@Override
public View getGroupView(int groupPosition,
boolean isExpanded, View convertView, ViewGroup parent) {
LinearLayout layout = new LinearLayout(ExpandableListActivityTest.this);
layout.setOrientation(LinearLayout.HORIZONTAL);
ImageView logo = new ImageView(ExpandableListActivityTest.this);
logo.setImageResource(logos[groupPosition]);
layout.addView(logo);
TextView textView = getTextView();
textView.setText(getGroup(groupPosition).toString());
layout.addView(textView);
return layout;
}
private TextView getTextView() {
AbsListView.LayoutParams p =
new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 64);
TextView textView = new TextView(ExpandableListActivityTest.this);
textView.setLayoutParams(p);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textView.setPadding(36, 0, 0, 0);
textView.setTextSize(15);
textView.setTextColor(Color.RED);
return textView;
}
};
// Adapter設定
setListAdapter(adapter);
}
}