How to add headers and footers for recyclerview

In the past two days, in the project, I abandoned listview and wanted to try recyclerview. In the process of using it, I encountered some problems, such as: how to add header and footer for recyclerview? How do I add split lines to recyclerview? How to add drop-down refresh and pull-up load for recyclerview? In the next period of time, I will address these issues one by one by writing a brief book. Today, I bring you the solution to the first problem. How to add headers and footers to recyclerview? Before that, I would like to share my understanding of recyclerview:

1、 In my eyes, recyclerview has attracted more and more attention of our Android developers in the past two years. Many people even say that listview and GridView have been gradually replaced by recyclerview. The main reason is the flexibility and performance improvement of recyclerview. So many people may ask: what is the relationship between recyclerview and listview and GridView? Tell you through a group of screenshots of the official Android documents:

Family spectrum of listview (inheritance structure)

Family spectrum of recyclerview (inheritance structure)

It can be found from the above two figures that listview inherits from abslistview. (girdview is the same). Recyclerview directly inherits the ViewGroup. Finally, it comes to the conclusion that recyclerview is the grandparent of listview, that is, recyclerview is the second master of listview. Therefore, from the level of encapsulation, it comes to the reason why recyclerview has better performance than listview, because the higher the level of encapsulation, the slower the query execution. Of course, there is another reason, Holder is added to the adapter in recyclerview, which is mandatory for developers to use. In listview, many people don't know how to use holder, which also leads to poor performance in using listview. This is also a main reason for the performance improvement of recyclerview. Of course, the higher the encapsulation layer, the more flexible it is, and it is relatively difficult to use. Many methods are encapsulated by themselves. For example, addheaderview (view view) and addfooterview (view view) are used to add their own headers and footers in listview, but not in recyclerview. I will introduce them in detail below:

2、 Add header and footer for recyclerview

1. Know header and footer. Let you know header and footer through the two pictures in the following brief book

Graph with header

Graph with footer

From the above two figures, we can see that header and footer are auxiliary functions in many cases. For example, header is usually used for advertising, while footer is more used to display drop-down loading, and more as a visual component to display loading and improve the user experience.

2. Analyze how to add headers and footers to recyclerview through code. The use of recyclerview is similar to that of listview, but there are only three steps: first, initialize recyclerview; Second, initialize the data and install the data into the view through the adapter; Third, bind the adapter to the recyclerview through the setadapter method. Let's go directly to the code: adding headers and footers through code comments: (1) main layout_ Main.xml: just added a recyclerview

(2) Item layout of listview in recyclerview_ Item.xml: just added a textview

(3) The layout files of headerview and footerview are also a textview. Only the layout of headerview is posted here. The layout file of footerview is the same as that of footerview:

Headerview layout file: header.xml:

OK, now that we have finished the layout file, let's directly look at the content in mainactivity:

(4) The contents in mainactivity.java are:

From the above code, we can see that we have done two things in mainactivity: one is to initialize the view, adapter and data related to recyclerview; The other is to add headerview and footerview to recyclerview through setheaderview () and setfooterview () methods of our custom adapter. Here, we can't wait to know what's in myadapter. Go directly to the code

(5) Code of myadapter.java

From the myadapter class above, there are setheaderview () and setfooterview () methods. We pass headerview and footerview from the activity through these two methods. In the oncreateviewholder () method in the adapter, we use getitemviewtype () to return the type of item (does your item belong to the header family, the footer family, or the listview family?) according to different types, we create different item views. The general idea is like this. For more details, please see the code comments. The following is the screenshot of the operation (I don't know what good GIF generation tool is available under Linux, so I can only cut the static diagram. If you know, you can recommend it to me. Thank you very much):

header

footer

It's finally finished. It needs to be digested and absorbed slowly. This is just a way to implement headerview and footerview. We can see from here that headerview and footerview have passed the adapter directly from the activity. We can also pass only the data of headerview and footerview into the adapter like listview, Then, create different headerholders and footerholders inside the adapter. Load different holders according to the type of returned item. I won't talk about the specific implementation. You can implement it. Through the screenshot, you will find that there is no dividing line between each item in recyclerview, and there is no method such as setdivider() to set the dividing line in recyclerview. Because recyclerview is flexible, it also needs to be customized. In fact, it is very simple. I will bring you how to add dividing lines for recyclerview in the next article. This article is over, There are shortcomings. I hope you can give me more advice. Thank you. Adding separator lines to recyclerview has been realized. Recyclerview series (2): add separator lines to recyclerview. Thank you for your support.

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