android 仿微信聊天界面

时间:2014-11-27 23:47:13   收藏:0   阅读:642

一些IM聊天软件我们发现,他的展现形式,是左右分开的形式,而我们的listview很多时候是显示同一个布局,其实BaseAdapter中有2个重要的方法在大多数情况下我们并未使用到,一个是public int getViewTypeCount(),它是显示listview中有多少种布局,默认是显示是1,像微信那样聊天界面,是有2种布局方式,;另外一个getItemViewType(),它是在那些item条目中显示那种布局,下面就简单的模拟下微信的聊天界面做法:

MainActivity.java

package com.example.weixin;

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 listview;
	private List<Person> persons;
	private int TYPE_COUNT = 2;
	private int LEFT = 0;
	private int RIGHT = 1;
	private LayoutInflater inflater;
	private MyAdapter adapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		listview = (ListView) findViewById(R.id.listview);
		inflater = LayoutInflater.from(this);
		initData();
		adapter = new MyAdapter();
		listview.setAdapter(adapter);
	}
	private void initData() {
		persons = new ArrayList<Person>();
		for(int i=0;i<40;i++){
			Person p = new Person();
			p.setAge(i);
			p.setName("张三");
			if(i%2==0){
				p.setType(0);
			}else{
				p.setType(1);
			}
			persons.add(p);
		}
	}
	
	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 holder;
			Person p = persons.get(position);
			if(getItemViewType(position)==LEFT){
				if(convertView==null){
					holder = new ViewHolder();
					convertView =inflater.inflate(R.layout.item_left, null); 
					holder.tv_username = (TextView) convertView.findViewById(R.id.tv_username);
					holder.tv_age = (TextView) convertView.findViewById(R.id.tv_age);
					convertView.setTag(holder);
				}else{
					holder = (ViewHolder) convertView.getTag();
				}
				
				holder.tv_username.setText(p.getName());
				holder.tv_age.setText(String.valueOf(p.getAge()));
			}else{
				if(convertView==null){
					holder = new ViewHolder();
					convertView =inflater.inflate(R.layout.item_right, null);
					holder.tv_username = (TextView) convertView.findViewById(R.id.tv_username);
					holder.tv_age = (TextView) convertView.findViewById(R.id.tv_age);
					convertView.setTag(holder);
				}else{
					holder = (ViewHolder) convertView.getTag();
				}
				holder.tv_username.setText(p.getName());
				holder.tv_age.setText(String.valueOf(p.getAge()));
			}
			
			return convertView;
		}
		class ViewHolder{
			TextView tv_username;
			TextView tv_age;
		}
	}
}

Person.java

package com.example.weixin;

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>

界面效果:

bubuko.com,布布扣

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!