android仿qq聊天界面的的布局适配器的写法

周六日的生活就搭在这上面了,做了一个聊天功能的app的demo,最近什么都不想干,但是也不能放弃,打算最近不弄Android了,想去学习一下js,因为接了一个开发网站的项目,但是目前的app项目做了一半,我我一个人做,老师把任务分下来,全部压在我头上了,前几天给一个女生表白失败了,心情超烂,程序员注定孤独终老吧,哎,什么都不说了,不然太对不起你们来看我的这篇博文了!今天我要说的是仿qq聊天界面的实现适配器的实现,



这个是发送方的布局,代码如下,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:paddingTop="13dp" >
<!-- 聊天界面适配器布局(接受消息布局) -->
    <TextView
        android:id="@+id/timestamp"
        style="@style/chat_text_date_style"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/margin_chat_activity" >

        <com.agbc.ui.CircleImageView
            android:id="@+id/iv_userhead"
            android:layout_width="@dimen/size_avatar"
            android:layout_height="@dimen/size_avatar"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="@dimen/margin_chat_activity"
            android:src="@drawable/default_head"
            />

        <TextView
             android:maxWidth="225.0dip"
            android:id="@+id/tv_chatcontent"
            style="@style/chat_content_date_style"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="8dip"
            android:gravity="left|center"
             android:lineSpacingExtra="2dp"
            android:layout_marginRight="@dimen/margin_chat_activity"
            android:layout_toLeftOf="@id/iv_userhead"
            android:background="@drawable/chatto_bg" />
        
     <!--    <TextView
            android:id="@+id/tv_userid"
            style="@style/chat_text_name_style"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_toRightOf="@id/tv_chatcontent"
            android:layout_below="@id/iv_userhead"/> -->

        <ImageView
            android:id="@+id/msg_status"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@id/tv_chatcontent"
            android:clickable="true"
            android:scaleType="fitXY"
            android:src="@drawable/msg_state_failed_resend"
            android:visibility="gone" />
        
        <TextView
            android:id="@+id/tv_ack"
            style="@style/chat_text_name_style"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:text="已读"
            android:textSize="12sp"
            android:layout_toLeftOf="@id/tv_chatcontent"
            android:visibility="invisible"
            />
        
        <ProgressBar 
            android:visibility="visible"
            android:id="@+id/pb_sending"
            android:layout_width="25dp"
            android:layout_height="25dp"
              android:layout_centerVertical="true"
            android:layout_toLeftOf="@id/tv_chatcontent"
            
            />
    </RelativeLayout>

</LinearLayout>

效果图在下面,看看是不是小伙伴们要的效果

下面就是接收消息的布局代码,其实就 是在左边显示人物信息和消息类容

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    
    android:paddingTop="13dp" >
<!-- 聊天界面的适陪布局 (接受消息的布局)-->
    <TextView
        android:id="@+id/timestamp"
        style="@style/chat_text_date_style"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp" >

        <com.agbc.ui.CircleImageView
            android:id="@+id/iv_userhead"
            android:layout_width="@dimen/size_avatar"
            android:layout_height="@dimen/size_avatar"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="@dimen/margin_chat_activity"
            android:src="@drawable/default_head"
             />

        <TextView
             android:maxWidth="225.0dip"
            android:id="@+id/tv_chatcontent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/margin_chat_activity"
            android:layout_toRightOf="@id/iv_userhead"
            android:background="@drawable/chatfrom_bg"
            android:clickable="true"
            android:focusable="true"
            android:gravity="left|center"
            android:lineSpacingExtra="2dp"
            android:minHeight="50dp"
            android:textColor="#ff000000"
            android:textSize="15sp" />
        <TextView
            android:id="@+id/tv_userid"
            style="@style/field_label_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_toLeftOf="@id/tv_chatcontent"
            android:layout_below="@id/iv_userhead"/>
    </RelativeLayout>

</LinearLayout>

效果图如下

当我们把这连个布局写好之后,我们就要写适配器的代码了,其实这个适配器还是好写的,当然啦,旁征博引嘛,会这个了就会其他的啦!

首先,我们定义了一个消息的实体,其实就是包含消息的一些必要的属性吧!因为用ksoap webservice通信,所有就实现

KvmSerializable接口,小伙伴们具体看看,
package com.agbc.entity;

import java.util.Date;
import java.util.Hashtable;

import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;

/**
 * TCommunity entity. @author MyEclipse Persistence Tools
 */
public class TCommunity implements KvmSerializable {
	// Fields
	private Integer CId;
	private Integer UId;
	private Integer TBuid;
	private String CContent;
	private String CDate;
	private Integer TId;
	private int CState;
	 //是否为对方发来的信息  
    private boolean isComMeg = true; 

	// Constructors

	public boolean isComMeg() {
		return isComMeg;
	}
	public void setComMeg(boolean isComMeg) {
		this.isComMeg = isComMeg;
	}
	/** default constructor */
	public TCommunity() {
	}
	@Override
	public Object getProperty(int arg0) {
		switch (arg0) {
		case 0:
			return CId;
		case 1:
			return UId;
		case 2:
			return TBuid;
		case 3:
			return CContent;
		case 4:
			return CDate;
		case 5:
			return TId;
		case 6:
			return CState; 
		default:
			break;
		}
		return null;
	}

	@Override
	public int getPropertyCount() {
		return 7;
	}

