一些IM聊天软件的展现形式是左右分开的形式。比如说,别人给你发的信息全部靠左显示,你自己发给别人的信息全部靠右显示。
而我们的ListView很多时候是显示同一个布局,其实BaseAdapter中有2个重要的方法在大多数情况下我们并未使用到,一个是public int getViewTypeCount(),显示ListView中有多少种布局(默认是显示是1),像微信那样聊天界面,是有2种布局方式;另外一个getItemViewType(),可以让不同item条目加载不同的布局,下面就简单的模拟下微信的聊天界面做法:
MainActivity.java
Person.java
-
package com.jackie.wechat;
-
-
public class Person {
-
private String name;
-
private int age;
-
private int type;
-
public String getName() {
-
return name;
-
}
-
-
public void setName(String name) {
-
this.name = name;
-
}
-
-
public int getAge() {
-
return age;
-
}
-
-
public void setAge(int age) {
-
this.age = age;
-
}
-
public int getType() {
-
return type;
-
}
-
-
public void setType(int type) {
-
this.type = type;
-
}
-
}
item_left.xml
-
<?xml version="1.0" encoding="utf-8"?>
-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent"
-
android:orientation="vertical"
-
android:background="#ffffff"
-
>
-
<RelativeLayout
-
android:layout_width="fill_parent"
-
android:layout_height="45dp"
-
>
-
<TextView
-
android:id="@+id/tv_username"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:textSize="16sp"
-
android:textColor="#123456"
-
android:layout_centerVertical="true"
-
android:layout_alignParentLeft="true"
-
/>
-
<TextView
-
android:id="@+id/tv_age"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:textSize="16sp"
-
android:textColor="#123456"
-
android:layout_centerVertical="true"
-
android:layout_toRightOf="@id/tv_username"
-
android:layout_marginLeft="20dp"
-
/>
-
</RelativeLayout>
-
</RelativeLayout>
item_right.xml
-
<?xml version="1.0" encoding="utf-8"?>
-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent"
-
android:orientation="vertical"
-
android:background="#ffffff"
-
>
-
<RelativeLayout
-
android:layout_width="fill_parent"
-
android:layout_height="45dp"
-
>
-
<TextView
-
android:id="@+id/tv_username"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:textSize="16sp"
-
android:textColor="#123456"
-
android:layout_centerVertical="true"
-
android:layout_alignParentRight="true"
-
/>
-
<TextView
-
android:id="@+id/tv_age"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:textSize="16sp"
-
android:textColor="#123456"
-
android:layout_centerVertical="true"
-
android:layout_toLeftOf="@id/tv_username"
-
android:layout_marginRight="20dp"
-
/>
-
</RelativeLayout>
-
</RelativeLayout>
Android 仿微信QQ聊天界面
原文:http://blog.csdn.net/shineflowers/article/details/41647761