Encapsulate the recyclerview implementation in Android and add the header and bottom sample code

preface

We all know that listview has methods to add headers and bottoms, but recyclerview does not have such methods. Therefore, the recyclerview cannot add a bottom and a head, but can you copy the listview to add a head and a bottom? The answer is certainly feasible. In this article, we will introduce the contents related to the addition of the header and bottom of the recyclerview in the Android package, and share them for your reference and study. I won't say much below. Let's take a look at the detailed introduction together.

First, let's look at the effect:

The code is as follows:

This is the encapsulated recyclerview. There are three main methods: addheaderview, addfooterview and overridden setadapter. The only idea here is to steal the beam and change the column. When we add the head, tail or set the adapter, the real adapter is not the adapter we passed in, but a new recyclerheaderviewlistadapter. This is the adapter finally set by recyclerview.

In fact, we can see that the listview adds the head and tail through this idea.

This is the addheaderview method of listview, which will secretly create the headerviewlistadapter adapter. The final added header and footer are implemented in the headerviewlistadapter.

This is the recyclerheaderviewlistadapter that implements adding headers, footers, and incoming adapters. The specific logic is explained in the notes of the document. Logic is implemented by imitating the headerviewlistadapter of listview.

In fact, you can create an adapter, and then create entries and bind the data of entries according to different entry types.

This is the layout file

You can directly call addheaderview and addfooterview.

In addition to implementing addheaderview and addfooterview in this way, another way is to encapsulate the adapter. The principle remains the same: create entries and bind entry data according to different entry types.

summary

The above is the whole content of this article. I hope the content of this article can bring some help to the study or work of Android developers. If you have any questions, you can leave a message. Thank you for your support for 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
分享
二维码
< <上一篇
下一篇>>