	@Override
	public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) {
		switch (arg0) {
		case 0:
			arg2.type=PropertyInfo.STRING_CLASS;
			arg2.name="CId";
			break;
		case 1:
			arg2.type=PropertyInfo.STRING_CLASS;
			arg2.name="UId";
			break;
		case 2:
			arg2.type=PropertyInfo.STRING_CLASS;
			arg2.name="TBuid";
			break;
		case 3:
			arg2.type=PropertyInfo.STRING_CLASS;
			arg2.name="CContent";
			break;
		case 4:
			arg2.type=PropertyInfo.STRING_CLASS;
			arg2.name="CDate";
			break;
		case 5:
			arg2.type=PropertyInfo.STRING_CLASS;
			arg2.name="TId";
			break;
		case 6:
			arg2.type=PropertyInfo.STRING_CLASS;
			arg2.name="CState";
			break;
		default:
			break;
		}
	}

	@Override
	public void setProperty(int arg0, Object arg1) {
		switch (arg0) {
		case 0:
			CId=Integer.parseInt(arg1.toString());
			break;
		case 1:
			UId=Integer.parseInt(arg1.toString());
			break;
		case 2:
			TBuid=Integer.parseInt(arg1.toString());
			break;
		case 3:
			CContent=arg1.toString();
			break;
		case 4:
			CDate= arg1.toString();
			break;
		case 5:
			TId=Integer.parseInt( arg1.toString());
			break;
		case 6:
			CState=Integer.parseInt(arg1.toString());
			break;
		default:
			break;
		}
	}



	public Integer getCId() {
		return CId;
	}



	public void setCId(Integer cId) {
		CId = cId;
	}



	public Integer getUId() {
		return UId;
	}



	public void setUId(Integer uId) {
		UId = uId;
	}



	public Integer getTBuid() {
		return TBuid;
	}



	public void setTBuid(Integer tBuid) {
		TBuid = tBuid;
	}



	public String getCContent() {
		return CContent;
	}



	public void setCContent(String cContent) {
		CContent = cContent;
	}



	public String getCDate() {
		return CDate;
	}



	public void setCDate(String cDate) {
		CDate = cDate;
	}



	public Integer getTId() {
		return TId;
	}



	public void setTId(Integer tId) {
		TId = tId;
	}



	public int getCState() {
		return CState;
	}



	public void setCState(int cState) {
		CState = cState;
	}
	


}

最后就是适配器代码了,为了简便,我写了一个基类适配器,可以减少代码量,其实程序员应该学会偷懒,写一个万能的类,

首先是基类适配器

package com.agbc.adapter;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
//基类适配器,
public abstract class AgbcBaseAdapter<T> extends BaseAdapter  {

	public Context content;
	public  List<T> list;
	public AgbcBaseAdapter(List<T> list,Context content) {
		this.list=list;
		this.content=content;
	}
	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public abstract View getView(int position, View convertView, ViewGroup parent); 


}
最后就是我们聊天界面的适配器了,

package com.agbc.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.agbc.entity.TCommunity;
import com.agbc.ui.CircleImageView;
import com.example.agbc.R;
/**
 * 聊天界面适配器(适配器的布局又消息的类型而定)
 * @author twj
 * 2016-4-17
 *
 */
public class ChatMsgViewAdapter extends AgbcBaseAdapter<TCommunity> {
	private List<TCommunity> list;//消息实体集合
	private Context context;//山下文环境
	private LayoutInflater layoutInflater;
	private static final String TAG = ChatMsgViewAdapter.class.getSimpleName(); 

	public interface IMsgViewType{//定义一个消息接口
		//对方发来的信息  
		int IMVT_COM_MSG = 0;  
		//自己发出的信息  
		int IMVT_TO_MSG = 1;  
	}  

	//获取项的类型  
	public int getItemViewType(int position) {  
		TCommunity entity = list.get(position);  

		if (entity.isComMeg())  
		{  
			return IMsgViewType.IMVT_COM_MSG;  
		}else{  
			return IMsgViewType.IMVT_TO_MSG;  
		}  

	}  
	//获取项的类型数  
	public int getViewTypeCount() {  
		// TODO Auto-generated method stub  
		return 2;  
	}  

	@SuppressWarnings("unchecked")
	public ChatMsgViewAdapter(List<TCommunity> list, Context context) {
		super(list, context);
		this.context=context;
		this.list=list;
		layoutInflater=LayoutInflater.from(context);

	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		TCommunity tCommunity=list.get(position);
		boolean msgtype=tCommunity.isComMeg();
		ViewHolder viewHolder = null;   
		if (convertView==null) {
			if (msgtype) {
				convertView=layoutInflater.inflate(R.layout.row_received_message, null);
			}else {
				convertView=layoutInflater.inflate(R.layout.row_sent_message, null);
			}	
			 viewHolder = new ViewHolder();
			 viewHolder.iv_userhead=(CircleImageView) convertView.findViewById(R.id.iv_userhead);
			 viewHolder.timestamp=(TextView) convertView.findViewById(R.id.timestamp);
			 viewHolder.msg_status=(ImageView) convertView.findViewById(R.id.msg_status);
			 viewHolder.tv_ack=(TextView) convertView.findViewById(R.id.tv_ack);
			 viewHolder.tv_chatcontent=(TextView) convertView.findViewById(R.id.tv_chatcontent);
			 viewHolder.isComMsg=msgtype;
		}else {
			viewHolder = (ViewHolder) convertView.getTag();  
		}
		 viewHolder.tv_chatcontent.setText(list.get(position).getCContent());
		return convertView;
	}


	class ViewHolder {
		TextView  
		timestamp,
		tv_ack,
		tv_chatcontent;

		CircleImageView iv_userhead;

		ImageView msg_status;

		public boolean isComMsg = true;  
		ProgressBar pb_sending;
	}
}

好了,大概就是这么多,其实我做了一个比较复杂的,因为代码为项目小组共同的吧,关系到一些问题,我就不详细说了,需要源码的小伙伴给我留言吧!我看到了给你发的,好了,就这样了!我要回宿舍睡一下,心太累了,敲代码没动力了,过几天吧!过几天就好了
©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值