Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to add footerview to android RecyclerView

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to add footerview to android RecyclerView". In daily operation, I believe many people have doubts about how to add footerview to android RecyclerView. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "how to add footerview to android RecyclerView". Next, please follow the editor to study!

Class FooterViewAdapter: RecyclerView.Adapter () {companion object {const val TYPE_FOOTER: Int = 1 const val TYPE_NORMAL: Int = 0} var isFooterView: Boolean = false fun setFooterView () {this.isFooterView = true notifyItemInserted (itemCount)} fun removeFooterView () {this.isFooterView = false} override fun getItemViewType (position: Int): Int {return if (isFooterView & & Position = = itemCount-1) TYPE_FOOTER else TYPE_NORMAL} override fun onCreateViewHolder (parent: ViewGroup ViewType: Int): RecyclerView.ViewHolder {if (viewType = = TYPE_FOOTER) {val view = LayoutInflater.from (parent.context) .propagate (R.layout.focus_footer_view_no_line, parent, false) return BottomViewHolder (view)} other VIewHolder} override fun onBindViewHolder (holder: RecyclerView.ViewHolder) set by return Position: Int) {if (getItemViewType (position) = = TYPE_FOOTER) return your binding events do not move} class BottomViewHolder (view: View): RecyclerView.ViewHolder (view) {} override fun getItemCount (): Int {return if (isFooterView & & mData.size > 0) mData.size + 1 else mData.size}}

1. Define the type of viewtype, one is normal, the other is the bottom view

Companion object {const val TYPE_FOOTER: Int = 1 const val TYPE_NORMAL: Int = 0} 2. Define a Boolean variable with or without FooterView (1) if your bottom view is fixed, you don't need to parse the xml layout multiple times, just parse the layout once in the onCreateViewHolder method. Var isFooterView: Boolean = false (2) if your bottom layout is changing, then var isFooterView: View should be set here

3.FooterView correlation method

/ / set FooterViewfun setFooterView () {this.isFooterView = true notifyItemInserted (itemCount) / / this method is to notify Adapter of a new Item insertion} / / remove FooterViewfun removeFooterView () {this.isFooterView = false}

4. Rewrite getItemViewType

According to isFooterView, determine whether adding FooterView&&item is the last one to locate the last item. All other ViewType are TYPE_NORMAL.

Override fun getItemViewType (position: Int): Int {return if (isFooterView & & position = = itemCount-1) TYPE_FOOTER else TYPE_NORMAL}

5.itemCount

This method is very important, is to calculate how many Item, it is also used above.

It's easy to add FooterView,ItemCount plus 1.

Override fun getItemCount (): Int {return if (isFooterView & & mData.size > 0) mData.size + 1 else mData.size}

6. Next, create an empty ViewHolder. Of course, you can reuse other created ViewHolder.

Class BottomViewHolder (view: View): RecyclerView.ViewHolder (view) {}

7. Rewrite onCreateViewHolder to parse different layouts according to viewType.

Override fun onCreateViewHolder (parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {if (viewType = = TYPE_FOOTER) {val view = LayoutInflater.from (parent.context) .propagate (R.layout.focus_footer_view_no_line, parent, false) return BottomViewHolder (view)} other VIewHolder set by return}

8. Override onBindViewHolder, skip if viewType is TYPE_FOOTER

Override fun onBindViewHolder (holder: RecyclerView.ViewHolder, position: Int) {if (getItemViewType (position) = = TYPE_FOOTER) return your binding events do not move}

9. Finally, getAdapterPosition and getLayoutPosition.

The recommendations are as follows:

Use holder.getAdapterPosition (); to get data in real time when you need to bind data.

Use holder.getLayoutPosition (); to get the location when you listen for events such as click / swipe

In most of the time, there is no difference between holder.getAdapterPosition () and holder.getLayoutPosition (), because the difference between the two data is within 16ms.

At this point, the study on "how to add footerview to android RecyclerView" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report