Android ExpandableListView的使用

转载请注明出处:http://blog.csdn.net/MAGIC_JSS/article/details/52740571;
ExpandableListView可扩展列表一般可用来开发类似QQ联系人的界面效果。简单整理了一下ExpandableListView的使用,希望帮助到需要的亲们,由于比较简单就没有添加文字描述,欢迎留言交流!!!

先看下效果图:
这里写图片描述

1、主Activity

/**
 * Created by magic on 2016年10月5日.防QQ联系人效果
 */
public class MainActivity extends Activity {

	ExpandableListView expandableListView;
	ExpandableListViewAdapter adapter;

	List<GroupBean> list_group = new ArrayList<GroupBean>();

	List<UserBean> list_child1 = new ArrayList<UserBean>();
	List<UserBean> list_child2 = new ArrayList<UserBean>();
	List<UserBean> list_child3 = new ArrayList<UserBean>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		expandableListView = (ExpandableListView) findViewById(R.id.exl);

		list_child1.add(new UserBean("赵丽颖"));
		list_child1.add(new UserBean("郑爽"));
		list_child1.add(new UserBean("杨幂"));

		list_child2.add(new UserBean("夏帆"));
		list_child2.add(new UserBean("刘亦菲"));
		list_child2.add(new UserBean("何曼婷"));

		list_child3.add(new UserBean("苍老师"));
		list_child3.add(new UserBean("刘诗诗 "));
		list_child3.add(new UserBean("佟丽娅"));

		list_group.add(new GroupBean("萌妹", "29/48", list_child1));
		list_group.add(new GroupBean("清纯", "42/60", list_child2));
		list_group.add(new GroupBean("美女", "53/71", list_child3));

		adapter = new ExpandableListViewAdapter(this, list_group);
		expandableListView.setAdapter(adapter);
		int groupCount = adapter.getGroupCount();
		for (int i = 0; i < groupCount; i++) {
			// 设置expandableListView展开子项
			// expandableListView.expandGroup(i);
			// 设置expandableListView不展开子项
			// expandableListView.collapseGroup(i);
		}

		// group 的点击事件
		expandableListView.setOnGroupClickListener(new OnGroupClickListener() {

			@Override
			public boolean onGroupClick(ExpandableListView parent, View v,
					int groupPosition, long id) {
				return false;
			}
		});
		// child 的点击事件
		expandableListView.setOnChildClickListener(new OnChildClickListener() {

			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				Toast.makeText(MainActivity.this,adapter.getGroup(groupPosition).getUserBeans().get(childPosition).getName(),
						Toast.LENGTH_SHORT).show();
				return false;
			}
		});

	}
}

相关实体类:
GroupBean.java

/**
 * Created by magic on 2016年10月5日.分组实体类
 */
public class GroupBean {

	// 组名
	String groupName;
	// 组描述
	String groupDesc;
	// 用户集合
	List<UserBean> userBeans;
	
	public GroupBean() {
		super();
	}

	public GroupBean(String groupName, String groupDesc,
			List<UserBean> userBeans) {
		super();
		this.groupName = groupName;
		this.groupDesc = groupDesc;
		this.userBeans = userBeans;
	}

	public String getGroupName() {
		return groupName;
	}

	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}

	public String getGroupDesc() {
		return groupDesc;
	}

	public void setGroupDesc(String groupDesc) {
		this.groupDesc = groupDesc;
	}

	public List<UserBean> getUserBeans() {
		return userBeans;
	}

	public void setUserBeans(List<UserBean> userBeans) {
		this.userBeans = userBeans;
	}
}

UserBean.java

/**
 * Created by magic on 2016年10月5日.用户实体类
 */
public class UserBean {

	String name;

