Android聊天界面布局实例

ChatMsgViewAdapter.java:

package com.weijie.user.adapter;


import java.util.List;

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

import com.weijie.user.R;
import com.weijie.user.model.ChatMsgEntity;

public class ChatMsgViewAdapter extends BaseAdapter {

	public static interface IMsgViewType {
		int IMVT_COM_MSG = 0;
		int IMVT_TO_MSG = 1;
	}

	private static final String TAG = ChatMsgViewAdapter.class.getSimpleName();

	private List<ChatMsgEntity> coll;

	private Context ctx;

	private LayoutInflater mInflater;

	public ChatMsgViewAdapter(Context context, List<ChatMsgEntity> coll) {
		ctx = context;
		this.coll = coll;
		mInflater = LayoutInflater.from(context);
	}

	public int getCount() {
		return coll.size();
	}

	public Object getItem(int position) {
		return coll.get(position);
	}

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

	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		ChatMsgEntity entity = coll.get(position);

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

	}

	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return 2;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		final ChatMsgEntity entity = coll.get(position);
		boolean isComMsg = entity.getMsgType();

		ViewHolder viewHolder = null;
		if (convertView == null) {
			if (isComMsg) {
				convertView = mInflater.inflate(
						R.layout.chatting_item_msg_text_left, null);
			} else {
				convertView = mInflater.inflate(
						R.layout.chatting_item_msg_text_right, null);
			}

			viewHolder = new ViewHolder();
			viewHolder.tvSendTime = (TextView) convertView
					.findViewById(R.id.tv_sendtime);
			viewHolder.tvContent = (TextView) convertView
					.findViewById(R.id.tv_chatcontent);
			viewHolder.tvTitle = (TextView) convertView
					.findViewById(R.id.show_title);
			viewHolder.isComMsg = isComMsg;

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		viewHolder.tvSendTime.setText(entity.getDate());
		
		viewHolder.tvContent.setText(entity.getText());
		
		viewHolder.tvTitle.setText(entity.getTitle());
		viewHolder.tvContent.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
		
		return convertView;
	}

	static class ViewHolder {
		public TextView tvSendTime;
		public TextView tvUserName;
		public TextView tvContent;
		public TextView tvTime;
		public TextView tvTitle;
		public boolean isComMsg = true;
	}

}

AfterSalesDetailActivity.java:

package com.weijie.user.activity;


import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.widget.ListView;

import com.weijie.user.R;
import com.weijie.user.adapter.ChatMsgViewAdapter;
import com.weijie.user.model.ChatMsgEntity;

/**  
 * @ClassName: AfterSalesDetailActivity  
 * @Description: 售后详情  
 * @author chenzheng
 * @date 2014-5-9 上午10:49:33  
 */
public class AfterSalesDetailActivity extends BaseActivity {

	private ListView mListView;
	private ChatMsgViewAdapter mAdapter;
	private List<ChatMsgEntity> mDataArrays = new ArrayList<ChatMsgEntity>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.after_sales_detail); // 默认布局管理器

		initView();

		initData();
	}
	
	public void initView() {
		mListView = (ListView) findViewById(R.id.listview);
	}

	private String[] titleArray = new String[] { "创建了投诉","卖家不同意退货申请"};
	private String[] msgArray = new String[] { "订单号:456892110052345 商品名称:红玫瑰花束 投诉类型:商品质量问题",
			"亲,把商品出问题的地方拍下来,发给我,我这边验证一下啊"};

	private String[] dataArray = new String[] { "2012-10-31 18:00",
			"2012-10-31 18:10"};
	private int COUNT = titleArray.length;

	public void initData() {
		for (int i = 0; i < COUNT; i++) {
			ChatMsgEntity entity = new ChatMsgEntity();
			entity.setDate(dataArray[i]);
			if (i % 2 == 0) {
				entity.setMsgType(true);
			} else {
				entity.setMsgType(false);
			}
			entity.setTitle(titleArray[i]);
			entity.setText(msgArray[i]);
			mDataArrays.add(entity);
		}

		mAdapter = new ChatMsgViewAdapter(this, mDataArrays);
		mListView.setAdapter(mAdapter);

	}
	
}

