1. Introduction

When the user slides the screen to switch views, the previous view will be recycled, RecyclerView is to recycle, recycle.

  • ViewHolder The main task of a ViewHolder is to hold the View. When you initialize the ViewHolder, when you initialize the ViewHolder, you put a bunch of little views inside it.

    public class ExpressInfoHolder extends RecyclerView.ViewHolder{ … . public ExpressInfoHolder(View itemView) { super(itemView); mGoodsImg = itemView.findViewById(R.id.aaa); mShippingNameTextView = itemView.findViewById(R.id.bbb); mShippingCodeTextView = itemView.findViewById(R.id.ccc); mCopyShippingCode = itemView.findViewById(R.id.ddd); shippingCodeDesc = ImString.get(R.string.eee); }… . public void bindData(){ … . }}

  • Adapter Adapter takes data from the model layer and provides it to RecyclerView for display. It is a bridge of communication. The Main tasks of the Adapter are to create a ViewHolder and bind model-layer data to the ViewHolder. BindData to a ViewHolder using bindData in the ViewHolder.

RecyclerView and Adapter interaction process

First, the adapter.getitemCount () method is called, and RecyclerView asks how many views are in the arraylist to display. Then, RecyclerView calling Adapter. OnCreateViewHolder create ViewHolder (ViewGroup, int). Finally, RecyclerView passes in the ViewHolder and its location, calling onBindViewHolder(ViewHolder, int). The Adapter finds the data for the destination location and binds it to the view of the ViewHolder. Note that the onCreateViewHolder() method is called infrequently compared to onBindViewHolder(). Once you have enough ViewHolder, RecyclerView stops calling onCreateViewHolder(). It then recycles the old ViewHolder to save time and memory.

  • LayoutManager RecyclerView does not personally place list items on the screen, and the task of placing list items is entrusted to LayoutManager. In addition to placing list items on the screen, LayoutManager is also responsible for defining screen scrolling behavior. In addition to some Android built-in implementations, LayoutManager has many third-party library implementations.

2.ViewHolder

The ViewHolder holds the view of each list item, so you need to initialize the ViewHolder before using RecyclerView.

private class CrimeHolder extends RecyclerView.ViewHolder { public CrimeHolder(LayoutInflater inflater, ViewGroup parent) { super(inflater.inflate(R.layout.list_item_crime, parent, false)); }}Copy the code

Notice that the CrimeHolder constructor method calls the superclass constructor super(View View)—-ViewHolder(View View). Thus, the CrimeHolder actually refers to the instantiated view of the LIST_ITEM_crime layout, which is also assigned to the parent variable itemView, which we can obtain in the itemView variable.

3.Adapter

Adapter is used when you need to display a newly created ViewHolder or associate data with an already created ViewHolder. There are usually three methods that need to be implemented in Adapter:

  • OnCreateViewHolder (ViewGroup parent, int viewType) When a new ViewHolder is needed to display a list item, the onCreateViewHolder method is called to create the ViewHolder.

    public CrimeHolder onCreateViewHolder(ViewGroup parent, int viewType) { LayoutInflater layoutInflater = LayoutInflater.from(getActivity()); return new CrimeHolder(layoutInflater, parent); }

  • OnBindViewHolder (CrimeHolder holder, int Position) binds data to the ViewHolder.

    public void onBindViewHolder(CrimeHolder holder, int position) { holder.bind(position); }

  • GetItemCount () returns the total number of lists to display (the number of Viewholders created is much smaller).

    public int getItemCount() { return list.size(); }