	public UserBean(String name) {
		super();
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

2、主Activity xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
     >

    <!-- cacheColorHint:系统默认拖动过程中列表背景是黑的,设置为透明 -->
    <!-- android:divider="@color/mainLine"分割线颜色
         android:dividerHeight="0.5dp" 分割线高度-->
    <!-- android:dividerHeight="0.5dp" 子child的分割线高度 -->
    <!-- android:groupIndicator=""设置指示器  箭头-->
    <ExpandableListView
        android:id="@+id/exl"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:cacheColorHint="@color/white"
        android:childDivider="@color/mainLine"
        android:divider="@color/mainLine"
        android:groupIndicator="@null"
        android:dividerHeight="0.5dp"
         />

</LinearLayout>

3、ExpandableListView适配器

/**
 * Created by magic on 2016年10月5日.可扩展列表适配器
 */
public class ExpandableListViewAdapter extends BaseExpandableListAdapter {

	Context context;
	List<GroupBean> list_group;
	
	public ExpandableListViewAdapter(Context context,List<GroupBean> list_group) {
		super();
		this.context = context;
		this.list_group = list_group;
	}

	/**
	 * 获取指定组中的指定子元素数据。
	 */
	@Override
	public UserBean getChild(int groupPosition, int childPosition) {
		return list_group.get(groupPosition).getUserBeans().get(childPosition);
	}

	/**
	 * 获取指定组中的指定子元素ID
	 */
	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	/**
	 * 获取一个视图对象,显示指定组中的指定子元素数据。
	 * 
	 * @param groupPosition
	 *            组位置
	 * @param childPosition
	 *            子元素位置
	 * @param isLastChild
	 *            子元素是否处于组中的最后一个
	 * @param convertView
	 *            重用已有的视图(View)对象
	 * @param parent
	 *            返回的视图(View)对象始终依附于的视图组
	 */
	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean arg2, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(R.layout.child_layout, null);
		}
		TextView textView = (TextView) convertView.findViewById(R.id.tev_child);
		textView.setText(getChild(groupPosition, childPosition).getName());
		return convertView;
	}

	/**
	 * 获取指定组中的子元素个数
	 */
	@Override
	public int getChildrenCount(int groupPosition) {
		return list_group.get(groupPosition).getUserBeans().size();
	}

	/**
	 * 获取指定组中的数据
	 */
	@Override
	public GroupBean getGroup(int groupPosition) {
		return list_group.get(groupPosition);
	}

	/**
	 * 获取组的个数
	 */
	@Override
	public int getGroupCount() {
		return list_group.size();
	}

	/**
	 * 获取指定组的ID,这个组ID必须是唯一的
	 */
	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	/**
	 * 获取显示指定组的视图对象
	 * 
	 * @param groupPosition
	 *            组位置
	 * @param isExpanded
	 *            该组是展开状态还是伸缩状态
	 * @param convertView
	 *            重用已有的视图对象
	 * @param parent
	 *            返回的视图对象始终依附于的视图组
	 */
	@Override
	public View getGroupView(int groupPosition,boolean isExpanded,
			View convertView, ViewGroup parent) {
		convertView = LayoutInflater.from(context).inflate(R.layout.group_layout, null);
		CheckBox chb_group=(CheckBox)convertView.findViewById(R.id.chb_group);
		TextView tev_group_name = (TextView) convertView.findViewById(R.id.tev_group_name);
		TextView tev_group_desc = (TextView) convertView.findViewById(R.id.tev_group_desc);
		tev_group_name.setText(getGroup(groupPosition).getGroupName() + "");
		tev_group_desc.setText(getGroup(groupPosition).getGroupDesc() + "");
		chb_group.setChecked(isExpanded);
		return convertView;
	}

	/**
	 * 组和子元素是否持有稳定的ID,也就是底层数据的改变不会影响到它们。
	 */
	@Override
	public boolean hasStableIds() {
		return false;
	}

	/**
	 * 是否选中指定位置上的子元素。
	 */
	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

}

相关布局:
group_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:background="@color/white"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:paddingLeft="12dp"
        android:paddingRight="12dp" >

        <CheckBox
            android:id="@+id/chb_group"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@null"
            android:clickable="false"
            android:drawableLeft="@drawable/checkbox_background"
            android:focusable="false" />

        <TextView
            android:id="@+id/tev_group_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp" />

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <TextView
            android:id="@+id/tev_group_desc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/secondText"
            android:textSize="12sp" />
    </LinearLayout>

</LinearLayout>

child_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tev_child"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:background="@color/white"
        android:gravity="center_vertical"
        android:paddingLeft="12dp"
        android:textColor="@color/secondText"
        android:textSize="14sp" />

</LinearLayout>

魔鬼是天使的另一面。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值