ListView之BaseExpandableListAdapter

本文详细介绍了如何使用BaseExpandableListAdapter类来创建一个适用于展示QQ分组的适配器,包括实现的关键代码和方法解释。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

BaseExpandableListAdapter主要用于类似于qq分组这样的显示的适配器

BaseExpandableListAdapteview适用于类似qq分组的模式


//关键代码是这个可扩展的listview适配器
	class ExAdapter extends BaseExpandableListAdapter {

		Context context;
		
		public ExAdapter(Context context) {
			super();
			this.context = context;
		}
		
		//得到大组成员总数
		public int getGroupCount() {
			// TODO Auto-generated method stub
			return groupData.size();
		}

		//得到小组成员总数
		public int getChildrenCount(int groupPosition) {
			// TODO Auto-generated method stub
			return childData.get(groupPosition).size();
		}

		//得到大组成员名称
		public Object getGroup(int groupPosition) {
			// TODO Auto-generated method stub
			return groupData.get(groupPosition).get(GROUP_TEXT).toString();
		}

		//得到小组成员名称
		public Object getChild(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return childData.get(groupPosition).get(childPosition).get(CHILD_NAME).toString();
		}

		//得到大组成员的id
		public long getGroupId(int groupPosition) {
			// TODO Auto-generated method stub
			return groupPosition;
		}
		
		//得到小组成员的id
		public long getChildId(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return childPosition;
		}

		 /**
         * Indicates whether the child and group IDs are stable across changes
         * to the underlying data. 表明大組和小组id是否稳定的更改底层数据。
         * 
         * @return whether or not the same ID always refers to the same object
         * @see Adapter#hasStableIds()
         */
		public boolean hasStableIds() {
			// TODO Auto-generated method stub
			return true;
		}

		//得到大组成员的view
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			View view = convertView;
			if(view == null) {
				LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				view = inflater.inflate(R.layout.member_listview2, null);
			}
			TextView title = (TextView)view.findViewById(R.id.content_001);
			title.setText(getGroup(groupPosition).toString()); //设置最大组成员名称
			
			ImageView image = (ImageView)view.findViewById(R.id.tubiao);  //是否展开大组的图标
			if(isExpanded) //大组展开时
				image.setBackgroundResource(R.drawable.hero);
			else
				image.setBackgroundResource(R.drawable.ic_launcher);
			
			return view;
		}

		
		//得到小组成员的view
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			View view = convertView;
			if(view == null) {
				LayoutInflater inflater = LayoutInflater.from(context);
				view = inflater.inflate(R.layout.member_childitem2, null);
			}
			final TextView title = (TextView)view.findViewById(R.id.child_name);
			title.setText(childData.get(groupPosition).get(childPosition).get(CHILD_NAME).toString());  //大标题
			final TextView title2 = (TextView)view.findViewById(R.id.child_text);
			title2.setText(childData.get(groupPosition).get(childPosition).get(CHILD_TEXT).toString());  //小标题
			return view;
		}

		//得到小组成员是否被选择
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return true;
		}
		
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值