制作Nine-Patch图片
将需要制作为Nine-Patch的图片放入存放图片的文件夹
然后对着图片右键Create 9- Patch file制作图片,可保存在drawalbe中,然后将制作的.9图片进行拉伸即可
activity_nine
<?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="#d8e0e8">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/msg_recyclerView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</androidx.recyclerview.widget.RecyclerView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/input"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="send"/>
</LinearLayout>
</LinearLayout>
msg_item
<?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">
<LinearLayout
android:layout_gravity="left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/left_layout"
android:background="@drawable/message_left"
android:orientation="vertical">
<TextView
android:id="@+id/left_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:layout_margin="10dp"
android:text="how are you?"/>
</LinearLayout>
<LinearLayout
android:layout_gravity="right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/right_layout"
android:background="@drawable/message_right"
android:orientation="vertical">
<TextView
android:id="@+id/right_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:layout_margin="10dp"
android:text="I'm fine thank you "/>
</LinearLayout>
</LinearLayout>
MsgAdapter
public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.MyViewHolder> {
private List<Msg> msgList;
public class MyViewHolder extends RecyclerView.ViewHolder{
LinearLayout leftLayout,rightLayout;
TextView leftMsg,rightMsg;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
leftLayout=itemView.findViewById(R.id.left_layout);
rightLayout=itemView.findViewById(R.id.right_layout);
leftMsg=itemView.findViewById(R.id.left_msg);
rightMsg=itemView.findViewById(R.id.right_msg);
}
}
public MsgAdapter(List<Msg> msgList) {
this.msgList = msgList;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item,parent,false);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
Msg msg = msgList.get(position);
if (msg.getType()==Msg.TYPE_RECEIVED){
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 msgList.size();
}
}
NineActivity
public class NineActivity extends AppCompatActivity {
private List<Msg>msgList=new ArrayList<>();
private EditText msg_input;
private Button btn_send;
private RecyclerView recyclerView;
private MsgAdapter msgAdapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nine);
initMsg();
initView();
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
msgAdapter = new MsgAdapter(msgList);
recyclerView.setAdapter(msgAdapter);
btn_send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String content = msg_input.getText().toString();
if (!"".equals(content)){
Msg msg = new Msg(content,Msg.TYPE_SENT);
msgList.add(msg);
msgAdapter.notifyItemInserted(msgList.size()-1);//当有新消息时,刷新RecyclerView中的显示
recyclerView.scrollToPosition(msgList.size()-1);//将RecyclerView定位到最后一行
msg_input.setText("");//清空输入框中内容
}
}
});
}
private void initView() {
recyclerView = findViewById(R.id.msg_recyclerView);
msg_input = findViewById(R.id.input);
btn_send = findViewById(R.id.send);
}
private void initMsg() {
Msg msg1 = new Msg("hello guy.",Msg.TYPE_RECEIVED);
msgList.add(msg1);
Msg msg2 = new Msg("hello,Who is that ?",Msg.TYPE_SENT);
msgList.add(msg2);
Msg msg3 = new Msg("This is Tom.Nice talking to you.",Msg.TYPE_SENT);
msgList.add(msg3);
}
}
代码解释 notifyItemInserted()
适配器的方法,用于通知列表有新的数据插入
而scrollToPosition()
方法用于将显示的数据定位到最后一行