Then, we create a layout xml file used for each message in the list. The views you declare here will end up in each row of the list, and you will be populate each row with the right data. We place this file in
res/layout/item_message.xml.
For displaying data in lists, we use what is called a RecyclerView. It uses an Adapter to control the display of each item in the list by asking you to first create a view for each position, and then define a way to update these views for the data at each position. We use a helper object called a ViewHolder that stores references to all the views we need for each row, e.g. a TextView for displaying an item name and a button for taking some action. We create this object in onCreateViewHolder using a LayoutInflater to create the item layout, and then findViewById to obtain references to the subviews. In onBindViewHolder we use the ViewHolder to get the stored TextView for this particular row and update the text displayed to the message text for this position in the list.
The MessagesViewHolder caches the view elements for quicker access. The MessagesAdapter provides access to the data set, as well as provides the correct view elements. In our example this is seen by the itemMessage.findViewById() calls.
We also need a LayoutManager, for the layout of child views. To display a list, the LinearLayoutManager is the chosen layout manager. It can display items either vertically or horizontally.
In the activity where we want to display a list, we get the RecyclerView from the activity layout using the id R.id.messageList and set it to use our adapter and a LayoutManager.