废话少说,直接上代码:
public class ExpendableListViewActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//创建一个BaseExpandableListAdapter对象,可扩展列表视图适配器
ExpandableListAdapter adapter=new BaseExpandableListAdapter() {
//用数组盛放相应图片地址
int [] logos=new int[]{R.drawable.p,
R.drawable.z,R.drawable.t};
//父选项标题
private String[] armTypes=new String[]{
"天堂","地狱","人间"
};
//子选项下的内容
private String[][] arms=new String[][]{
{"小A","小B","小C","小D"},{"中A","中B","中D","中D"},{"大A","大B","大C"}
};
@Override
//判断子列表是否被选中,在相应的图表上可以绑定相应的事件
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
@Override
//得到父框架视图
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LinearLayout ll=new LinearLayout(ExpendableListViewActivity.this);
ll.setOrientation(0);
ImageView logo=new ImageView(ExpendableListViewActivity.this);
logo.setImageResource(logos[groupPosition]);
ll.addView(logo);
TextView textView=getTextView();
textView.setText(getGroup(groupPosition).toString());
ll.addView(textView);
return ll;
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return armTypes.length;
}
@Override
//返回父组件
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return armTypes[groupPosition] ;
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return arms[groupPosition].length;
}
private TextView getTextView(){
AbsListView.LayoutParams lp=new AbsListView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,50);
TextView textView=new TextView(ExpendableListViewActivity.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);
textView.setPadding(36, 0, 0, 0);
textView.setTextSize(20);
return textView;
}
@Override
//G该方法决定每个子选项 的外观
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TextView textView=getTextView();
textView.setText(getChild(groupPosition, childPosition).toString());
return textView;
}
//获取子列表的ID
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return arms[groupPosition][childPosition];
}
};
ExpandableListView expandableListView=(ExpandableListView)findViewById(R.id.list);
expandableListView.setAdapter(adapter);
}
}
ExpandableListView思路分:一、父选项的getGroupView 、getGroupId、getGroupCount、getGroup 二,子选项的getChildView、getChile、getChild、getChildrenCount
public class ExpendableListViewActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//创建一个BaseExpandableListAdapter对象,可扩展列表视图适配器
ExpandableListAdapter adapter=new BaseExpandableListAdapter() {
//用数组盛放相应图片地址
int [] logos=new int[]{R.drawable.p,
R.drawable.z,R.drawable.t};
//父选项标题
private String[] armTypes=new String[]{
"天堂","地狱","人间"
};
//子选项下的内容
private String[][] arms=new String[][]{
{"小A","小B","小C","小D"},{"中A","中B","中D","中D"},{"大A","大B","大C"}
};
@Override
//判断子列表是否被选中,在相应的图表上可以绑定相应的事件
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
@Override
//得到父框架视图
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LinearLayout ll=new LinearLayout(ExpendableListViewActivity.this);
ll.setOrientation(0);
ImageView logo=new ImageView(ExpendableListViewActivity.this);
logo.setImageResource(logos[groupPosition]);
ll.addView(logo);
TextView textView=getTextView();
textView.setText(getGroup(groupPosition).toString());
ll.addView(textView);
return ll;
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return armTypes.length;
}
@Override
//返回父组件
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return armTypes[groupPosition] ;
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return arms[groupPosition].length;
}
private TextView getTextView(){
AbsListView.LayoutParams lp=new AbsListView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,50);
TextView textView=new TextView(ExpendableListViewActivity.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);
textView.setPadding(36, 0, 0, 0);
textView.setTextSize(20);
return textView;
}
@Override
//G该方法决定每个子选项 的外观
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TextView textView=getTextView();
textView.setText(getChild(groupPosition, childPosition).toString());
return textView;
}
//获取子列表的ID
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return arms[groupPosition][childPosition];
}
};
ExpandableListView expandableListView=(ExpandableListView)findViewById(R.id.list);
expandableListView.setAdapter(adapter);
}
}
ExpandableListView思路分:一、父选项的getGroupView 、getGroupId、getGroupCount、getGroup 二,子选项的getChildView、getChile、getChild、getChildrenCount