Statement: original, reproduced please indicate the source: www.jianshu.com/p/5525310c0…

RecyclerView is used for list display control, for xiaobai, is more difficult to use, below, I will explain the most basic way to use it.

  • Steps:
  1. Add Recyclerview dependency.
  2. Add Recyclerview control to XML file.
  3. Create a Recyclerview Adapter.
  4. Set the adapter to Recyclerview.

Add Recyclerview dependency

  • Add the following code to dependencies{} in the build.gradle module:
implementation 'com. Android. Support: recyclerview - v7:28.0.0'
Copy the code

Two, add Recyclerview control in XML file

<android.support.v7.widget.RecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager"/>
Copy the code

Create Recyclerview Adapter (Adapter)

  • Steps:
  1. Create a subclass of RecyclerView.Adapter and pass the generic recyclerView. ViewHolder.
  2. Create a subclass of RecyclerView.ViewHolder.
  3. Initialize the item control in a subclass of RecyclerView.ViewHolder.
  4. Rewrite recyclerView. Adapter class related methods.
package com.liuym.myapplication;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class RvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    //数据源
    private List<Data> mList;

    public RvAdapter(List<Data> list) {
        mList = list;
    }

    /**
     * 创建ViewHolder
     *
     * @param viewGroup
     * @param i
     * @return
     */
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View item = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_rv, viewGroup, false);
        RecyclerView.ViewHolder holder = new MyViewHolder(item);
        returnholder; } /** * Use the ViewHolder to control the controls in the item (e.g. / @override public void onBindViewHolder(@nonnull recyclerView.viewholder viewHolder, int i) { MyViewHolder holder = (MyViewHolder) viewHolder; Data data = mList.get(i); holder.iv.setImageResource(R.drawable.ic_launcher_background); holder.tv.setText(data.getText()); } /** * returns the list length ** @return
     */
    @Override
    public int getItemCount() {
        returnmList == null ? 0 : mList.size(); } /** * Create a ViewHolder class to cache the child controls of item, FindViewById */ class MyViewHolder extends RecyclerView.ViewHolder {ImageView iv; TextView tv; public MyViewHolder(@NonNull View itemView) { super(itemView); iv = itemView.findViewById(R.id.iv); tv = itemView.findViewById(R.id.tv); }}}Copy the code

Four, set the adapter to Recyclerview

    private void initRv() {
        List<Data> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            String text = String.format("mockData: %1s", i);
            list.add(new Data(text));
        }
        RecyclerView recyclerView = findViewById(R.id.rv);
        RvAdapter adapter = new RvAdapter(list);
        recyclerView.setAdapter(adapter);
    }
Copy the code

conclusion

In this chapter, I explained the basic use of RecyclerView. In the next chapter, I will explain how to use different layouts in RecyclerView.

series

RecyclerView Use guide (a) – basic use “RecyclerView use guide (two) – a variety of ItemLayout” RecyclerView use guide (three) – add separate lines and click events” RecyclerView use guide (4) – use ItemDecoration “RecyclerView use guide (5) – achieve top suction effect”