SoFunction
Updated on 2025-04-09

Detailed explanation of how to use Android RecyclerView

This article shares the usage method of Android RecyclerView for your reference. The specific content is as follows

1. RecyclerView is a new list component provided in Android support - v7 to replace the traditional ListView.

. To use RecyclerView, you need to add support:recycle-v7 to me project: right-click on the app - Open Module Settings - Dependencies - Point + sign - Add a library upport:recycle-v7 - Code implementation

. Render data to TextView

public class MainActivity extends AppCompatActivity {
  //Create RecyclerView  private RecyclerView recyclerView;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);

    recyclerView = new RecyclerView(this);
    //Take it as the content layout of the activity    setContentView(recyclerView);

    //Set the layout of RecyclerView    (newLinearLayoutManager(this));

    //Fill content for RecyclerView - Create an Adapter - Three functions need to be rewrited    (() {

      //First customize a class inheritance and implement the constructor      class ViewHolerextends {

        // Views that bind sub-objects in ViewHolder        private TextViewtv;

        public ViewHoler(TextView itemView) {
          super(itemView);
          tv = itemView;//In this way, TextView can be associated with ViewHolder        }

        //Expose a function getTV() in the outside world and use it to return this TextView        public TextView getTV(){
          return tv;
        }
      }
      // Method to create ViewHolder      @Override
      public (ViewGroup parent, intviewType) {

        return new ViewHoler(newTextView(()));
      }
      //onBindViewHolder can assign values ​​to TextView - configure it in the outside world      @Override
      public void onBindViewHolder( holder, intposition) {
        ViewHoler vh = (ViewHoler) holder;
        ().setText("Item " + position);
      }
      //Get the number of RecyclerView subobjects      @Override
      public int getItemCount() {
        return 1000;
      }
    });
  }
}

. Use array to host and display data

@Override
public void onBindViewHolder( holder, intposition) {
ViewHoler vh = (ViewHoler) holder;
().setText(data[position]);
}

    @Override
    public int getItemCount() {
      return ;
    }

    /*Write an array to present the data obtained from a series of channels in the network and are all presented in the array*/
    private String[] data = new String[]{"hello","wang","xiaobao"};
  });
}

2. Use resource files to customize list items

. Because we used the custom list item to write the program directly, in many cases you will find that if you write the interface directly in the program, the final modification will be very troublesome, so we need to learn to use resource files to configure.

. Create a new resource file  layout - New - Layout resource file - list_cell - Add two TextViews

Then

class MyAdapter extends  {

  /* new () can be transferred to a separate file
    * Inside Adapter(): Right-click - Refactor (refactor) - Move - Move to a class.
    * It will automatically extract the anonymous class into an internal class.
    *
    * Then continue to move to a separate file.
    */
  class ViewHolerextends  {
    private Viewroot; //No sense yet. Referring to the above case, you can connect with the outside world    private TextView tvTitle,tvContent;

    public ViewHoler(View root) {
      super(root);
      //We know that the layout passed in is list_cell; after creation, these two controls can be obtained.      tvTitle = (.tv_title);
      tvContent = (.tv_content);
    }
    // Two get methods need to be added to be accessed by the outside world    public TextViewgetTvTitle() {
      return tvTitle;
    }

    public TextViewgetTvContent() {
      return tvContent;
    }
  }

  @Override
  public (ViewGroup parent, intviewType) {
    /*
     *There is a view to create, not new TextView(). We need to create it in a different way based on a resource and use
     * LayoutInflater: Layout interpreter, use the layout interpreter to parse a layout. The first thing that comes to the layout is a resource, and the resource is the established cell
     * The second item: is the root object of the layout we created. Here we pass null. In this way, we create this layout.  Then get the control
     */
    return new ViewHoler((()).inflate(.list_cell,null));
  }

  @Override//Configure it separately // Let's create a data object.  Create a constructor  public void onBindViewHolder( holder, intposition) {
    ViewHoler vh = (ViewHoler) holder;
    //First get these data    Cell_Data cd = data[position];
    ().setText();
    ().setText();
  }

  //After creating the object, it is a CellData[]  private Cell_Data[]data = new Cell_Data[]{newCell_Data("Wang Xiaobao","hhh"),newCell_Data("news","This news is good")};

  @Override
  public int getItemCount() {
    return ;
  }
}

//Create a data object// List item datapublic class Cell_Data {

  public Cell_Data(String title,String content) {
    = title;
     = content;
  }

  public String title = "title";
  public String content = "content";
}

3. RecyclerView layout style

protected void onCreate(Bundle savedInstanceState) {
  (savedInstanceState);
  recyclerView = new RecyclerView(this);
  setContentView(recyclerView);
  //true; whether to flip, you can drag left and right - linear layout  (newLinearLayoutManager(,,true));
  //Create a table layout that is vertical by default and can be up and down  (newGridLayoutManager(this,3));

  (newMyAdapter());

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.