Explanation of some advanced techniques used by recyclerview component in Android Development

Advantages of recyclerview:

Usage:

Import recyclerview V7 Library: recyclerview is a control in android.support.v7 library. Therefore, when using it, we need to add compile 'com. Android. Support: recyclerview-v7:22.2.1' in gradle configuration file to import it into Google's official XML layout of this library, and use the conventional control import method to import recyclerview, as follows:

The writing method in the code is basically the same as that in listview, but I still want to focus on:

After instantiating recyclerview, we need to use setlayoutmanager () to set the layout manager for it. The actual parameter is layoutmanager. There are two kinds of layoutmanagers:

1. Staggeredgridlayoutmanager is the streaming layout we mentioned earlier: it has a construction method staggeredgridlayoutmanager (int spancount, int orientation). The first parameter is the number of columns of the grid, and the second parameter is the direction of data presentation (if it is vertical, the meaning of the first parameter is the number of columns, and vice versa is the number of rows. Moreover, the second parameter also has constants with the same name in the staggeredgridlayoutmanager, so please adopt them by yourself. (here, we still recommend that you use the constants brought in the library, because they are generally integer values, so as to avoid different constant values in various classes Other problems]); 2. Gridlayoutmanager, grid layout (streaming layout should be a special case): gridlayoutmanager (context context, int spancount) or gridlayoutmanager (context, int spancount, int orientation, Boolean reverselayout). It should be noted that the last parameter indicates whether to reverse layout (it means that the data is displayed in reverse, originally from left to right, from top to bottom. Set to true, and then all data are reversed) Tip: in the two layoutmanagers, the default orientation is vertical and reverselayout is false. The corresponding parameters have corresponding methods in the gridlayoutmanager for supplementary settings. In the staggeredgridlayoutmanager, all methods judge the reverselayout, but it does not give the API for setting the parameter value.

Linear layout, linear layout manager, linear layout manager (context context) constructs a recyclerview with the default layout direction of vertical, which is also the default of listview. linearlayoutmanager(Context context,boolean reverseLayout)。 No, linear layout and grid layout are almost the same. Only one spancount parameter is missing.

Like listview, set the adapter for recyclerview

As mentioned above, this is how the adapter is written. The viewholder is used to recycle and reuse the view, so we don't need to judge, bind and find in getview (int position, view convertview, ViewGroup parent) after writing the viewholder. Moreover, the viewholder here has become a part that must be inherited in recyclerview. After rewriting, it needs to be placed in recyclerview. Adapter < > to initialize the template of the base class.

Here, recyclerview has been encapsulated for you:

Here, someone will ask, where is the child control findviewbyid of item? We gave it to the viewholder's construction method (other methods can also be used). Its essence is to execute when generating the viewholder in the oncreateviewholder method.

Improvement: 1. Code refactoring: after reading so much, I feel that the function of viewholder is not very clear? It is not only responsible for the query of child controls, but also responsible for the loading of child controls. The layout loading and data binding are left to the adapter... Let's take a look at the practice of Nuggets: first, it separates the functions of the adapter and viewholder in a relatively low coupling way, so that all the logic code in the viewholder only appears in the viewholder. Let's refactor the code mentioned earlier:

ViewHodler:

RecyclerView:

Extract an item click event to make it more like listview:

Interface call

2. External mentioned above

Additemdecoration (itemdecoration decor) can be used to control the interval of items, but the itemdecoration inside is an abstract class and needs to be implemented by ourselves... Let's actually solve this problem:

This is a subclass of the sheep God implementation

Then we add split lines for our recyclerview instance

The default split line of the system often fails to meet the requirements of our products and designers. What should we do?

Through the above XML attributes, set the listdivider of the system as the dividing line drawn by yourself, which only needs to be placed under the topic of your corresponding activity.

Tips: display and hide the recyclerview scroll bar

Vertical display:

Horizontal display:

Hide:

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
分享
二维码
< <上一篇
下一篇>>