android之ExpandableListView

最近在做企业难忘录,得使用ExpandableListView,现在已做好了。

这需要两个布局,如下

第一个tt_item_group_parent.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:gravity="center_vertical"
    android:orientation="horizontal"
    android:background="#ffffff"
    android:padding="10dp">

    <TextView
        android:id="@+id/tt_item_group_parent_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dip"
        android:textColor="@color/Black"
        android:textSize="17sp" />

    <TextView
        android:id="@+id/tt_item_group_parent_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone" />

</LinearLayout>

第二个tt_item_group_child.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="horizontal">

    <com.chat.ui.widget.ImageViewCircle
        android:id="@+id/tt_item_group_child_icon"
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:layout_marginRight="10dp"
        android:scaleType="centerCrop" />

    <TextView
        android:id="@+id/tt_item_group_child_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:textColor="@color/Black"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/tt_item_group_child_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone" />

</LinearLayout>

现在开始写容器了GroupAdapter.java

package com.chat.ui.adapter;

import java.util.List;

import com.chat.service.aidl.Contact;
import com.chat.service.aidl.GroupInfo;
import com.chat.ui.widget.ImageViewCircle;

import com.chat.IM;
import com.chat.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class GroupAdapter extends BaseExpandableListAdapter{
	private List<GroupInfo> groupList;
	private List<Contact> contactList;
	private LayoutInflater mLayoutInflater = null;

	public GroupAdapter(Context ctx,List<GroupInfo> groupList){
		this.groupList = groupList;
		mLayoutInflater = (LayoutInflater) ctx
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	//获取指定组列表、指定子列表处的子列表数据
	@Override
	public Object getChild(int groupPos, int childPos) {
		contactList = groupList.get(groupPos).getContactList();
		return contactList.get(childPos);
	}

	@Override
	public long getChildId(int grounpPos, int childPos) {
		return childPos;
	}

	//返回特定组处的子列表数量
	@Override
	public int getChildrenCount(int groupPos) {
		contactList = groupList.get(groupPos).getContactList();
		return contactList.size();
	}

	//该方法决定每个子选项的外观
	@Override
	public View getChildView(int groupPos, int childPos, boolean isLastChild, View convertView,
			ViewGroup parent) {
		ChildHolder holder = null;
		if(convertView == null ){
			convertView = mLayoutInflater.inflate(R.layout.tt_item_group_child, null);
			holder = new ChildHolder(convertView);
			convertView.setTag(holder);
		}else{
			holder = (ChildHolder)convertView.getTag();
		}

		Contact contact = (Contact)getChild(groupPos,childPos);
		holder.name.setText(contact.name);
		holder.icon.setImageDrawable(IM.getAvatar(contact.account));
		return convertView;
	}

	//获取指定组位置处的数据
	@Override
	public GroupInfo getGroup(int groupPos) {
		return groupList.get(groupPos);
	}

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

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

	@Override
	public View getGroupView(int groupPos, boolean isExpanded, View convertView, ViewGroup parent) {
		GroupHolder holder = null;
		if(convertView == null){
			convertView = mLayoutInflater.inflate(R.layout.tt_item_group_parent, null);
			holder = new GroupHolder(convertView);
			convertView.setTag(holder);
		}else{
			holder = (GroupHolder)convertView.getTag();
		}

		GroupInfo info = (GroupInfo)getGroup(groupPos);
		System.out.println("GroupAdapter->getGroupView:"+info.getName());
		holder.name.setText(groupList.get(groupPos).getName());
		return convertView;
	}

	// 是否指定分组视图及其子视图的ID对应的后台数据改变也会保持该ID。
	@Override
	public boolean hasStableIds() {
		return true;
	}

	// 指定位置的子视图是否可选择。
	@Override
	public boolean isChildSelectable(int groupPos, int childPos) {
		return true;
	}

	class ChildHolder{
		ImageViewCircle icon;
		TextView name;
		TextView accout;

		public ChildHolder(View v){
			icon = (ImageViewCircle)v.findViewById(R.id.tt_item_group_child_icon);
			name = (TextView)v.findViewById(R.id.tt_item_group_child_name);
			accout = (TextView)v.findViewById(R.id.tt_item_group_child_id);
		}
	}

	class GroupHolder{
		TextView name;
		TextView accout;

		public GroupHolder(View v){
			name = (TextView)v.findViewById(R.id.tt_item_group_parent_name);
			accout = (TextView)v.findViewById(R.id.tt_item_group_parent_id);
		}
	}
}

容器也写好,当然是写数据加载了Activity

public class AddFriActivity extends Activity {
	private ExpandableListView list;
	private GroupAdapter adapter;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tt_group);
		list = (ExpandableListView)curView.findViewById(R.id.tt_fragment_group_list);
		init();
	}
	private void initData(){
		List<GroupInfo> groupList = new ArrayList<GroupInfo>();
		List<Contact> contactList = null;
		GroupInfo groupInfo = null;
		Contact contact = null;

		contactList = new ArrayList<Contact>();
		contact = new Contact();
		contact.account = "小明";
		contact.name = "小明";
		contactList.add(contact);
		contact = new Contact();
		contact.account = "小红";
		contact.name = "小红";
		contactList.add(contact);

		groupInfo = new GroupInfo();
		groupInfo.setName("开发部");
		groupInfo.setContactList(contactList);

		groupList.add(groupInfo);
		//**********************

		contactList = new ArrayList<Contact>();
		contact = new Contact();
		contact.account = "大明";
		contact.name = "大明";
		contactList.add(contact);
		contact = new Contact();
		contact.account = "大红";
		contact.name = "大红";
		contactList.add(contact);

		groupInfo = new GroupInfo();
		groupInfo.setName("销售部");
		groupInfo.setContactList(contactList);

		groupList.add(groupInfo);
		//**********************

		contactList = new ArrayList<Contact>();
		contact = new Contact();
		contact.account = "中明";
		contact.name = "中明";
		contactList.add(contact);

		groupInfo = new GroupInfo();
		groupInfo.setName("财务部");
		groupInfo.setContactList(contactList);

		groupList.add(groupInfo);
		//**********************
		for(int i=0;i<groupList.size();i++){
			System.out.println(groupList.get(i).getName());
			for(int j=0;j<groupList.get(i).getContactList().size();j++){
				System.out.println(" "+groupList.get(i).getContactList().get(j).name);
			}
		}
		adapter = new GroupAdapter(getActivity(),groupList);
		list.setAdapter(adapter);
	}
}

