Android 仿微信QQ聊天界面
一些IM聊天软件的展现形式是左右分开的形式。比如说,别人给你发的信息全部靠左显示,你自己发给别人的信息全部靠右显示。
而我们的ListView很多时候是显示同一个布局,其实BaseAdapter中有2个重要的方法在大多数情况下我们并未使用到,一个是public int getViewTypeCount(),显示ListView中有多少种布局(默认是显示是1),像微信那样聊天界面,是有2种布局方式;另外一个getItemViewType(),可以让不同item条目加载不同的布局,下面就简单的模拟下微信的聊天界面做法:
MainActivity.java
- package com.jackie.wechat;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.ListView;
- import android.widget.TextView;
- public class MainActivity extends Activity {
- private ListView mListView;
- private List<Person> persons;
- private int TYPE_COUNT = 2;
- private int LEFT = 0;
- private int RIGHT = 1;
-
private LayoutInflater
mInflater; - private MyAdapter adapter;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mListView = (ListView) findViewById(R.id.listview);
- mInflater = LayoutInflater.from(this);
- initData();
- adapter = new MyAdapter();
- mListView.setAdapter(adapter);
- }
- private void initData() {
- persons = new ArrayList<Person>();
-
for(int i
= 0; i < 40;
i++){ - Person person = new Person();
- person.setAge(i);
- person.setName("更深的蓝");
-
if(i
% 2 == 0){ - person.setType(0);
- } else {
- person.setType(1);
- }
- persons.add(person);
- }
- }
- private class MyAdapter extends BaseAdapter{
- @Override
- public int getCount() {
- return persons.size();
- }
- @Override
- public Object getItem(int position) {
- return persons.get(position);
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
-
- @Override
- public int getItemViewType(int position) {
-
if(persons.get(position).getType()
== 0){ - return LEFT;
- }
- return RIGHT;
- }
- @Override
- public int getViewTypeCount() {
- return TYPE_COUNT;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- ViewHolder mHolder;
- Person person = persons.get(position);
-
if(getItemViewType(position)
== LEFT) { -
if (convertView
== null) { - mHolder = new ViewHolder();
- convertView = mInflater.inflate(R.layout.item_left, null);
- mHolder.tv_username = (TextView) convertView.findViewById(R.id.tv_username);
- mHolder.tv_age = (TextView) convertView.findViewById(R.id.tv_age);
- convertView.setTag(mHolder);
- } else {
- mHolder = (ViewHolder) convertView.getTag();
- }
- mHolder.tv_username.setText(person.getName());
- mHolder.tv_age.setText(String.valueOf(person.getAge()));
- } else {
-
if(convertView
== null){ - mHolder = new ViewHolder();
- convertView = mInflater.inflate(R.layout.item_right, null);
- mHolder.tv_username = (TextView) convertView.findViewById(R.id.tv_username);
- mHolder.tv_age = (TextView) convertView.findViewById(R.id.tv_age);
- convertView.setTag(mHolder);
- }else{
- mHolder = (ViewHolder) convertView.getTag();
- }
-
- mHolder.tv_username.setText(person.getName());
- mHolder.tv_age.setText(String.valueOf(person.getAge()));
-
}
- return convertView;
- }
-
private class ViewHolder
{ - TextView tv_username;
- TextView tv_age;
- }
- }
- }
Person.java
[java] view
plaincopyprint?
plaincopyprint?
- 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
[html] view
plaincopyprint?
plaincopyprint?
- <?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
[java] view
plaincopyprint?
plaincopyprint?
- <?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