RecyclerView for Android development and Android recyclerview

Source: Internet
Author: User

RecyclerView for Android development and Android recyclerview

RecyclerView is a very useful control. Its effect is similar to that of ListView. It can be said that the appearance of RecyclerView replaces ListView's

RecyclerView is more flexible and scalable than ListView.

The following describes the usage of RecyclerView in detail.

Since the RecyclerView is used with ListView, it indicates that the RecyclerView and ListView are similar in usage. In fact, there are two things: a control and an Adapter)

I. Import package

I am developing and using AndroidStudio here. Before using RecyclerView, I need to introduce the RecyclerView package (this is mandatory and does not need to be explained)

Find the dependencies code block in the build file, add the compile 'com. android. support: recyclerview-v7: 23.0.0 'statement, and rebuild the project to use RecyclerView.

dependencies {    compile fileTree(include: ['*.jar'], dir: 'libs')    compile 'com.android.support:recyclerview-v7:23.0.0'}
Ii. write xml files

After completing the above work, you can start to write code. First, create an Activity and generate an xml layout file, and write the RecyclerView control to the layout file.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <android.support.v7.widget.RecyclerView        android:id="@+id/recyclerView"        android:layout_width="match_parent"        android:layout_height="match_parent">    </android.support.v7.widget.RecyclerView></RelativeLayout>

When the above Code is RecyclerView, you cannot directly write RecyclerView. You must add the previous android. support. v7.widget. If you cannot remember the previous content, we can provide you with a simple method to declare a variable RecyclerView in the Activity.

 

We can see that after the RecyclerView is declared, the above import will be automatically generated. The content after the import is the full path of the RecyclerView.

Iii. Write adapter class

First, create a class inherited from RecyclerView. Adapter.

I believe that ViewHolder is already familiar with the ViewHolder class in ListView. ViewHolder is built in the RecyclerView Adapter. Therefore, do not worry about anything. You must first write a ViewHolder internal class inherited from RecyclerView. viewHolder: rewrite the constructor. Do not rush to implement the abstract method of the Adapter.

public class MyRecyclerViewAdapter extends RecyclerView.Adapter {        class MyViewHolder extends RecyclerView.ViewHolder{        public MyViewHolder(View itemView) {            super(itemView);        }    }}

After completing the above steps, we will add the ViewHolder we wrote to the Adapter, which is the content of the angle brackets behind RecyclerView. Adapter.

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.MyViewHolder> {    class MyViewHolder extends RecyclerView.ViewHolder{        public MyViewHolder(View itemView) {            super(itemView);        }    }}

The above steps last night are used to implement the abstract method of the Adapter.

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {    @Override    public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {        return null;    }    @Override    public void onBindViewHolder(MyViewHolder myViewHolder, int i) {    }    @Override    public int getItemCount() {        return 0;    }    class MyViewHolder extends RecyclerView.ViewHolder{        public MyViewHolder(View itemView) {            super(itemView);        }    }}

If you implement the above abstract method first, it will be very troublesome if you do not do the above steps first, and you may even be confused.

In this way, the Adapter class is basically formed. The following describes the functions of these methods.

First, the onCreateViewHolder () method is used to create the ViewHolder class. Although the ViewHolder class is written above, the instance has not yet been generated, that is, the new process. Therefore, this method is used to create a new ViewHolder. As you can see, myViewHolder has a constructor that contains a View object, therefore, when creating ViewHolder, You need to input a parameter. The View here is very simple, that is, the View object of each item of your RecyclerView. Load it with LayoutInflater and return ViewHolder.

Next, onBinderViewHolder (). There are two parameters: MyViewHolder myViewHolder. Your ViewHolder instance. int I represents the current row. Therefore, this method is used to assign values to controls in ViewHolder.

Then, let's look at getItemCount (); that is, determine the number of rows in your RecyclerView.

MyViewHolder is the place where space of each row is initialized

The above method is implemented to form the following Adapter class

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {    private List<String> mList;    private Context context;    public MyAdapter(List<String> mList,Context context){        this.mList = mList;        this.context = context;    }    @Override    public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {        View view = LayoutInflater.from(context).inflate(R.layout.item,viewGroup,false);        MyViewHolder viewHolder = new MyViewHolder(view);        return viewHolder;    }    @Override    public void onBindViewHolder(MyViewHolder myViewHolder, int i) {        myViewHolder.tv_msg.setText(mList.get(i));    }    @Override    public int getItemCount() {        return mList.size();    }    class MyViewHolder extends RecyclerView.ViewHolder{        TextView tv_msg;        public MyViewHolder(View itemView) {            super(itemView);            tv_msg = (TextView) itemView.findViewById(R.id.tv_msg);        }    }}  
4. Content in the Activity

The content in the Activity is very simple, that is, setAdapter. It is worth mentioning that in addition to setting the Adapter, there is a LayoutManager in the RecyclerView, which is also the main reason for the high scalability of RecyclerView.

The Code is as follows:

public class MainActivity extends Activity {    private RecyclerView recyclerView;    private List<String> mList ;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();        function();    }    private void function() {        mList = new ArrayList<>();        for (int i = 0 ; i < 20 ; i++){            mList.add("Hello"+i);        }        MyAdapter adapter = new MyAdapter(mList,this);        LinearLayoutManager llm = new LinearLayoutManager(this,0,false);        recyclerView.setLayoutManager(llm);        recyclerView.setAdapter(adapter);    }    private void initView() {        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);    }}

First, find the RecyclerView by using findViewById.

Then initialize List

Then generate the Adapter instance

Next, the LayoutManager object is generated.

Finally, set LayoutManager and Adapter.

V. Final

LayoutManager

There are three LayoutManager types in RecyclerView, which are commonly used. Here we will introduce the two commonly used layoutmanagers.

First LinearLayoutManager

At this point, we can see the linear layout management. The constructor has three parameters: the first context object (not described), and the second is an int-type parameter (only 0 and 1) 0 indicates the horizontal direction, 1 indicates the vertical direction, and the third parameter Boolean indicates the order or reverse order.

"False" indicates the order, and "true" indicates the reverse order. The layout manager has the same effect as the ListView. Only one item can be placed in one row.

GridLayoutManager

The grid layout manager can be used to put multiple items in a row. It has two parameters. The first context is object-oriented, the second int type indicates the number of items in a row (1: one item in a row; 2: two items in a row)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.