记得有两个辅助类写一下

class GroupInfo {
	private String name;
	private List<Contact> contactList;

	public List<Contact> getContactList() {
		return contactList;
	}

	public void setContactList(List<Contact> contactList) {
		this.contactList = contactList;
	}

	public String getName() {
		return name;
	}

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

class Contact{
	public String account , name;

	public Contact(){}

	public String getAccount () {
		return account ;
	}

	public void setAccount(String account) {
		this.account = account ;
	}

	public String getName() {
		return name;
	}

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

最后写Activity里面的布局

<!--      android:cacheColorHint="#00000000",这个设置可以去除拖动view时背景变成黑色的效果
		     android:listSelector="#00000000" ,可以去除选中时的黄色底色 -->
    <ExpandableListView
        android:id="@+id/tt_fragment_group_list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5dip"
        android:cacheColorHint="@android:color/transparent"
        android:background="#ffffff"
        android:listSelector="@android:color/transparent"
        android:groupIndicator="@drawable/tt_drop_select"
        android:divider="@drawable/tt_divide_line"
        android:childDivider="@drawable/tt_divide_line"
        />

差点忘记了点击时的效果,看下面来了

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

    <item
        android:drawable="@drawable/tt_drop_up"
        android:state_expanded="false"
        android:drawingCacheQuality="auto"/>

