前言:今天休假回来的第一天,突然不知道该写点什么了,打开微信也找不到人聊天。。。无聊只好翻阅聊天记录,突然有了想法:今天写一下聊天记录的实现。
我们的聊天记录是保存在本地的,利用安卓自带的轻量化数据库SQlite实现。设计思路是这样的,对于会话列表我们只保存与某一好友聊天的最后一条记录,而进入会话界面后再查询所有记录,也就是会话列表与聊天记录实际上是两个表,在这里我们一起来了解一下会话记录的实现。
Step1,设计界面
整个会话记录界面其实就是一个首部与一个列表组成,首部即朋友圈消息,列表则是展现会话记录,每一条会话记录由对方头像、昵称、最后一条消息以及时间构成,界面整体布局代码如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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_parent" android:orientation="vertical" tools:context="com.example.administrator.parentproject.activity.AllWorkActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="@dimen/dp_72" android:background="@drawable/text_bg"> <TextView android:id="@+id/title_tv" android:layout_width="wrap_content" android:layout_height="@dimen/dp_72" android:layout_centerHorizontal="true" android:gravity="center" android:paddingTop="27dp" android:text="消息" android:textColor="@color/white" android:textSize="@dimen/text_14_sp" /> <ImageView android:layout_width="@dimen/dp_20" android:layout_height="@dimen/dp_20" android:layout_alignParentRight="true" android:layout_marginRight="@dimen/dp_10" android:layou