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 coll;

	private Context ctx;

	private LayoutInflater mInflater;

	public ChatMsgViewAdapter(Context context, List 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 mDataArrays = new ArrayList();

	@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:

 




    

    

        

        

            
        
        

        
    

    
    

 

 

 

 

 

chatting_item_msg_text_left.xml:

 




    

        
    

    

        

        

            

            

            
        
        
    

 

 

 

 

 

chatting_item_msg_text_right.xml:

 




    

        
    

    

        

        

            

            

            
        
    

 

 

 

 

 

textview_style_sendtime.xml:

 




    
	
    

      

 

 

 

 

你可能感兴趣的:(Android进阶)