---
title: "android聊天列表实现"
author: "phenom"
date: "2012-09-27T11:50:44Z"
tags: []
categories: ["移动开发"]
slug: "android"
draft: false
lastmod: "2012-09-27T11:50:44Z"
id: 1687262
url: "https://phenom.iteye.com/blog/1687262"
---

```java

关于聊天内容，见得多了，就是一个列表，里面存着不同的人的聊天记录，自己的内容在一边，其它人的内容在另一边。列表没有看到分隔线，背景是白的或其它的，没有列表按下的效果。

现在一个一个解决：
列表背景android:background="" android:cacheColorHint=""
这两个颜色设置成一样的，就可以了。
如果选中的颜色也不要，可以自定义一个selector，设置成固定 的颜色 android:listSelector="#00000000" 这样。

android:dividerHeight="0.0dip" 设置分隔线的高度为0，但是还可以看到一个分隔线。
还需要一个设置android:divider="@null"就可以完全移除分隔线了。

内容显示在左与右，最蠢的办法就是：
if (orientation==1) {
            ((LayoutInflater) mContext.getSystemService("layout_inflater")).inflate(R.layout.chat_left_item, this);

            chat_txt_time=(TextView) findViewById(R.id.chat_txt_time);
            chat_txt_msg=(TextView) findViewById(R.id.chat_txt_msg);
            //content=(LinearLayout) findViewById(R.id.content);
            nickname=(TextView) findViewById(R.id.chat_name);
        } else {
            ((LayoutInflater) mContext.getSystemService("layout_inflater")).inflate(R.layout.chat_right_item, this);

            chat_txt_time=(TextView) findViewById(R.id.chat_txt_time);
            chat_txt_msg=(TextView) findViewById(R.id.chat_txt_msg);
            //content=(LinearLayout) findViewById(R.id.content);
            nickname=(TextView) findViewById(R.id.chat_name);
        }
弄两个布局了，就可以解决了，但性能不高。

可以动态的处理View元素的位置，比如一个ImageView作头像，一个TextView作文本，使用relativelayout，使用java动态的处理它们间的关系，也是可以的。relativelayout addRule可以添加规则，动态的添加子元素，这样至少不必每次都新建一个Layout，可以重复使用原来的。
而上面的加载不同的布局，是不可以重用的，每次需要重新加载新的。

```