“This is the fifth day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

preface

Today is the fifth day of learning Android. Yesterday, we combined previous knowledge and completed the login interface of imitation QQ, which was very satisfying. Today we will learn a new control RecyclerView

Let’s start with a case study

You can see that there are a lot of little modules in the same format, and we can’t just write tens or hundreds of TextViews and ImageView, in fact, The Android official has provided ListView, but RecyclerView compared to ListView has stronger scalability and performance, we directly ignore ListView.

As always, first we declare the control in XML

<? The XML version = "1.0" encoding = "utf-8"? > <androidx.constraintlayout.widget.ConstraintLayout 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=".RecyclerViewActivity"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/rv" android:layout_width="match_parent" android:layout_height="wrap_content" /> </androidx.constraintlayout.widget.ConstraintLayout>Copy the code

Explain layout, ConstraintLayout, ConstraintLayout, ConstraintLayout, ConstraintLayout, ConstraintLayout

And then we define an adapter, our RecyclerView is not human and we don’t infer what you want, we define an adapter to determine our requirements, okay

public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.ViewHolder> { private final Context context; private final List<String> list; Public MessageAdapter(Context Context, List<String> List) {// Context this. Context = Context; // Our data set this.list = list; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, View root = Layoutinflater.from (context).inflate(r.layout.item, parent, false); return new ViewHolder(root); } @override public int getItemCount() {return list.size(); } @Override public void onBindViewHolder(ViewHolder holder, Int position) {// Set the data holder.mitemtv.settext (list.get(position)); } // Describes an item view and its metadata about its location in RecyclerView. public class ViewHolder extends RecyclerView.ViewHolder { private final TextView mItemTv; public ViewHolder(View itemView) { super(itemView); MItemTv = ItemView.findViewById (R.i.tem_tv); }}}Copy the code

Activity

mRv = findViewById(R.id.rv); // Set the layout manager mrv.setLayoutManager (new LinearLayoutManager(this)); List< string > List = new ArrayList<>(); for (int i = 0; i < 50; I ++) {list.add(" item "+ I); MessageAdapter = new MessageAdapter(this, list); // Set the adapter mrv. setAdapter(messageAdapter);Copy the code

There are many different layout managers, so we’ll use a LinearLayoutManager to see the effect

Sometimes we have horizontal, we just need to add two parameters

mRv.setLayoutManager(new LinearLayoutManager(this,RecyclerView.HORIZONTAL,false));
Copy the code

See the effect

There seems to be something wrong, right? We still need to modify our item

<? The XML version = "1.0" encoding = "utf-8"? > <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" Android :layout_width="wrap_content" Android :layout_height="wrap_content" android:layout_height="wrap_content" xmlns:app="http://schemas.android.com/apk/res-auto"> <TextView android:id="@+id/item_tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/teal_200" App :layout_constraintRight_toRightOf="parent" android: layout_constraintRight_toRightOf=" 15dp" android: layout_torightRight ="15dp" Android :gravity=" gravity "android:gravity=" gravity" android:gravity=" gravity "android:gravity=" gravity" Android :padding=" gravity "android:gravity=" gravity" android:padding=" gravity "android:padding=" gravity" android:padding=" gravity "android:padding=" gravity" android:padding=" gravity "android:padding=" gravity" android:padding=" gravity "android:padding=" gravity" Because set the android: text = "99999" android: textSize = "20 dp" / > < / androidx. Constraintlayout. Widget. Constraintlayout >Copy the code

Look at the effect

In order to make the difference more obvious, I added the background…

Ok, you're done!!