android之ExpandableListView

人生难得几回搏,此时不搏待何时。这篇文章主要讲述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" />

 
【android之ExpandableListView】差点忘记了点击时的效果,看下面来了
 
< ?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图,随便下的
连图标都上传了
 
android之ExpandableListView

文章图片
android之ExpandableListView

文章图片

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






    推荐阅读