Talk about the full solution of recyclerview developed by Android

Since Android 5.0, Google has launched the recylerview control, recylerview. I think after seeing a new term, most people will first ask, what is recylerview? Why is there a recylerview, that is, what are the advantages of recylerview? How does recylerview work? Next, we will discuss these issues together. If there is any fallacy, please criticize and correct it. If you have any questions, please leave a message.

Through this blog, you will learn the following knowledge points

① Advantages of recyclerview over listview

② Preliminary usage of recyclerview

③ Recyclerview add separator

④ Recyclerview changes the style of the separation line

⑤ Usage of recyclerview adapter

⑥ Comparison of several refresh methods in recyclerview. Adapter

⑦ Add an entry click event to recyclerview

1. What is recyclerview?

Recyclview is a new component in the support-v7 package. It is a powerful sliding component. Compared with the classic listview, it also has the function of item recycling and reuse. This can also be seen from its name, recyclview. Seeing this, some people may ask, don't you already have a listview? Why do you want a recelerview? This involves the second problem.

2. What are the advantages of recyclerview?

According to the official introduction, recylerview is an upgraded version of listview. In this case, recylerview must have its advantages. Now the advantages of recylerview over listview are listed as follows:

① Recylerview encapsulates the recycling and reuse of viewholder, that is, recylerview standardizes the viewholder, writes the adapter for the viewholder instead of the view, and the reuse logic is encapsulated, making it easier to write.

② It provides a plug-in experience with high decoupling and exceptional flexibility. For the display of an item, the corresponding class is specially extracted from the recylerview to control the display of the item, making it very scalable. For example, if you want to control the horizontal or vertical sliding list effect, you can control it through the class linearlayoutmanager (the gridlayoutmanager corresponding to the GridView effect and the staggeredgridlayoutmanager corresponding to the waterfall flow). That is to say, the stylerview is no longer limited to the linear display mode of the listview, but can also realize various effects such as the GridView effect. If you want to control the separation line of items, you can inherit the itemdecoration class of recylerview, and then write code according to your business needs.

③ You can control the animation of item addition and deletion, which can be controlled through the itemanimator class. Of course, for the animation of addition and deletion, recelerview has its own default implementation.

3. Usage of recyclerview

3.1 preliminary usage of recyclerview (including recyclerview. Adapter usage)

After saying so much, you may be most concerned about how to use recylerview. Let's first discuss the theoretical knowledge of the usage of recylerview, and then experience the advantages of recylerview with an example. First of all, we need to understand that it is necessary to import the support-v7 package to use recylerview. I mentioned that recylerview is highly decoupled, Exceptionally flexible, Google provides us with multiple classes to control the display of items.

It can be seen that the setting process of the recylerview is more complex than the listview, which is also a manifestation of the highly decoupled recylerview. Although the code description is a little complex, its scalability is very high.

After understanding some controls of recyclerview, let's take a look at the writing method of its adapter. There are still some differences between recyclerview's adapter and listview's adapter. Recyclerview.adapter needs to implement three methods:

①onCreateViewHolder()

This method mainly generates a view for each item inflator, but the method returns a viewholder. This method directly encapsulates the view in the viewholder, and then we target the instance of viewholder. Of course, this viewholder needs to be written by ourselves. The cumbersome steps of convertview. Settag (holder) and convertview. Gettag () are directly omitted.

②onBindViewHolder()

This method is mainly used to adapt rendering data to view. Method provides you with a viewholder instead of the original convertview.

③getItemCount()

This method is similar to the getcount method of baseadapter, that is, how many entries are there in total. Examples: next, let's take a few small examples to help you better understand the usage of recyclerview. First, let's implement the simplest list. The effects are as follows

The mainacitivity code for this effect is as follows

The code of recyclerview adapter is as follows:

It can be seen that recyclerview standardizes the viewholder, writes the adapter for the viewholder instead of the view, and the reuse logic is encapsulated, making it easier to write. In fact, its writing method is similar to that of baseadapter. You can compare it with that of getview method. Initialize a view in oncreateviewholder method, and then return a viewholder. The returned viewholder is similar to convertview. Gettag() in getview, Then fill the value of the control in the viewholder in the onbindviewholder method. In fact, its principle is similar to that of getview, but it is encapsulated. We write it more succinctly. Here, seeing the above operation effect, many people may say that the effect is too ugly. There is no separation line. Don't worry. Let's take it step by step.

3.2 recyclerview adds separation lines

As mentioned earlier, it can be set through the recyclerview. Additemdecoration (itemdecoration decoration) method. The required parameters are an object inherited from itemdecoration defined by ourselves. We can create a class that inherits recyclerview.itemdecoration to draw the separation line. Through itemdecoration, we can visually separate each item from each other. For example, the effect of listview's divider is very similar. Of course, as in our example above, itemdecoration is not set and no error is reported, which shows that itemdecoration is not mandatory. As our developers, we can set or not set decoration. Implement an itemdecoration. The itemdecoration provided by the system is an abstract class. In addition to the abandoned methods, we mainly implement the following three methods:

And because when recyclerview is drawing, it will draw and decorate, so it will call OnDraw and ondrawover methods. In fact, we can just rewrite OnDraw and getitemoffsets. Then, when the layoutmanager performs item layout, it will call the getitemoffset method to calculate the appropriate size of each item's decoration. Let's implement a decoration, divideritemdecoration.java

Here, we use the system theme (Android. R.attr.listdivider) to set the component separator, then obtain the size, draw the position with setbound (), and then set and draw the whole area with outrect. Set (). Of course, there are two cases: one is linearlayoutmanager.horizontal, and the other is linearlayoutmanager.vertical, which needs to be processed separately, Finally, don't forget to set the custom split line in recyclerview, and then add a recyclerview. Additemdecoration (New divideritemdecoration (mainactivity. This, linearlayoutmanager. Vertical)) in mainactivity to add a split line to recyclerview. Then run, the effect is as follows:

