ExpandableListView简介

设置事件监听

去除分割线android:divider="@null"

expandableListView = (ExpandableListView) findViewById(R.id.expandable_list);
		//remove group icon
		expandableListView.setGroupIndicator(null);
		//group click
		expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
			@Override
			public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
				//return false,else not expand
				return false;
			}
		});

		//child click
		expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
			@Override
			public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
				childClickHandler(groupPosition,childPosition);
				return true;
			}
		});

适配器

默认折叠图标在左边,可以通过自定义设置图标的位置

public class MyExpandableListAdapter extends BaseExpandableListAdapter{

		@Override
		public int getGroupCount() {
			return buildingInfo.size();
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			return buildingInfo.get(groupPosition).BuildingList.size();
		}

		@Override
		public Object getGroup(int groupPosition) {
			return buildingInfo.get(groupPosition);
		}

		@Override
		public Object getChild(int groupPosition, int childPosition) {
			return buildingInfo.get(groupPosition).BuildingList;
		}

		@Override
		public long getGroupId(int groupPosition) {
			return 0;
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			return 0;
		}

		@Override
		public boolean hasStableIds() {
			return false;
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
			GroupViewHolder groupViewHolder;
			if (convertView == null) {
				convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_expand_group, parent, false);
				groupViewHolder = new GroupViewHolder();
				groupViewHolder.tvTitle = (TextView) convertView.findViewById(R.id.group_label);
				groupViewHolder.tvTitle.setTypeface(typeface);
				groupViewHolder.imageView = (ImageView) convertView.findViewById(R.id.group_indicator);
				convertView.setTag(groupViewHolder);
			} else {
				groupViewHolder = (GroupViewHolder) convertView.getTag();
			}
			groupViewHolder.tvTitle.setText(buildingInfo.get(groupPosition).Office);
			if(isExpanded){
				groupViewHolder.imageView.setImageResource(R.drawable.icon_up);
			}else{
				groupViewHolder.imageView.setImageResource(R.drawable.icon_down);
			}
			return convertView;
		}

		@Override
		public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
			ChildViewHolder childViewHolder;
			if (convertView == null) {
				convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_expand_child, parent, false);
				childViewHolder = new ChildViewHolder();
				childViewHolder.tvTitle = (TextView) convertView.findViewById(R.id.label_expand_child);
				childViewHolder.tvTitle.setTypeface(typeface);
				convertView.setTag(childViewHolder);
			} else {
				childViewHolder = (ChildViewHolder) convertView.getTag();
			}
			childViewHolder.tvTitle.setText(buildingInfo.get(groupPosition).BuildingList.get(childPosition).Building);
			return convertView;
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return true;
		}
		private class GroupViewHolder {
			TextView tvTitle;
			ImageView imageView;
		}

		private class ChildViewHolder {
			TextView tvTitle;
		}
	}

expandabel


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值