Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 仿微信聊天界面

android 仿微信聊天界面

編輯:關於Android編程

一些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 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();
		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



    
    
	
	


item_right.xml



    
    
	
	


界面效果:


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved