模拟一个聊天的几面右边为模拟发送消息栏,左边显示为接收消息栏。
<LinearLayout 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="#d8e0e8"
android:orientation="vertical"
tools:context="com.example.liujing.uidemo.MainActivity">
<ListView
android:id="@+id/msg_list_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:divider="#0000">
ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/input_text"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:hint="请输入"
android:maxLines="2" />
<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="send" />
LinearLayout>
LinearLayout>
已收到消息为例:设置背景图片为我们事先准备好的一张聊天背景图,然后在它设为文字的背景即可。完整代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/left_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:background="@drawable/message_left">
<TextView
android:id="@+id/left_msg"
android:layout_gravity="center"
android:layout_margin="10dp"
android:textColor="#fff"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
LinearLayout>
<LinearLayout
android:id="@+id/right_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="@drawable/message_right">
<TextView
android:id="@+id/right_msg"
android:layout_gravity="center"
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
LinearLayout>
LinearLayout>
package com.example.liujing.uidemo;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.List;
/**
* Created by liujing on 16/2/6.
*/
public class MsgAdapter extends ArrayAdapter<Msg> {
private int resourceId;
public MsgAdapter(Context context, int textViewResourceId, List objects) {
//创建够着方法传入的三个参数分别是上下文,子项布局的id,数据源
super(context, textViewResourceId,objects);
this.resourceId = textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Msg msg = getItem(position);
//首先我们需要在这里得到当前item的位置
View view;
ViewHolder viewHolder;
//新建一个ViewHolder内部类,对控件实例进行缓存.
if(convertView == null){
//进行判断convertView是够为空,如果为空我们就进行加载布局,如果不为空我们直接将他拿来使用
view = LayoutInflater.from(getContext()).inflate(resourceId,null);
//这里通过LayoutInflater将对应子项布局的xml文件转化为view对象
viewHolder = new ViewHolder();
viewHolder.leftlayout = (LinearLayout) view.findViewById(R.id.left_layout);
viewHolder.rightlayout = (LinearLayout) view.findViewById(R.id.right_layout);
viewHolder.rightMsg = (TextView) view.findViewById(R.id.right_msg);
viewHolder.lestMsg = (TextView) view.findViewById(R.id.left_msg);
view.setTag(viewHolder);
//上面代码表示将viewHolder存储在view中
}else {
view = convertView;
viewHolder = (ViewHolder) view.getTag();
//重新获取ViewHolder
}
if(msg.getType()==Msg.TYPR_RECEIVED){
viewHolder.leftlayout.setVisibility(View.VISIBLE);
viewHolder.rightlayout.setVisibility(View.GONE);
viewHolder.lestMsg.setText(msg.getContent());
//判断如果是接收将发送聊天框隐藏,显示接收聊天框并将内内容显示上去
}else if(msg.getType()==Msg.TYPR_SEND){
viewHolder.rightMsg.setText(msg.getContent());
viewHolder.rightlayout.setVisibility(View.VISIBLE);
viewHolder.leftlayout.setVisibility(View.GONE);
}
return view;
}
class ViewHolder{
LinearLayout leftlayout;
LinearLayout rightlayout;
TextView lestMsg;
TextView rightMsg;
}
}
自定义一个Msg类:
package com.example.liujing.uidemo;
/**
* Created by liujing on 16/2/6.
*/
public class Msg {
public static final int TYPR_RECEIVED = 0;
public static final int TYPR_SEND = 1;
private String content;
private int type;
public Msg(String content, int type) {
this.content = content;
this.type = type;
}
public String getContent() {
return content;
}
public int getType() {
return type;
}
}
package com.example.liujing.uidemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView msglistview;
private EditText inputtext;
private Button send;
private MsgAdapter adapter;
private List msgList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initMsgs();
adapter = new MsgAdapter(MainActivity.this,R.layout.msg_item,msgList);
inputtext = (EditText) findViewById(R.id.input_text);
send = (Button) findViewById(R.id.send);
msglistview= (ListView) findViewById(R.id.msg_list_view);
msglistview.setAdapter(adapter);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String content = inputtext.getText().toString();
if(!"".equals(content)){
Msg msg = new Msg(content,Msg.TYPR_SEND);
msgList.add(msg);
adapter.notifyDataSetChanged();
msglistview.setSelection(msgList.size());
inputtext.setText("");
}
}
});
}
private void initMsgs(){
Msg msg1 = new Msg("在吗?",Msg.TYPR_SEND);
msgList.add(msg1);
Msg msg2 = new Msg("明天天气比较低,注意保暖",Msg.TYPR_SEND);
msgList.add(msg2);
Msg msg3 = new Msg("空气还是比较好不算坏",Msg.TYPR_SEND);
msgList.add(msg3);
Msg msg4 = new Msg("新年快乐",Msg.TYPR_SEND);
msgList.add(msg4);
Msg msg5 = new Msg("忙",Msg.TYPR_RECEIVED);
msgList.add(msg5);
}
}
ListView的使用时,从布局方面我们首先需要建立一个ListView的大布局,让后在建立每一个ListView项目的小布局。然后建立需要显示的数据源,并通过自定义Adapter的方法将数据源传入到ListView的itme中。在在定义Adapter的时候我们有两个办法可以提高效率1)进行判断convertView是够为空,如果为空我们就进行加载布局,如果不为空我们直接将他拿来使用。2)新建一个ViewHolder内部类,对控件实例进行缓存。