效果图:
GIF.gif1、导入Recyclerview依赖
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:recyclerview-v7:25.3.1'
testCompile 'junit:junit:4.12'
}
2、编写主界面 activity_main.xml中代码 :
<?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="match_parent"
android:orientation="vertical"
android:background="#d8e9e8">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycle_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/input_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint=" Type something here"
android:maxLines="2" />
<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="send"/> </LinearLayout></LinearLayout>
界面中放了一个RecyclerView用于显示聊天的消息内容
EditText用于输入消息
Button发送消息
3、定义消息实体类 新建Msg 代码如下:
public class Msg {
public static final int TYPE_RECEIVEO = 0;
public static final int TYPE_SENT = 1;
private String content;
private int type;
public Msg(String content, int type) {
this.content = content;
this.type = type;
}
public static int getTypeReceiveo() {
return TYPE_RECEIVEO;
}
public static int getTypeSent() {
return TYPE_SENT;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}}
msg类中两个字段,content表示消息内容,type表示消息类型。消息类型有两个值可选择,TYPE_RECEUVED表示收到的消息,TYPE_SENT表示发送出去的消息
4、编写RecyclerView子项布局,新建msg_item.xml :
<?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:padding="10dp">
<LinearLayout
android:id="@+id/left_layout"
android:layout_gravity="left"
android:background="@drawable/imag"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/left_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:textColor="#0b0505"/>
</LinearLayout>
<LinearLayout
android:id="@+id/right_layout"
android:layout_gravity="right"
android:background="@drawable/imagm"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/right_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:textColor="#f10808"/>
</LinearLayout></LinearLayout>
这里我们让收到消息居左对齐,发送消息居右对其
5、创建RecyclerView适配器类,新建MsgAdapter :
public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder> {
private List<Msg> mMsgList;
static class ViewHolder extends RecyclerView.ViewHolder {
LinearLayout leftLayout;
LinearLayout rightLayout;
TextView leftMsg;
TextView rightMsg;
public ViewHolder(View view) {
super(view);
leftLayout = (LinearLayout) view.findViewById(R.id.left_layout);
rightLayout = (LinearLayout) view.findViewById(R.id.right_layout);
leftMsg = (TextView) view.findViewById(R.id.left_msg);
rightMsg = (TextView) view.findViewById(R.id.right_msg);
}
}
public MsgAdapter(List<Msg> msgList) {
mMsgList = msgList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item, parent,false );
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Msg msg = mMsgList.get(position);
if (msg.getType() == Msg.TYPE_RECEIVEO) {
//如果是收到的消息 ,则显示左边的消息布局,将右边的消息布局隐藏
holder.leftLayout.setVisibility(View.VISIBLE);
holder.rightLayout.setVisibility(View.GONE);
holder.leftMsg.setText(msg.getContent());
} else if (msg.getType() == Msg.TYPE_SENT) {
//如果是发出的消息,则显示右边的消息不就,将左边的消息布局隐藏
holder.rightLayout.setVisibility(View.VISIBLE);
holder.leftLayout.setVisibility(View.GONE);
holder.rightMsg.setText(msg.getContent());
}
}
@Override
public int getItemCount() {
return mMsgList.size();
} }
在onBindViewHolder()方法中对消息类型判断,如果这条消息是收到的,则显示左边。如果这条消息是发出的,则显示在右边消息布局。
6 、最后 MainActivity 中的代码,来为RecyclerView初始化数据,并给发送按钮添加监听事件 :
public class MainActivity extends AppCompatActivity {
private List<Msg> msgList = new ArrayList<>();
private EditText inputText;
private Button send;
private RecyclerView msgRecyclerView;
private MsgAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initMsg();
//findViewById
inputText = (EditText) findViewById(R.id.input_text);
send = (Button) findViewById(R.id.send);
msgRecyclerView = (RecyclerView) findViewById(R.id.recycle_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
msgRecyclerView.setLayoutManager(layoutManager);
adapter = new MsgAdapter(msgList);
msgRecyclerView.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.TYPE_SENT);
msgList.add(msg);
//当有新消息是刷新ListView中的显示
adapter.notifyItemInserted(msgList.size() - 1);
//将ListView定位到最后一行
msgRecyclerView.scrollToPosition(msgList.size() - 1);
//清空输入框内容
inputText.setText("");
}
}
});
}
//初始化数据用于在RecyclerView中显示
private void initMsg() {
Msg msg1 = new Msg("Hello tututu.", Msg.TYPE_RECEIVEO);
msgList.add(msg1);
Msg msg2 = new Msg("What are you 弄啥嘞.", Msg.TYPE_SENT);
msgList.add(msg2);
Msg msg3 = new Msg("Guess what ??", Msg.TYPE_RECEIVEO);
msgList.add(msg3);
Msg msg4 = new Msg("Rats , screw you", Msg.TYPE_SENT);
msgList.add(msg4);
Msg msg5 = new Msg("FFFUK", Msg.TYPE_RECEIVEO);
msgList.add(msg5);
Msg msg6 = new Msg("1", Msg.TYPE_SENT);
msgList.add(msg6);
Msg msg7 = new Msg("2", Msg.TYPE_RECEIVEO);
msgList.add(msg7);
Msg msg8 = new Msg("3", Msg.TYPE_SENT);
msgList.add(msg8);
Msg msg9 = new Msg("QWER", Msg.TYPE_RECEIVEO);
msgList.add(msg9);
Msg msg10 = new Msg("DF", Msg.TYPE_SENT);
msgList.add(msg10);
Msg msg11 = new Msg("碧池", Msg.TYPE_RECEIVEO);
msgList.add(msg11);
} }
网友评论