ExpandableListAcitivity类很好的实现了简单的一,二,三级菜单功能:
详见图片:
实现词功能,不再是继承Activity,而是ExpandableListActivity
而list中的Adapter 则是采用 ExpandableListAdapter
初始化语句:ExpandableListAdapter adpter = new BaseExpandableListAdapter(){};
其中有几个重要的方法需要重写:
1.View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent){};
View convertView, ViewGroup parent
作用:用来获取1级菜单的样式
2.View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent){};
作用:用来获取2级菜单的样式:
最后:记得加载adapter setListAdapter(adpter);
public class ActivityStudyActivity extends ExpandableListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
ExpandableListAdapter adapter = new BaseExpandableListAdapter() {
int[] logos = new int[]
{
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher
};
private String[] armTypesStrings = new String[]{"A","B","C"};
private String[][] arms = new String[][]{
{"a","b","c","d"},
{"e","f","g","h"},
{"i","j","k","l"}
};
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return false;
}
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
LinearLayout ll = new LinearLayout(ActivityStudyActivity.this);
ll.setOrientation(0);
ImageView logo = new ImageView(ActivityStudyActivity.this);
logo.setImageResource(logos[groupPosition]);
ll.addView(logo);
TextView textView = new TextView(ActivityStudyActivity.this);
textView.setText(getGroup(groupPosition).toString());
ll.addView(textView);
return ll;
}
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
public int getGroupCount() {
// TODO Auto-generated method stub
return armTypesStrings.length;
}
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return armTypesStrings[groupPosition];
}
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return arms[groupPosition].length;
}
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;
}
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return arms[groupPosition][childPosition];
}
private TextView getTextView(){
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,64);
TextView textView = new TextView(ActivityStudyActivity.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textView.setPadding(36, 0, 0, 0);
textView.setTextSize(20);
return textView;
}
};
setListAdapter(adapter);
}
}
附上全部代码: