聊天界面

1.布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffffff"
    tools:context=".ChatActivity" >


    <RelativeLayout
        android:id="@+id/layout_actionbar"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_alignParentTop="true"
        android:background="@drawable/title_bar4" >
        
        <TextView
            android:id="@+id/textview_chatActionbar_mesCount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:paddingLeft="25dp"
            android:text="标题"
            android:textColor="#ffffffff"
            android:textSize="14sp" />


        <ImageView
            android:id="@+id/imageview_chat_actionbar_left"
            android:layout_width="23dp"
            android:layout_height="23dp"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:src="@drawable/left" />


        <TextView
            android:id="@+id/textview_chatActionbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="标题"
            android:textColor="#ffffffff"
            android:textSize="18sp" />


        <ImageView
            android:id="@+id/imageview_chat_actionbar_right"
            android:layout_width="36dp"
            android:layout_height="36dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:visibility="gone"
            android:src="@drawable/ic_launcher" />
    </RelativeLayout>


    <ListView
        android:id="@+id/listview_chat_context"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/include_chat_bottom"
        android:layout_below="@+id/layout_actionbar" >
    </ListView>


    <include
        android:id="@+id/include_chat_bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        layout="@layout/chat_bottom" />


</RelativeLayout>

界面


3.实现代码

1)创建一个adapter

public class ChatMessageListViewAdapter extends BaseAdapter{
private LayoutInflater inflate = null;
private List<ChatMessageActivity> allList = new ArrayList<ChatMessageActivity>();
private int id;
public ChatMessageListViewAdapter(Context context) {
super();
inflate = LayoutInflater.from(context);
}


public void addDatas(List<ChatMessageActivity> list){
if(list!=null){
allList.clear();
allList.addAll(list);
notifyDataSetChanged();
}
}


@Override
public int getCount() {
return allList.size();
}


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


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


@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = new ViewHolder();
ChatMessageActivity chatMessage = allList.get(position);
id = chatMessage.getId();
if(id == 1){
if(convertView == null){
convertView = inflate.inflate(R.layout.chat_left, null);
viewHolder.timing = (TextView) 
convertView.findViewById(R.id.textview_chat_left_date);
viewHolder.context = (TextView) 
convertView.findViewById(R.id.textview_chat_left_context);
viewHolder.logo = (ImageView) 
convertView.findViewById(R.id.imageview_chat_left_logo);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder)convertView.getTag();
}
viewHolder.timing.setText(chatMessage.getTime());
viewHolder.context.setText(chatMessage.getContext());
}else if(id == 2){
if(convertView == null){
convertView = inflate.inflate(R.layout.chat_left, null);
viewHolder.timing = (TextView) 
convertView.findViewById(R.id.textview_chat_right_date);
viewHolder.context = (TextView) 
convertView.findViewById(R.id.textview_chat_right_context);
viewHolder.logo = (ImageView) 
convertView.findViewById(R.id.imageview_chat_right_logo);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder)convertView.getTag();
}
viewHolder.timing.setText(chatMessage.getTime());
viewHolder.context.setText(chatMessage.getContext());
}
return convertView;
}
class ViewHolder{
TextView timing;
ImageView logo;
TextView context;
}
}

2)显示代码

public class ChatActivity extends Activity {
//ActionBar
private TextView title,msgCount;
//BottomBar
private ImageView add;
private EditText message;

private ListView listContext;
private LinearLayout chat_bottom;
private Button send;
private List<ChatMessageActivity> allMessage;
private ChatMessageListViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
initView();
setListener();
setOnClickMethod();
setListView();
}
private void initView() {
//初始化ActionBar
msgCount = (TextView) findViewById(R.id.textview_chatActionbar_mesCount);
title = (TextView) findViewById(R.id.textview_chatActionbar_title);
//初始化BottomBar
chat_bottom = (LinearLayout) findViewById(R.id.include_chat_bottom);
add = (ImageView) chat_bottom.findViewById(R.id.imageview_chatBottom_add);
message = (EditText) chat_bottom.findViewById(R.id.edittext_chatBottom_message);
message.setText("");
send = (Button) chat_bottom.findViewById(R.id.button_chatBottom_send);

allMessage = new ArrayList<ChatMessageActivity>();
listContext = (ListView) findViewById(R.id.listview_chat_context);

}
private void setListener() {
msgCount.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(ChatActivity.this,MainActivity.class);
startActivity(intent);
finish();
}
});
}
private void setOnClickMethod() {
send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String message_Str = message.getText().toString();
if(TextUtils.isEmpty(message_Str)){
return;
}
//格式化日期
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
long time = System.currentTimeMillis();
Date date = new Date(time);
String timeing = dateFormat.format(date);
//实体类(日期、内存、ID)
ChatMessageActivity chatMessage = new ChatMessageActivity();
chatMessage.setTime(timeing);
chatMessage.setContext(message_Str);
chatMessage.setId(1);
//将消息发出去
Message msg = new Message();
msg.obj = chatMessage;
msg.what = 1;
handler.sendMessage(msg);
//初始化
message.setText("");
}
});
}
private void setListView(){
adapter = new ChatMessageListViewAdapter(this);
listContext.setAdapter(adapter);
}
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
if(msg.what == 1){
ChatMessageActivity chatMessage = (ChatMessageActivity) msg.obj;
allMessage.add(chatMessage);
adapter.addDatas(allMessage);
listContext.setSelection(allMessage.size());
}
super.handleMessage(msg);
};
};
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值