ANDROID 实现微信通讯录界面
正常使用ExpandableListView的思路如下:
(1)要给ExpandableListView 设置适配器,那么必须先设置数据源。
(2)数据源,就是此处的适配器类ExpandableAdapter,此方法继承了BaseExpandableListAdapter ,它是ExpandableListView的一个子类。
**
第一步:layout中通讯录整体布局contactfragment.xml:
**
<?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 <ExpandableListView7 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:
<?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 <TextView7 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:
<?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 <LinearLayout6 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)
<?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 <LinearLayout6 android:background="@color/colorwhite"7 android:layout_width="match_parent"8 android:layout_height="match_parent"9 android:orientation="vertical">10 <LinearLayout11 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 <ImageView19 android:layout_width="wrap_content"20 android:layout_height="wrap_content"21 android:src="@mipmap/default_fmessage"22 android:adjustViewBounds="true"23 android:maxWidth="35dp"/>24 <TextView25 android:layout_margin="10dp"26 android:layout_width="0dp"27 android:layout_height="wrap_content"28 android:layout_weight="1"29 android:text="新的朋友"/>30 </LinearLayout>31 <View32 android:layout_width="match_parent"33 android:layout_height="1dp"34 android:layout_marginLeft="10dp"35 android:layout_marginRight="10dp"36 android:background="@color/fragmentback"/>37 <LinearLayout38 android:paddingLeft="10dp"39 android:paddingTop="5dp"40 android:paddingBottom="5dp"41 android:gravity="center_vertical"42 android:layout_width="match_parent"43 android:layout_height="wrap_content"44 android:orientation="horizontal">45 <ImageView46 android:layout_width="wrap_content"47 android:layout_height="wrap_content"48 android:src="@mipmap/default_chatroom"49 android:adjustViewBounds="true"50 android:maxWidth="35dp"/>51 <TextView52 android:layout_margin="10dp"53 android:layout_width="0dp"54 android:layout_height="wrap_content"55 android:layout_weight="1"56 android:text="群聊"/>57 </LinearLayout>58 <View59 android:layout_width="match_parent"60 android:layout_height="1dp"61 android:layout_marginLeft="10dp"62 android:layout_marginRight="10dp"63 android:background="@color/fragmentback"/>64 <LinearLayout65 android:paddingLeft="10dp"66 android:paddingTop="5dp"67 android:paddingBottom="5dp"68 android:gravity="center_vertical"69 android:layout_width="match_parent"70 android:layout_height="wrap_content"71 android:orientation="horizontal">72 <ImageView73 android:layout_width="wrap_content"74 android:layout_height="wrap_content"75 android:src="@mipmap/default_contactlabel"76 android:adjustViewBounds="true"77 android:maxWidth="35dp"/>78 <TextView79 android:layout_margin="10dp"80 android:layout_width="0dp"81 android:layout_height="wrap_content"82 android:layout_weight="1"83 android:text="标签"/>84 </LinearLayout>85 <View86 android:layout_width="match_parent"87 android:layout_height="1dp"88 android:layout_marginLeft="10dp"89 android:layout_marginRight="10dp"90 android:background="@color/fragmentback"/>91 <LinearLayout92 android:paddingLeft="10dp"93 android:paddingTop="5dp"94 android:paddingBottom="5dp"95 android:gravity="center_vertical"96 android:layout_width="match_parent"97 android:layout_height="wrap_content"98 android:orientation="horizontal">99 <ImageView
100 android:layout_width="wrap_content"
101 android:layout_height="wrap_content"
102 android:src="@mipmap/default_servicebrand_contact"
103 android:adjustViewBounds="true"
104 android:maxWidth="35dp"/>
105 <TextView
106 android:layout_margin="10dp"
107 android:layout_width="0dp"
108 android:layout_height="wrap_content"
109 android:layout_weight="1"
110 android:text="公众号"/>
111 </LinearLayout>
112 </LinearLayout>
113 </LinearLayout>
第五步:java中定义继承BaseExpandableListAdapter类(自定义适配器)
1 import android.content.Context;2 import android.view.LayoutInflater;3 import android.view.View;4 import android.view.ViewGroup;5 import android.widget.BaseExpandableListAdapter;6 import android.widget.ImageView;7 import android.widget.TextView;8 import com.mly.panhouye.wechat.R;9 /**10 * Created by panchengjia on 2016/12/28 0028.11 */12 public class MyExpandableListAdapter extends BaseExpandableListAdapter {13 Context context;14 String[] group;15 String[][] itemName;16 int[][] itemIcon;17 public MyExpandableListAdapter(Context context, String[] group, String[][] itemName, int[][] itemIcon) {18 this.context = context;19 this.group = group;20 this.itemName = itemName;21 this.itemIcon = itemIcon;22 }23 24 @Override25 public int getGroupCount() {26 return group.length;27 }28 29 @Override30 public int getChildrenCount(int groupPosition) {31 return itemName[groupPosition].length;32 }33 34 @Override35 public Object getGroup(int groupPosition) {36 return group[groupPosition];37 }38 39 @Override40 public Object getChild(int groupPosition, int childPosition) {41 return itemName[groupPosition][childPosition];42 }43 44 @Override45 public long getGroupId(int groupPosition) {46 return groupPosition;47 }48 49 @Override50 public long getChildId(int groupPosition, int childPosition) {51 return childPosition;52 }53 54 @Override55 public boolean hasStableIds() {56 return false;57 }58 59 @Override60 public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {61 ViewHolder vh;62 //ExpandableList的第一个分组没有组名,这里需要自定义布局63 if(groupPosition==0){64 convertView =LayoutInflater.from(context).inflate(R.layout.contact_list_title,null);65 }else{66 if(convertView==null){67 convertView= LayoutInflater.from(context).inflate(R.layout.contact_list_group_item,null);68 vh = new ViewHolder();69 vh.tv = (TextView) convertView.findViewById(R.id.group_tv);70 convertView.setTag(vh);71 }72 vh = (ViewHolder) convertView.getTag();73 74 vh.tv.setText(group[groupPosition]);75 }76 77 return convertView;78 }79 80 @Override81 public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {82 ViewHolder vh;83 //ExpandableList的第一个分组没有组名,这里需要自定义布局84 if (groupPosition==0){85 convertView =LayoutInflater.from(context).inflate(R.layout.contact_list_title,null);86 }else{87 if(convertView==null){88 convertView= LayoutInflater.from(context).inflate(R.layout.contact_list_item,null);89 vh = new ViewHolder();90 vh.tv = (TextView) convertView.findViewById(R.id.contact_item_tv);91 vh.iv= (ImageView) convertView.findViewById(R.id.contact_item_iv);92 convertView.setTag(vh);93 }94 vh = (ViewHolder) convertView.getTag();95 vh.tv.setText(itemName[groupPosition][childPosition]);96 vh.iv.setImageResource(itemIcon[groupPosition][childPosition]);97 }98 return convertView;99 }
100 @Override
101 public boolean isChildSelectable(int groupPosition, int childPosition) {
102 return true;
103 }
104 class ViewHolder{
105 TextView tv;
106 ImageView iv;
107 }
108 }
第六步:java中重写之前的与contactfragment.xml布局对应的ContactFragment.java类
import android.os.Bundle;2 import android.support.annotation.Nullable;3 import android.support.v4.app.Fragment;4 import android.view.LayoutInflater;5 import android.view.View;6 import android.view.ViewGroup;7 import android.widget.ExpandableListView;8 import com.mly.panhouye.wechat.R;9 import com.mly.panhouye.wechat.adapter.MyExpandableListAdapter;
10
11 /**
12 * Created by panchengjia on 2016/12/28 0028.
13 */
14
15 public class ContactFragment extends Fragment {
16 private ExpandableListView contact_list;
17 //定义分组以及组内成员(设置头文件位置为空)
18 String[] group ={"","好友列表"};
19 String[][] itemName={{},{"郭嘉", "黄月英", "华佗",
20 "刘备", "陆逊", "吕布", "吕蒙", "马超", "司马懿", "孙权", "孙尚香", "夏侯惇",
21 "许褚", "杨修", "张飞", "赵云", "甄姬", "周瑜", "诸葛亮"}};
22 int[][] itemIcon={{},{R.mipmap.guojia,
23 R.mipmap.huangyueying, R.mipmap.huatuo,
24 R.mipmap.liubei, R.mipmap.luxun, R.mipmap.lvbu, R.mipmap.lvmeng,
25 R.mipmap.machao, R.mipmap.simayi, R.mipmap.sunquan, R.mipmap.sunshangxiang,
26 R.mipmap.xiahoudun, R.mipmap.xuchu, R.mipmap.yangxiu, R.mipmap.zhangfei,
27 R.mipmap.zhaoyun, R.mipmap.zhenji, R.mipmap.zhouyu, R.mipmap.zhugeliang}};
28 @Override
29 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
30 View view = inflater.inflate(R.layout.contact_fragment,container,false);
31 contact_list = (ExpandableListView) view.findViewById(R.id.contact_list);
32 //实例化适配器
33 MyExpandableListAdapter myExpandableListAdapter=new MyExpandableListAdapter(getContext(),group,itemName,itemIcon);
34 //配置适配器
35 contact_list.setAdapter(myExpandableListAdapter);
36 //去掉ExpandableListView 默认的箭头
37 contact_list.setGroupIndicator(null);
38 //设置ExpandableListView默认展开
39 for (int i = 0; i <group.length; i++) {
40 contact_list.expandGroup(i);
41 }
42 //设置ExpandableListView不可点击收回
43 contact_list.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
44 @Override
45 public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
46 return true;
47 }
48 });
49 return view;
50 }
51 }
ANDROID 实现微信通讯录界面相关推荐
- android 仿微信demo————微信通讯录界面功能实现(移动端,服务端)
android 仿微信demo----微信启动界面实现 android 仿微信demo----注册功能实现(移动端) android 仿微信demo----注册功能实现(服务端) android 仿微 ...
- 【Android】快速实现仿美团选择城市界面,微信通讯录界面
概述 本文是这个系列的第三篇,不出意外也是终结篇.因为使用经过重构后的控件已经可以快速实现市面上带 索引导航.悬停分组的列表界面了. 在前两篇里,我们从0开始,一步一步实现了仿微信通讯录.饿了么选餐界 ...
- Android仿微信通讯录
Android仿微信通讯录 分3部: 1.listview实现显示头像.名字(太简单,这里就不写了) 通讯录页面xml布局代码: <LinearLayout xmlns:android=&quo ...
- android 仿微信聊天界面 以及语音录制功能,Android仿微信录制语音功能
本文实例为大家分享了Android仿微信录制语音的具体代码,供大家参考,具体内容如下 前言 我把录音分成了两部分 1.UI界面,弹窗读秒 2.一个类(包含开始.停止.创建文件名功能) 第一部分 由于6 ...
- 【Android 仿微信通讯录 导航分组列表-上】使用ItemDecoration为RecyclerView打造带悬停头部的分组列表
*本篇文章已授权微信公众号 guolin_blog (郭霖)独家发布 转载请标明出处: http://blog.csdn.net/zxt0601/article/details/52355199 本文 ...
- Android仿微信聊天界面
今天说说android的仿微信聊天界面,我只想说两个字:坑爹 项目已经传到了github: https://github.com/hebiao6446/Hantu-android- 还好我写过iOS仿 ...
- Android微信通讯录界面代码,Android中使用Expandablelistview实现微信通讯录界面
之前的博文<Android 中使用ExpandableListView 实现分组的实例>我简单介绍了使用ExpandableListView实现简单的好友分组功能,今天我们针对之前的所做的 ...
- Android中使用ExpandableListView实现微信通讯录界面(完善仿微信APP)
之前的博文<Android中使用ExpandableListView实现好友分组>我简单介绍了使用ExpandableListView实现简单的好友分组功能,今天我们针对之前的所做的仿微信 ...
- 【Android 仿微信通讯录 导航分组列表-下】自定义View为RecyclerView打造右侧索引导航栏IndexBar
本篇文章已授权微信公众号 guolin_blog (郭霖)独家发布 转载请标明出处: http://blog.csdn.net/zxt0601/article/details/52420706 本文出 ...
最新文章
- 100天59万行代码_如何抽出100天的代码时间
- 你在中国有去过那些地区?
- 漫话:程序员要失业了??!机器人开始在GitHub上修Bug了。
- 【spring boot】【POI】错误:The supplied data appears to be in the Office 2007+ XML
- php 设置post大小_php 修改上传文件大小限制实例详解
- php raabitmq中间件_rabbitMQ消息中间件环境配置及原理了解
- 华为二层创建vlan_二层交换机不同VLAN实现互通 (华为)
- 控件ListView相关属性 1217
- Java Web解决跨域请求,java初级面试笔试题
- 【Elasticsearch】Elasticsearch 通信模块的分析
- 饿了么element UIel-dialog弹出层/el-dialog修改默认样式不能在style scoped修改
- 深入理解ArrayList 和 LinkedList 区别
- 添加WSS3.0中文模板
- Origin科研绘图实战
- 不动点求数列通项原理_【数列】浅谈“不动点”求数列通项的方法
- delphi学习笔记(2)-object pascal语言的语句 选择自 xmz2629 的 Blog
- 三进制计算机可以实现吗,制造三进制计算机的一种方法技术
- 验证错误信息jquery validation
- 东芝四轴机器人加相机
- 怎么从STM32将代码移植到海思开发板Hi3861
热门文章
- 抖音短连接v.douyin.com/xxx 如何生成?
- 字体设计中什么是断笔设计啊
- PPC/SP/PC汉化教程:如何汉化一个软件
- 站斧浏览器——用实力,说实话
- 连接到mysql提示错误:The server time zone value is unrecognized or represents more than one time zone
- 练习4-7 求e的近似值
- 解决 animation.FuncAnimation 更新函数无反应的方法
- 2023临沂大学计算机考研信息汇总
- React中文文档之Handling Events
- Win10(Win7)通过注册表(regedit)添加、修改、删除系统环境变量、或系统服务(services.msc)