Android tutorial 2020 – getting started with recyclerview

This article is a link to this article to introduce you to the use of recyclerview. Here is a common use.

Android tutorial 2020 - series overview

If you want to read, your friends are no longer unfamiliar with the form of list. There are contact list, file list, SMS list and so on. This article describes how to use recyclerview to achieve list effect in Android development.

Add a reference in the build.gradle file of the app. We use the Android x package.

dependencies {
    // ...
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
}

First determine what data to display. Is user information, contacts, or files. Here, take characters as an example. Before writing code, we first consider the requirements, that is, how to display and how to display data. In daily work, there are UI renderings. The art design in this paper is developed by ourselves.

For example, display a 97.

Now the data to be displayed has been determined. To design UI presentation. Layout is closely related to viewholder. It is better to write the viewholder class before designing the adapter class.

Create a new layout file item that defines item (list sub item)_ letter.xml。

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

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000" />

</LinearLayout>

Here, the internal class is used, and the viewholder class is written in the activity class.

    private class VH extends RecyclerView.ViewHolder {
        TextView tv1;
        TextView tv2;

        public VH(@NonNull View itemView) {
            super(itemView);
            tv1 = itemView.findViewById(R.id.tv1);
            tv2 = itemView.findViewById(R.id.tv2);
        }
    }

As can be seen from the above, the layout of viewholder and item are closely related. The ID in layout is relatively simple. Some more meaningful IDS can be named in actual projects.

In the layout of the activity, add recyclerview.

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/re_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

Design an adapter that inherits from recyclerview. Adapter < VH >. The VH here is the viewholder we wrote above. The letteradapter holds its own list of data. Three methods need to be implemented.

Oncreateviewholder method, which requires a VH object to be returned. Here is to create a VH object and return it. The VH constructor requires a view to be passed in. We create a view for it using layoutinflator. Of course, the creation is based on the previously designed item_ letter。

Onbindviewholder is to give the data to the corresponding VH for display.

The getitemcount method requires the number of returned data.

    private class LetterAdapter extends RecyclerView.Adapter<VH> {

        private List<Character> dataList;

        public LetterAdapter(List<Character> dataList) {
            this.dataList = dataList;
        }

        @NonNull
        @Override
        public VH onCreateViewHolder(@NonNull ViewGroup parent,int viewType) {
            return new VH(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_letter,parent,false));
        }

        @Override
        public void onBindViewHolder(@NonNull VH holder,int position) {
            Character c = dataList.get(position);
            holder.tv1.setText(c.toString());
            holder.tv2.setText(String.valueOf(Integer.valueOf(c)));
        }

        @Override
        public int getItemCount() {
            return dataList.size();
        }
    }

If you are careless, you may forget to initialize the datalist in the adapter. A null pointer exception is reported.

It is initialized in the oncreate method of the activity. Recyclerview requires 2 settings, adapter and layoutmanager. The adapter is the one we ordered above. Layoutmanager here uses linear layoutmanager to specify the vertical direction, so that we will get a sliding list up and down.

        List<Character> characterList = new ArrayList<>();
        for (char c = 'a'; c <= 'z'; c++) {
            characterList.add(c);
        }

        mLetterAdapter = new LetterAdapter(characterList);
        RecyclerView letterReView = findViewById(R.id.re_view);
        letterReView.setAdapter(mLetterAdapter);
        letterReView.setLayoutManager(new linearlayoutmanager(this,RecyclerView.VERTICAL,false));

Run to the mobile phone or simulator and open the activity. Some friends found that why does a sub item item item occupy the whole screen? Because the item we set earlier fills the screen. Back to item_ Letter. XML, take a look at the root layout settings. Put layout_ Change the setting in height = "match_parent" to wrap_ content。 Recompile and run again to see the results.

You can also set a fixed height for the root layout. It depends on art design and demand.

Here we have mastered the basic usage of recyclerview. Next, learn how to customize its presentation. For example, click or long press item; Add a dividing line between items; Add / delete / modify data; Use waterfall flow styles, and so on. Recyclerview uses adapter mode.

You can refer to the official recyclerview documentation.

Put the project here: https://github.com/AnRFDev/Tutorial2020

Related reading recyclerview - getting started recyclerview click events - how to set click events recyclerview examples - actually use recyclerview to obtain sliding distance recyclerview displays a variety of items

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>