ChatMsgEntity.java:

package com.weijie.user.model;


/**  
 * @ClassName: ChatMsgEntity  
 * @Description: 聊天信息类
 * @author chenzheng
 * @date 2014-4-23 上午9:21:15  
 */
public class ChatMsgEntity {
    private static final String TAG = ChatMsgEntity.class.getSimpleName();

    private String name;

    private String date;

    private String text;
    
    private String time;
    private String title;
    
    

    public String getTime() {
		return time;
	}

	public void setTime(String time) {
		this.time = time;
	}

	private boolean isComMeg = true;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public boolean getMsgType() {
        return isComMeg;
    }

    public void setMsgType(boolean isComMsg) {
    	isComMeg = isComMsg;
    }

    public ChatMsgEntity() {
    }

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

    

}

after_sales_detail.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white" >

    <!-- 标题栏 -->

    <RelativeLayout
        android:id="@+id/rl_layout"
        android:layout_width="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_height="48dp"
        android:background="@color/color_header"
        android:orientation="horizontal" >

        <!-- 返回按钮 -->

        <LinearLayout
            android:id="@+id/header_back"
            android:layout_width="@dimen/width_go_back"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:orientation="vertical" >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/go_back" />
        </LinearLayout>
        <!-- 售后详情 -->

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@id/header_back"
            android:text="售后详情"
            android:textColor="@color/white"
            android:textSize="@dimen/xl_size" />
    </RelativeLayout>

    <!-- 聊天内容 listview-->
    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/rl_layout"
        android:cacheColorHint="#0000"
        android:divider="@null"
        android:dividerHeight="20dp"
        android:scrollbarStyle="outsideOverlay"
        android:stackFromBottom="true" />
</RelativeLayout>

chatting_item_msg_text_left.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="6dp" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tv_sendtime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/textview_style_sendtime"
            android:textColor="#999999"
            android:textSize="14sp" />
    </LinearLayout>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="15dp"
        android:layout_marginTop="5dp" >

        <ImageView
            android:id="@+id/iv_userhead"
            android:layout_width="52dp"
            android:layout_height="52dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:background="@drawable/myhead"
            android:clickable="true" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_toRightOf="@id/iv_userhead"
            android:background="@drawable/msg_left_bg"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/show_title"
                android:layout_width="wrap_content"
                android:textColor="#666666"
                android:textSize="17sp"
                android:layout_height="wrap_content" />

            <View
                android:id="@+id/separate_line"
                android:layout_width="fill_parent"
                android:layout_height="2dip"
                android:background="#666666" />

            <TextView
                android:id="@+id/tv_chatcontent"
                android:layout_width="wrap_content"
                android:textColor="#666666"
                android:textSize="15sp"
                android:layout_height="wrap_content" />
        </LinearLayout>
        
    </RelativeLayout>

</LinearLayout>

chatting_item_msg_text_right.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="6dp" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tv_sendtime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/textview_style_sendtime"
            android:textColor="#999999"
            android:textSize="14sp" />
    </LinearLayout>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="5dp" >

        <ImageView
            android:id="@+id/iv_userhead"
            android:layout_width="52dp"
            android:layout_height="52dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:background="@drawable/goods_img"
            android:focusable="false" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:layout_toLeftOf="@id/iv_userhead"
            android:background="@drawable/msg_right_bg"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/show_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/white"
                android:textSize="17sp" />

            <View
                android:id="@+id/separate_line"
                android:layout_width="fill_parent"
                android:layout_height="2dip"
                android:background="@color/white" />

            <TextView
                android:id="@+id/tv_chatcontent"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/white"
                android:textSize="15sp" />
        </LinearLayout>
    </RelativeLayout>

</LinearLayout>

textview_style_sendtime.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <solid android:color="#EDEDED" />
	
    <padding
        android:bottom="4dp"
        android:top="4dp" 
        android:left="6dp"
        android:right="6dp"/>

     <corners android:radius="5dp" /> 
</shape>

相关推荐