可展开的列表组件ExpandableListView

/*
 * 可展开的列表组件ExpandableListView
 * ExpandableListView是ListView的子类,它在普通的ListView的基础上进行了扩展,
 * 它把应用中的列表项分为几组,每组有可包含多个列表项。它与ListView的用法非常
 * 相似,只是ExpandableListView所显示的列表项应由ExpandableAdapter提供
 * 下面我们就来看看通过自定义的ExpandableAdapter为ExpandableListView提供列表项
 */

import  略

public class Ex02_10Activity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// 创建一个BaseExpandableAdap对象
		ExpandableListAdapter adapter = new BaseExpandableListAdapter() {
			// 定义图片数组,用以加入ExpandableListView中
			int[] logos = new int[] { R.drawable.p, R.drawable.z, R.drawable.t };
			// 定义一个数组,作为上一级目录
			private String[] armType = new String[] { "神族兵种", "虫族兵种", "人族兵种" };
			// 上一级目录中所包含的子目录
			private String[][] arms = new String[][] {
					{ "狂战士", "龙骑士", "黑暗圣堂", "电兵" },
					{ "小狗 ", "赤蛇", "飞龙", "自爆飞机" }, { "机枪兵", "护士MM", "幽灵" } };

			// 以下方法获取指定组位置、指定子列表项处的子列表项数据
			// 参数说明
			// groupPosition:the position of the group that the child resides in
			// childPosition:the position of the child with respect to other
			// children in the group
			public Object getChild(int groupPosition, int childPosition) {
				// TODO Auto-generated method stub
				return arms[groupPosition][childPosition];
			}

			// 得到子目录中数据的位置
			public long getChildId(int groupPosition, int childPosition) {
				// TODO Auto-generated method stub
				return childPosition;
			}

			// 该方法决定每个子选项的外观
			public View getChildView(int groupPosition, int childPosition,
					boolean isLastChild, View convertView, ViewGroup parent) {
				// TODO Auto-generated method stub
				// 调用getTextView()方法,得到TextView的显示方式和布局方式
				TextView textView = getTextView();
				textView.setText(getChild(groupPosition, childPosition)
						.toString());
				return textView;
			}

			// 得到子目录的个数
			public int getChildrenCount(int groupPosition) {
				// TODO Auto-generated method stub
				return arms[groupPosition].length;
			}

			public Object getGroup(int groupPosition) {
				// TODO Auto-generated method stub
				return armType[groupPosition];
			}

			// 得到主目录的个数
			public int getGroupCount() {
				// TODO Auto-generated method stub
				return armType.length;
			}

			// 得到主目录的ID
			public long getGroupId(int groupPosition) {
				// TODO Auto-generated method stub
				return groupPosition;
			}

			public View getGroupView(int groupPosition, boolean isExpanded,
					View convertView, ViewGroup parent) {
				// TODO Auto-generated method stub
				LinearLayout ll = new LinearLayout(Ex02_10Activity.this);
				ll.setOrientation(0);
				ImageView logo = new ImageView(Ex02_10Activity.this);
				TextView textView = getTextView();
				logo.setImageResource(logos[groupPosition]);
				ll.addView(logo);
				textView.setText(getGroup(groupPosition).toString());
				ll.addView(textView);
				return ll;
			}

			// 这个没怎么看懂,望指教...
			public boolean hasStableIds() {
				// TODO Auto-generated method stub
				return false;
			}

			// 设置子目录时候可以被选择,返回true
			public boolean isChildSelectable(int groupPosition,
					int childPosition) {
				// TODO Auto-generated method stub
				return true;
			}

			// 设置TextView的显示方式和布局方式
			private TextView getTextView() {
				AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
						ViewGroup.LayoutParams.FILL_PARENT, 64);
				TextView textView = new TextView(Ex02_10Activity.this);
				textView.setLayoutParams(lp);
				textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
				textView.setPadding(36, 0, 0, 0);
				textView.setTextSize(20);
				return textView;
			}

		};
		ExpandableListView expandListview = (ExpandableListView) findViewById(R.id.elv);
		expandListview.setAdapter(adapter);
	}
}

下面我们来看下程序运行后的截图:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值