Introduction to RecycleView Xiaobai (teaching you to fully grasp its usage)

RecycleView concept

RecyclerView is a new UI control proposed by Android 5.0. It is located in the support-v7 package and is downward compatible to the android 3.0 version. It can replace ListView and GridView in many list scenarios.

RecycleView can flexibly realize the display of big data. The reuse management of views is better than ListView. It can display lists, grids, waterfalls and other forms, and different ViewHolders can realize the diversified functions of item

Show results

Insert picture description here

RecycleView actual combat

(1) For RecycleView, the first step we need to import RecycleView dependencies

    implementation 'androidx.recyclerview:recyclerview:1.1.0'

Of course, we can also download RecycleView in xml, as shown in the figure, click RecycleView to download automatically.
Insert picture description here

(2) After we download RecycleView, we can use its layout in our layout file, as shown in the figure

Insert picture description here
code show as below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".HorRecycleActivity">
<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/hor_recycle"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></androidx.recyclerview.widget.RecyclerView>
</LinearLayout>

(3) Get the above recycleView control in our java code, and set the linear layout display for this recycleView control.

   //获取RecyclerView控件
        hor_recycle=findViewById(R.id.hor_recycle);
        //为线性布局
        hor_recycle.setLayoutManager(new LinearLayoutManager(HorRecycleActivity.this));

(4) Set the adapter for recycleView

 //为其设置适配器
        hor_recycle.setAdapter(new HorAdapter(HorRecycleActivity.this));

(5) We did not set the HorAdapter mentioned above, so we need to define certain

The HorAdapter class, and let it inherit RecyclerView.Adapter, the specific operation is as follows
Insert picture description here
When we create it, we should see this

public class HorAdapter extends RecyclerView.Adapter {
    
    
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    
    
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
    
    

    }

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

So the important step is here.
We put the mouse on the Adapter and ctrl+left-click the mouse to enter the Adapter source code.
Insert picture description here
After entering we see the source code using generics Adapter
Adapter <extends ViewHolder>
Insert picture description here
Therefore, we need to write HorAdapter we just have to let the class have generics ViewHolder
, here we use a internal class MyViewHolder to inherit ViewHolder
do too I'm worried that you can't write yet, I will put the complete code at the end of the article later

    //需要定义一个内部类继承ViewHolder
    class  MyViewHolder extends RecyclerView.ViewHolder {
    
    
private TextView textView;
        public MyViewHolder(@NonNull View itemView) {
    
    
            super(itemView);
            //注意这里是通过itemView来找,而不是R,因为我们的textView是在itemView里面的
            textView=itemView.findViewById(R.id.textView);
        }
    }

Let's not care about the textview in the MyViewHolder construction method, we will introduce it later.
When we finish writing MyViewHolder, we can add a generic type to the HorAdapter class

public class HorAdapter extends RecyclerView.Adapter<HorAdapter.MyViewHolder> 

At the same time, we can see that of the three methods that MyViewHolder inherits from RecyclerView.Adapter, two of them are ViewHolder. We need to change the ViewHolder of the arrow part to the internal class MyViewHolder we just wrote (if you don’t understand the principle, then you write RecycleView can be written in this way)
Insert picture description here

It should be like this after the change

public class HorAdapter extends RecyclerView.Adapter<HorAdapter.MyViewHolder> {
    
    


    @NonNull
    @Override
    public HorAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    
    
       
            return null;
    }
//展示数据
    @Override
    public void onBindViewHolder(@NonNull HorAdapter.MyViewHolder holder, int position) {
    
    

    }

    @Override
    public int getItemCount() {
    
    
        //需要展示的个数
        return 0;
    }
    //需要定义一个内部类继承ViewHolder
    class  MyViewHolder extends RecyclerView.ViewHolder {
    
    
private TextView textView;
        public MyViewHolder(@NonNull View itemView) {
    
    
            super(itemView);
            //注意这里是通过itemView来找,而不是R,因为我们的textView是在itemView里面的
            textView=itemView.findViewById(R.id.textView);
        }
    }

}

ok, we mentioned earlier that there is textView in MyViewHolder, so where does this come from? We know that RecycleView is used to display data, so should each piece of data have a layout, and then RecycleView finally summarizes each layout and displays it on an interface? So we need to create a separate layout to display the data

(6) Create the layout hor_item for each item of data (the layout here is only one TextView)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="439dp"
        android:layout_height="match_parent"
        android:background="#CCC5C5"
        android:gravity="center"
        android:text="TextView"
        android:textSize="20sp" />
</LinearLayout>

Okay, so we can explain the textview in MyViewHolder. Here we use
itemView.findViewById(R.id.textView); to find the control in hor_item and prepare for adding values ​​to the control later .

//需要定义一个内部类继承ViewHolder
    class  MyViewHolder extends RecyclerView.ViewHolder {
    
    
private TextView textView;
        public MyViewHolder(@NonNull View itemView) {
    
    
            super(itemView);
            //注意这里是通过itemView来找,而不是R,因为我们的textView是在itemView里面的
            textView=itemView.findViewById(R.id.textView);
        }
    }

}

(7) Rewrite the parent class method of Adapter:


public class HorAdapter extends RecyclerView.Adapter<HorAdapter.MyViewHolder> {
    
    
    private Context mcontext;
    //1.构造方法
    public HorAdapter( Context context) {
    
    
        this.mcontext=context;
    }

    @NonNull
    @Override
    public HorAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    
    
        //传入一个item布局
            return new MyViewHolder(LayoutInflater.from(mcontext).inflate(R.layout.hor_item,parent,false));
    }
//展示数据
    @Override
    public void onBindViewHolder(@NonNull HorAdapter.MyViewHolder holder, int position) {
    
    
holder.textView.setText("Hello ,Android");
    }

    @Override
    public int getItemCount() {
    
    
        //需要展示的个数
        return 30;
    }
    //需要定义一个内部类继承ViewHolder
    class  MyViewHolder extends RecyclerView.ViewHolder {
    
    
private TextView textView;
        public MyViewHolder(@NonNull View itemView) {
    
    
            super(itemView);
            //注意这里是通过itemView来找,而不是R,因为我们的textView是在itemView里面的
            textView=itemView.findViewById(R.id.textView);
        }
    }

}

Here I give the complete code of HorAdapter
1. Get the context through the construction method
2. In our onCreateViewHolder method
new MyViewHolder(LayoutInflater.from(mcontext).inflate(R.layout.hor_item,parent,false));
where (R.layout.hor_item is the layout of each item.
3. In the onBindViewHolder method, it is item the set value of the control holder.textView.setText("Hello ,Android");
4. set the number of item you need to show in getItemCount method.

At this point our adapter class is over

(8) Finally, we can set the adapter for the RecyclerView in HorRecycleActivity

  hor_recycle.setAdapter(new HorAdapter(HorRecycleActivity.this));

Code reference address

Source code cloud link

Follow-up

Do you think you have mastered the recycleview, no, you haven't mastered it yet, we set the click event for it, is it only the display effect like our initial interface? Is there anything else? These answers are yes! Have! There must be, so let's introduce it in a later article, let's talk about it here first

Technical chat

If you are interested, you can join the QQ group to discuss technology
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_45353823/article/details/107723612