Since Android 5.0, Google launched a RecyclerView control, he is a new component in support-V7 package, is a powerful sliding component, compared with the classic ListView, also has the function of item recycling. RecyclerView is an upgrade of ListView.

RecyclerView encapsulated the recycling of ViewHolder, that is to say, RecyclerView standardized ViewHolder, write Adapter oriented to ViewHolder rather than View, reuse logic was encapsulated, write more simple.

RecyclerView provides a plug and pull type experience, high decoupling, abnormal flexibility, for an Item display RecyclerView specifically extracts the corresponding class, to control the display of Item, so that its scalability is particularly strong.

The introduction of RecyclerVIew

compile 'com. Android. Support: recyclerview - v7:25.1.0'
Copy the code

Creating a layout file

Master layout file
/*activity_main.xml*/
<?xml version="1.0" encoding="utf-8"? >
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.manu.mrecyclerview.MainActivity">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v7.widget.RecyclerView>
</LinearLayout>

Copy the code
Item layout file
/*item.xml*/
<?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"
    android:padding="5dp">
    <TextView
        android:id="@+id/tv_recycle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="data"
        android:background="#cac3c3"
        android:padding="10dp"
        android:textSize="20sp"/>
</LinearLayout>

Copy the code

Create the Adapter

The Adapter of RecyclerView is a little more complex than the Adapter of ListView, which is also a reflection of the high decoupling of RecyclerView. Although the code is a little more complex, it has good scalability. Here are three ways to implement RecyclerView Adapter:

onCreateViewHolder()

This method basically loads a View for each Item, but it returns a ViewHolder, which is the ViewHolder that the View encapsulates directly, and then we’re going to use the instance ViewHolder, which is also written by ourselves, But instead of calling convertView.settag (vh) and convertView.getTag() as ListView does.

onBindViewHolder()

This method is primarily used to bind data to the View, providing a ViewHolder directly rather than a convertView.

getItemCount()

This method returns the total number of options.

The Adapter code
/** * Created by jzman on 2017/5/13 0013. */
public class RvAdapter extends RecyclerView.Adapter<RvAdapter.DataViewHolder>{
    private Context mContext;
    private ArrayList<String> mList;

    public RvAdapter(a) {}

    public RvAdapter(Context mContext, ArrayList<String> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    // Used to create ViewHolder
    @Override
    public DataViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item,null);
        // Use code to set width and height (when XML layout Settings are invalid)
        view.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        DataViewHolder holder = new DataViewHolder(view);
        return holder;
    }
    // Bind data
    @Override
    public void onBindViewHolder(DataViewHolder holder, int position) {
        holder.tv_data.setText(mList.get(position));
    }
    // Total number of data
    @Override
    public int getItemCount(a) {
        return mList.size();
    }

    / / create the ViewHolder
    public static class DataViewHolder extends RecyclerView.ViewHolder{
        TextView tv_data;
        public DataViewHolder(View itemView) {
            super(itemView); tv_data = (TextView) itemView.findViewById(R.id.tv_recycle); }}Copy the code

Using StaggeredGridLayoutManager manager, Adapter reference is as follows:

/** * Created by jzman on 2017/5/130013. * RecycleView Adapter */
public class RvAdapter extends RecyclerView.Adapter<RvAdapter.DataViewHolder>{
    private Context mContext;
    private RecyclerView recyclerView;
    private ArrayList<String> mList;
    private ArrayList<Integer> mHeight;

    public RvAdapter(a) {}

    public RvAdapter(Context mContext, ArrayList<String> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    /** * Initializes the height of each Item (waterfall effect) *@return* /
    public ArrayList<Integer> initHeight(a){
        mHeight = new ArrayList<>();
        for (int i=0; i<mList.size(); i++){ mHeight.add((int) (Math.random()*300) +200);
        }
        return mHeight;
    }

    /** * create ViewHolder *@param parent
     * @param viewType
     * @return* /
    @Override
    public DataViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item,null);
        // Use code to set width and height (when XML layout Settings are invalid)
        view.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        DataViewHolder holder = new DataViewHolder(view);
        return holder;
    }

    /** * bind data *@param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(DataViewHolder holder, int position) {
        // Set the height of each Item
        ViewGroup.LayoutParams h = holder.tv_data.getLayoutParams();
        h.height = mHeight.get(position);
        holder.tv_data.setText(mList.get(position));
    }

    /** * Total number of options *@return* /
    @Override
    public int getItemCount(a) {
        return mList.size();
    }

    /** * Create ViewHolder */
    public static class DataViewHolder extends RecyclerView.ViewHolder{
        TextView tv_data;
        public DataViewHolder(View itemView) {
            super(itemView); tv_data = (TextView) itemView.findViewById(R.id.tv_recycle); }}/** * Add RecycleView to Adapter */
    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.recyclerView= recyclerView;
        // Initialize the height of each Item
        initHeight();
    }
    /** * Remove RecycleView from Adapter */
    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        this.recyclerView = null; }}Copy the code

MainActivity

/** * Created by jzman on 2017/5/13 0013. */
public class MainActivity extends AppCompatActivity {
    private RecyclerView rv;
    RvAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rv = (RecyclerView) findViewById(R.id.rv);
        // Set the layout manager
        rv.setLayoutManager(new LinearLayoutManager(this));/ / linear
// rv.setLayoutManager(new GridLayoutManager(this,4)); / / linear
// rv.setLayoutManager(new StaggeredGridLayoutManager(4,StaggeredGridLayoutManager.VERTICAL)); / / linear
        adapter = new RvAdapter(this,initData());
        rv.setAdapter(adapter);
    }
    public static ArrayList<String> initData(a){
        ArrayList<String> arrayList = new ArrayList<>();
        for (int i=0; i<50; i++){ arrayList.add("The first"+i+"Piece of data");
        }
        returnarrayList; }}Copy the code

According to the effect

LinearLayoutManager GridLayoutManager StaggeredGridLayoutManager

If you think it is helpful for you, you can follow the public account jzman-blog to communicate and learn together.