Android uses recyclerview to write chat interface
This example shares the specific code of Android recyclerview to write the chat interface for your reference. The specific contents are as follows
1. Recyclerview will be used later. First, add the dependency Library in APP / build.gradle (note that there are two build.gradle, select the one under APP), as follows:
After adding, remember to click sync now to synchronize.
2. Start to write the main interface and modify the activity_ The code in main.xml is as follows:
Recyclerview is used to display the chat message content (because it is not built into the system SDK, the complete package path needs to be written out);
Place an editview for inputting messages and a button for sending messages.
3. Define the entity class of the message, create a new MSG, and the code is as follows:
MSG has only two fields, content indicates the content of the message and type indicates the type of the message (two values are optional, one is type_recovered and the other is type_sent).
4. Then write the layout of recyclerview sub items and create a new MSG_ Item.xml, the code is as follows:
The received message is left aligned, the sent message is right aligned, and the corresponding picture is used as the background.
5. Create the adapter class of recyclerview and create a new msgadapter. The code is as follows:
6. Finally, modify the code in mainactivity to initialize some data for recyclerview and add an event response to the send button. The code is as follows:
Run the program, and the effect is as follows:
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.