Android Development Notes - RecyclerView


RecyclerView is a scroll control provided in support:recyclerview-v7. To use this control, you need to do the following:

  1. Introduce dependency Library
  2. Add RecyclerView label to layout file
  3. Create a single layout file
  4. Create adapter class
  5. Set up the layout manager and adapter for Recyclerview in the activity

Although it's not complicated, it's hard for me who has a bad memory to remember completely, so I wrote this note.


Reference articles

What you know about RecyclerView and what you don't know are all here (Part 1)

Since I used RecyclerView, my waist is no longer painful and my hands are no longer sour

Recycle view use full resolution

Android RecyclerView uses full parsing to experience artistic controls

Android control RecyclerView


I. introduce dependency Library

implementation ''


II. Add labels to layout files

            android:layout_height="match_parent" />

III. create single layout file

The layout file is the layout of each item in the scroll control, and the value of each item will be set in the ViewHolder subclass of the adapter


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""

        android:layout_height="wrap_content" />


IV. create adapter class

public class MyRecyclerViewAdapterextends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private List<String> list;
    public MyAdapter(List<String> list) {
        this.list = list;
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // Bind item layout
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_base_use, parent, false);
        MyAdapter.ViewHolder viewHolder = new MyAdapter.ViewHolder(view);
        // You can set event listening for each item here
        return viewHolder;
    public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) {
        // Set the value of the control
        String string = list.get(position);
    public int getItemCount() {
        // Returns the length of the list
        return list.size();
    class ViewHolder extends RecyclerView.ViewHolder {
        // Control object, corresponding to the control in item
        TextView mText;

        ViewHolder(View itemView) {
            // Bound controls
            mText = itemView.findViewById(;

5. Set the layout manager and adapter for Recyclerview in the activity

//Get the RecyclerView instance through findViewById
mRecyclerView = findViewById(;
//Set up RecyclerView Manager
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//Initialize adapter
mAdapter = new MyRecyclerViewAdapter(list); 
//Animate when adding or deleting item s, the default animation is used here
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
//Set adapter


Keywords: Mobile Android xml encoding

Added by sam_rich on Wed, 11 Dec 2019 19:38:04 +0200