android之ExpandableListView
来源:互联网 发布:平方c语言 编辑:程序博客网 时间:2024/06/06 10:50
最近在做企业难忘录,得使用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);}//获取指定组列表、指定子列表处的子列表数据@Overridepublic Object getChild(int groupPos, int childPos) {contactList = groupList.get(groupPos).getContactList();return contactList.get(childPos);}@Overridepublic long getChildId(int grounpPos, int childPos) {return childPos;}//返回特定组处的子列表数量@Overridepublic int getChildrenCount(int groupPos) {contactList = groupList.get(groupPos).getContactList();return contactList.size();}//该方法决定每个子选项的外观@Overridepublic 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;}//获取指定组位置处的数据@Overridepublic GroupInfo getGroup(int groupPos) {return groupList.get(groupPos);}@Overridepublic int getGroupCount() {return groupList.size();}@Overridepublic long getGroupId(int groupPos) {return groupPos;}@Overridepublic 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。 @Overridepublic boolean hasStableIds() {return true;}// 指定位置的子视图是否可选择。 @Overridepublic 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图,随便下的
连图标都上传了
终于写好了,怎么感觉像王大妈的臭脚,又臭 又长了,可能是我在做的过程中很多细节都遇到困难,所以才写出来,希望别人做的时候不会再遇到了
现在看看效果吧
0 0
- Android控件之ExpandableListView
- Android之ExpandableListView
- Android控件之ExpandableListView
- Android控件之ExpandableListView
- Android开发之ExpandableListView
- Android控件之ExpandableListView
- android之ExpandableListView
- Android控件之ExpandableListView
- Android开发之ExpandableListView
- Android之ExpandableListView
- Android之ExpandableListView
- Android之ExpandableListView
- Android开发之ExpandableListView
- Android开发之ExpandableListView
- Android 之ExpandableListView使用
- Android开发之ExpandableListView
- android 学习之ExpandableListView
- Android之ExpandableListView
- 使用Nexus搭建Maven私服
- 【算法】Trie数(前缀树/字典树)简介及Leetcode上关于前缀树的题
- 即使是一个技术人员,也需要知道沟通的重要性
- 海量数据处理题
- 算法训练 表达式计算
- android之ExpandableListView
- Android 学习资源
- poj2356 抽屉原理
- Linux基础命令1
- 基于Swift使用FMDB实现(增删改查+搜索+聊天置顶+分页读取)
- Django学习笔记
- java代码操作cmd二次运行新java程序
- Unity学习日记
- 安装MySQL总出现3534错误,也许你可以试试用cmd打开