You can see that there are separation lines, which is basically the same as the effect of listview. Of course, since Google has provided us with this special method to add separation lines, it will certainly allow us to customize the style of separation lines, otherwise it will make no sense to extract this method.

3.3 changing separator style

So how do I change the style of the separation line? In the above class divideritemdecoration, we can see that the separator is the same as listview, that is, the default style of the system. Therefore, we can change it in the XML file of styles, as follows:

The content of the divider is as follows:

The operation effect after modification is as follows:

You can see that the color of the separation line has changed. Of course, this is just a small example. We can change it according to business needs, so that the effect of listview can be basically realized. When you see this, some people will say that NIMA is so troublesome. It is not as simple as listview. From the amount of code above, it is really complex to use, However, if you want to display this list in the form of GridView at this time, the use of recylerview is just a matter of changing a line of code. We used it in the above code

Recyclerview.layoutmanager is an abstract class. The system provides us with three implementation classes

① Linear layout manager is linear layout, which is the layout we used in the above example

② Gridlayoutmanager is the table layout

③ Staggeredgridlayoutmanager is a flow layout, such as waterfall flow effect

If the above example is changed to the effect of GridView, the corresponding code should be changed like this

In addition, the above separation line should also be changed accordingly, because in the above divideritemdecoration method, from

We can see that these two lines draw a line, which is the remaining part after removing the left and right margins from the recyclerview. When displayed as a listview, each line has an item, so the overall effect looks similar to that of the listview. When displayed as a GridView, there are more than one item in each line, and there may be multiple items, So this class is no longer applicable. We need to rewrite it.

Don't forget to change the divider recyclerview. Additemdecoration (New dividergriditemdecoration (this)); After running, the results are as follows

You can see that if you have prepared the class of separation line, you only need a few lines of code from listview effect to GridView effect. Does it feel tall in an instant? There is also a more eye popping effect. Make the following changes to the above code

It should be noted here that the second parameter of the staggeredgridlayoutmanager structure passes an orientation. If the passed in parameter is staggeredgridlayoutmanager.vertical, the previous parameter represents the number of columns; If it is staggeredgridlayoutmanager.horizontal, the previous parameter represents the number of rows

The operation effect is as follows

Is this effect a little against the sky? As you can see, it is fixed to 4 lines and becomes sliding left and right. It should be noted that if it is horizontal, the width of the item needs to be set. After all, the horizontal width is not constrained, because the control can scroll horizontally. In addition, it can also achieve the effect of waterfall flow. I'm going to write a blog about waterfall flow later.

3.4 animation added and deleted by recyclerview (including comparison of several refresh methods in recyclerview. Adapter)

As mentioned above, the animation controlling the addition and deletion of recyclerview is implemented through the itemanimator class. The itemanimator class is also an abstract class. By default, the system provides us with an animation for addition and deletion. Let's take a look at the effects of this animation. The modifications we need to make are as follows:

Then rewrite the of actionbar

The main.xml file code in r.menu. Main will not be pasted. In the last summary example, there will be two methods added in recyclerviewadapter:

Here are several methods to refresh data in recyclerview. Adapter

The notifydatasetchanged () method is the same as the listview adapter method we usually use, so we won't describe it here.

Notifyitemchanged (int position). This method will be called when the data of position position changes, and the onbindviewholder () method of the corresponding position will be called back. Of course, because the viewholder is reused, if the position is outside the current screen, it will not be called back, because it is meaningless, The next time position scrolls within the current screen, the onbindviewholder () method will also be called to refresh the data. The same is true for other methods. Public final void notifyitemrangechanged (int positionstart, int itemcount). As the name suggests, you can refresh the number of items in itemcount from positionstart (the refresh here refers to the callback onbindviewholder() method).

Public final void notifyiteminserted (int position). This method can be used to refresh when a piece of data is inserted at position. Note that there will be inserted animation after this method is called. This animation can be used by default or defined by itself. Public final void notifyitemmoved (int fromposition, int toposition). This method can be used to refresh when moving from fromposition to toposition

Public final void notifyitemrangeinserted (int positionstart, int itemcount), which is obviously added in batch.

Public final void notifyitemremoved (int position). When the position is deleted, it will be refreshed, and there will also be animation.

Run the above changes and click the add and delete buttons. The effect diagram is as follows:

Public final void notifyitemrangeremoved (int positionstart, int itemcount), batch deletion.

We can see that the animation effect provided by the system is quite good. Of course, we can also define the animation effect ourselves according to the business needs.

3.5 add a click event to the item of recyclerview

There is another point that we haven't mentioned from the beginning of the article until now, that is, the item click event recyclerview listens for event processing. When the listview is used, the control provides us with an onitemclicklistener listener. In this way, when we click an item, it will call back relevant methods so that we can easily handle the item click event. For recyclerview, it is a pity that the control does not provide us with such a built-in listener method, but we can modify the implementation to realize the monitoring of item click events. These two methods are added to our adapter

Then the onbindviewholder method needs to make the following changes

Added in mainacitivity

Then run, the effect is as follows:

You can see that the onclick and onlongclick events of the item are triggered. So far, the basic usage of recyclerview is almost introduced. Of course, there are several points not mentioned, such as waterfall flow, pull-down refresh, pull-up load, etc. due to space reasons, these will be presented to you in the following updates. Finally, integrate the knowledge points mentioned in this blog and write them into the demo. You can refer to the demo for learning

Source download: Demo

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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