Android中使用ExpandableListView实现微信通讯录界面(完善仿微信APP),expandablelistview
Android中使用ExpandableListView实现微信通讯录界面(完善仿微信APP),expandablelistview
之前的博文《Android中使用ExpandableListView实现好友分组》我简单介绍了使用ExpandableListView实现简单的好友分组功能,今天我们针对之前的所做的仿微信APP来对ExpandableListView做一个扩展介绍,实现效果如下(通讯里使用ExpandableListView实现):
相关知识点博文链接:
Android中使用ExpandableListView实现好友分组
Android中Fragment和ViewPager那点事儿
Android中ListView实现图文并列并且自定义分割线(完善仿微信APP)
正常使用ExpandableListView的思路如下:
(1)要给ExpandableListView 设置适配器,那么必须先设置数据源。
(2)数据源,就是此处的适配器类ExpandableAdapter,此方法继承了BaseExpandableListAdapter ,它是ExpandableListView的一个子类。需要重写里面的多个方法。方法的意思,代码中都有详细的注释。数据源中,用到了自定义的View布局,此时根据自己的需求,来设置组和子项的布局样式。getChildView()和getGroupView()方法设置自定义布局。
(3)数据源设置好,直接给 ExpandableListView.setAdapter()即可实现此收缩功能。
但本次实现除以上实现步骤之外,还需要注意的有以下几点:
(1)首次加载ExpandableListView需要默认全部展开,使用以下方法: 在给ExpandableListView 设置适配器后,添加以下代码:1 //Group.size()为组名个数,如果为数组存储则为group、length 2 for (int i = 0; i < Group.size(); i++) { 3 expandableListView.expandGroup(i); 4 }提醒:加载前别忘了判断adapter是否为空和有没有Group数据哦
(2)保持ExpandableListView始终展开无法收缩
1 expandableListView.setOnGroupClickListener(new OnGroupClickListener() { 2 @Override 3 public boolean onGroupClick(ExpandableListView parent, View v, 4 int groupPosition, long id) { 5 return true;//返回true则表示无法收缩 6 } 7 });(3)取消通讯录上方的groupName空间 微信通讯录中“新的朋友”,“群聊”,“标签”,“公众号”,作为一个整体自定义布局添加到ExpandableListView中,详情见以下代码实现 (4)修改ExpandableListView的分割线 大概思路就是这样,现在开始整体实现代码的演示:
第一步:layout中通讯录整体布局contactfragment.xml:
其实就是一个ExpandableListView,添加android:divider ="#FFFFFF"取消自带分割线
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:background="@color/fragmentback"> 6 <ExpandableListView 7 android:id="@+id/contact_list" 8 android:layout_width="match_parent" 9 android:layout_height="match_parent" 10 android:layout_alignParentTop="true" 11 android:layout_alignParentStart="true" 12 android:divider ="#FFFFFF"/> 13 </LinearLayout>
第二步:layout中组名(groupName)的布局文件contact_list_group_item.xml:
注意设置间距,保证美观且尽量与微信一致
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:background="@color/fragmentback"> 6 <TextView 7 android:text="TextView" 8 android:textSize="20sp" 9 android:layout_width="match_parent" 10 android:layout_height="wrap_content" 11 android:layout_marginLeft="10dp" 12 android:gravity="center_vertical" 13 android:id="@+id/group_tv" /> 14 </LinearLayout>
第三步:layout中ExpandableListView中每个item的布局文件contact_list_item.xml:
这里添加了自定义分割线1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 <LinearLayout 6 android:background="@color/colorwhite" 7 android:layout_width="match_parent" 8 android:layout_height="match_parent" 9 android:orientation="vertical"> 10 <LinearLayout 11 android:paddingLeft="10dp" 12 android:paddingTop="5dp" 13 android:paddingBottom="5dp" 14 android:gravity="center_vertical" 15 android:layout_width="match_parent" 16 android:layout_height="wrap_content" 17 android:orientation="horizontal"> 18 <ImageView 19 android:id="@+id/contact_item_iv" 20 android:layout_width="wrap_content" 21 android:layout_height="wrap_content" 22 android:src="@mipmap/default_fmessage" 23 android:adjustViewBounds="true" 24 android:maxWidth="35dp"/> 25 <TextView 26 android:id="@+id/contact_item_tv" 27 android:layout_margin="10dp" 28 android:layout_width="0dp" 29 android:layout_height="wrap_content" 30 android:layout_weight="1" 31 android:text="新的朋友"/> 32 </LinearLayout> 33 <View 34 android:layout_width="match_parent" 35 android:layout_height="1dp" 36 android:layout_marginLeft="10dp" 37 android:layout_marginRight="10dp" 38 android:background="@color/fragmentback"/> 39 </LinearLayout> 40 </LinearLayout>
第四步:layout中ExpandableListView中的头布局contact_list_title.xml(不需要groupName)
我们观察微信通讯录布局中“新的朋友”,“群聊”,“标签”,“公众号”上方直接为微信的顶部导航,不存在ExpandableListView一贯的组名布局,这里我们将 “新的朋友”,“群聊”,“标签”的布局单独实现:1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 <LinearLayout 6 android:background="@color/colorwhite" 7 android:layout_width="match_parent