1. Main functions:
①Realization of chat interface
② Status bar immersion
③ The soft keyboard pops up automatically
④ ScrollView automatically scrolls after sending a message, and makes EditText get the focus again
2. On the previous interface, see if it meets your needs. If it matches, continue to look down
3. The main interface is implemented in MainActivity
//onCreate
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//Immersive status bar implementation
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);//Automatically recalculate the layout after the soft keyboard pops up
scrollView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {//Registration event for scrollview registration layout change
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
if (mHandler != null) {
mHandler.post(new Runnable() {
@Override
public void run() {
scrollView.fullScroll(FOCUS_DOWN);//scrollView automatically slide to the bottom
msg.setFocusable(true);//The next thing is to return focus to EditText
msg.setFocusableInTouchMode(true);
msg.requestFocus();
msg.findFocus();
}
});
}
}
});
4. Realization of immersive status bar
The root layout is focused on fitsSysytemWindows="true"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/main_color"
android:fitsSystemWindows="true">
5. Classification of chat content
private class ChatMsgAdapter extends RecyclerView.Adapter<ChatMsgAdapter.ViewHolder> {
private ArrayList<MsgChatBody> list;
public ChatMsgAdapter(ArrayList<MsgChatBody> list) {
this.list = list;
}
class ViewHolder extends RecyclerView.ViewHolder {
public View view;
public TextView chatMsgLeft;
public TextView chatMsgRight;
public ViewHolder(View itemView) {
super(itemView);
view = itemView;
chatMsgLeft = view.findViewById(R.id.chat_msg_left);//The layout on the left
chatMsgRight = view.findViewById(R.id.chat_msg_right);//right layout
}
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_msg_content,
parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
MsgChatBody body = list.get(position);
if (body.type == CHAT_MSG_TYPE_LEFT) {//Judging by the type field, which layout is displayed
holder.chatMsgRight.setVisibility(View.GONE);
holder.chatMsgLeft.setVisibility(VISIBLE);
holder.chatMsgLeft.setText(body.msg);
} else if (body.type == CHAT_MSG_TYPE_RIGHT) {
holder.chatMsgLeft.setVisibility(View.GONE);
holder.chatMsgRight.setVisibility(VISIBLE);
holder.chatMsgRight.setText(body.msg);
}
}
@Override
public int getItemCount() {
return list.size();
}
}
6.github address
Decree of the basic operation of Android Bluetooth Communication / Bluetooth chat This series of articles mainly introduce Android devices based on Bluetooth communication, an...
Android Bluetooth Communication / Bluetooth Chat Chat Communication This series of articles main introductionAndroidThe device is real-time chat based on Bluetooth communication, and the article serie...
Achieve results socket chat room github demo What is SWOOLE Official website says: Swoole: PHP asynchronous network communication engine for the production environment The PHP developers to write high...
Websocket very hot nowadays, it isA new TCP-based network protocol. It implements the browser and server full-duplex (full-duplex) communication - allows the server actively sends information to the c...
...
1. WebSocket definition The WebSocket protocol is a new network protocol based on TCP. It implements full-duplex communication between the browser and the server-allowing the server to actively send i...
Server startup and configuration Server processor Client startup and configuration Client processor ...
Client mainwindow.h main.cpp mainwindow.cpp Server mainwindow.h main.cpp mainwindow.cpp interface...
Client mainwindow.h main.cpp mainwindow.cpp Server widget.h main.cpp widget.cpp interface...
First contact network programming. A Netty-based WebSocket chat room Demo is done. (It is really just a demo, it may not be possible at all). Record. Service class GroupChatService.java enumerate MSGT...