    <item
        android:drawable="@drawable/tt_drop_down"
        android:state_expanded="true"
        android:drawingCacheQuality="auto"/>

</selector>

图标在下面,想用的话直接下载吧,图标有点不对称,我不会p图,随便下的
连图标都上传了

终于写好了,怎么感觉像王大妈的臭脚,又臭 又长了,可能是我在做的过程中很多细节都遇到困难,所以才写出来,希望别人做的时候不会再遇到了 
现在看看效果吧

时间: 2024-08-26 13:06:10

android之ExpandableListView的相关文章

android的ExpandableListView

activity_main.xml <RelativeLayout 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_

22.Android之ExpandableListView树形列表学习

Android经常用到树形菜单,一般ExpandableListView可以满足这个需要,今天学习下. XML代码: 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent

Android中ExpandableListView控件基本使用

本文採用一个Demo来展示Android中ExpandableListView控件的使用,如怎样在组/子ListView中绑定数据源.直接上代码例如以下: 程序结构图: layout文件夹下的 main.xml 文件源代码例如以下: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/a

Android在ExpandableListView控制的基本使用

在本文中,Demo为了展示Android在ExpandableListView用途管制.如该组/儿子ListView绑定数据源. 直接上代码例如以下: 程序结构图: layout文件夹下的 main.xml 文件源代码例如以下: <? xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/an

Android中ExpandableListView中嵌套ListView

最近项目挺紧张,一直没有时间总结学习,今天把最近遇到的一个奇葩的设计,做一下总结.其他的好多的APP中做的通讯录都类似微信通讯录这样,但是有这样一个需求的设计. 就是分为两个组,第一个组不需要A-Z的索引,第二组需要A-Z的索引.以下是我的实现思路. 1.实现思路 1.写布局文件,将ExpandableListView添加到布局文件. 2.写MyExpandableListViewAdapter的实现 3.ListView设置为不可滑动. 4.再第一组中,添加一个ListView,第二个组中,添

Android 利用ExpandableListView显示和查询仿QQ分组列表用户信息

在我们的项目开发过程中,经常会对用户的信息进行分组,即通过组来显示用户的信息,同时通过一定的查询条件来显示查询后的相关用户信息,并且通过颜色选择器来设置列表信息的背景颜色. 其中借鉴xiaanming:http://blog.csdn.net/xiaanming/article/details/12684155 下面来看看项目运行后的效果图以及代码结构图: 下面通过代码来实现整个效果. 1.主界面布局activity_main.xml <span style="font-size:18px

Android的ExpandableListView的学习

最近在公司做项目,有一个小模块,需要用到ExpandableListView这个控件,所以在工作中边学习边工作.根据需求,在进入ExpandableLisView的时候默认展开第一组,需要用到这个属性,expandableListView.expandGroup(0); 在ExpandableListView中,当点击一个组展开时,其他展开的组是不自动关上的,这需要在监听事件中处理一下: @Override public void onGroupExpand(int groupPosition)

Android中ExpandableListView的使用

ExpandableListView是Android中可以实现下拉list的一个控件,具体的实现方法如下: 首先:在layout的xml文件中定义一个ExpandableListView view plain copy to clipboard print ? < LinearLayout android:id ="@+id/linearLayout" android:layout_width ="fill_parent" android:layout_hei

android原生ExpandableListView

android原生可扩展ExpandableListView就是可以伸缩的listView,一条标题下面有多条内容. 这个list的adapter对的数据要求与普通ListView的数据要求也有一些差别,这个list需要有两个数据源 一半需要 List<String> groups 作为group 和一个 Map<String , List<String>> children 作为children,他们最好意义对应 ExpandableListView使用起来和普通的l

Android的ExpandableListView的动画展开效果和使用traceview的性能优化

Android的原生提供和展开分组的ListView:ExpandableListView,然而相比于iOS上原生提供的UITableView,其UI能力不足,比如没有原生的动画展开和收起效果支持. 然而性能的优化是没有止境的,当分组内的子view(childView)变得复杂,或者ListView的parent结构复杂,例如内嵌与其它 LinearLayout, FrameLayout或者ScrollView之中,并且parent的使用自定义的重写的onMeasure()方法时,生成child