Add footview and headview to Android recyclerview

Premise summary:

The previous article has introduced the basic usage of recyclerview. The original text is as follows: the layout of Android recyclerview is really just that simple! This article is an introduction to the deeper use of recyclerview.

Footview and headview have corresponding functions in listview, but not in the trendy recyclerview. Footview plays a very important role in paging loading (more pull-up loading), so you must learn. (compared with the addition of footview, the addition of headview is roughly the same. Here we only talk about the addition of footview)

Effect: (the source code is at the end of the article)

Implementation key

Int getitemviewtype (int position): this function is a self-contained function in recyclerview. The parameter is the position of each item and returns an int type representation type.

This function is used to distinguish between ordinary items and footview items, so that the footview item can always be at the bottom of the adapter.

In the example, two types are defined as follows:

The function is as follows:

Implementation steps

1. Define getitemviewtype (int position) and define the type of viewtype you need. 2. In defining viewholder(), oncreateviewholder() and onbindviewholder(), two cases are considered: one is a common item and the other is a footview. In addition, the default viewholder() function will only have a view ItemView parameter. Here, you need to add the parameter int viewtype. The code is as follows:

Here is the general function of the three functions:

ViewHolder(View itemView,int viewType):

Binding the control in the item layout to the property defined in the viewholder makes it easier to use in onbindviewholder().

onCreateViewHolder(ViewGroup parent,int viewType):

This function is used to create each item. Instead of a view, it returns a viewholder.

onBindViewHolder(ViewHolder holder,int position):

This function is generally used to bind data to the control in item.

code:

Based on the above analysis, it should be easier for readers to understand. The key adapter code and source code address are attached below:

Expansion and extension

In the next article, the author has made some changes on this basis, which can easily realize the function of paging loading. The article address is as follows: http://blog.csdn.net/double2hao/article/details/52788708

Source address: http://xiazai.jb51.net/201610/yuanma/AndroidPullRecyclerView (jb51.net). rar

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