android打下扎实基础-----ExpandableListActivity类的使用

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);
    }
}

附上全部代码:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值