在openfire服务器下制作的仿微信会话界面,包括接收用户的消息,初始化数据列表ListView,好友名称,好友头像,聊天消息,消息发送时间等,以及统计好友消息数目。
界面效果:
由于会话界面用到的是Fragment,于是想要实现自定义ListView利用到了ListFragment组件,直接让当前Fragment继承ListFragment类;然后就是Fragment的布局文件和item布局文件,将item整理成理想的样式;在Fragment中获取用户收到的好友消息,好友名,头像等,使用SimpleAdapter适配器进行数据适配;统计好友发送的消息数目,显示在UI上,已经点击item项触发事件,跳转聊天页面。
1、Fragment的布局文件fragment_one.xml,这里就仅仅是一个ListView,要注意的是ListView的id必须是Android:list,此为ListFragment指定的ListView项。
<?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="vertical" >
<ListView
android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
2、Fragment的item项布局文件fragment_one_item.xml,将所有的内容都放在一个相对布局中,头像和时间都单独放,将好友名和好友消息放置在一个垂直布局中,最后在加上红点文本,放置在最上端显示。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:background="@drawable/linearlayout_boder">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="horizontal" >
<ImageView
android:id="@+id/tab1_item_img"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center_vertical"
/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weig