class FooterViewAdapter :<>() { companion object { const val TYPE_FOOTER: Int = 1 const val TYPE_NORMAL: Int = 0 } var isFooterView: Boolean = false fun setFooterView() { = true notifyItemInserted(itemCount) } fun removeFooterView() { = 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): { if (viewType == TYPE_FOOTER) { val view = () .inflate(.focus_footer_view_no_line, parent, false) return BottomViewHolder(view) } return Other settingsVIewHolder } override fun onBindViewHolder(holder: , position: Int) { if (getItemViewType(position) == TYPE_FOOTER) return Your binding event does not need to be moved } class BottomViewHolder(view: View) : (view) { } override fun getItemCount(): Int { return if (isFooterView && > 0) + 1 else } }
1. Define the type of viewtype, one is normal and the other is the bottom view
companion object { const val TYPE_FOOTER: Int = 1 const val TYPE_NORMAL: Int = 0 }
2.Define whether to add aFooterViewofBooleanvariable (1)如果你of底部view是固定of,No need to parse multiple timesxmllayout,Directly inonCreateViewHolder方法里解析一次layout。 var isFooterView: Boolean = false (2)如果你of底部layout是变化of,那这里设置of就应该是 var isFooterView: View
Related Methods
//Set FooterViewfun setFooterView() { = true notifyItemInserted(itemCount) //This method is to notify Adapter that there is a new Item insertion } //Remove FooterView fun removeFooterView() { = false }
4. Rewrite getItemViewType
According to isFooterView, determine whether the FooterView&&item is the last one to locate the last one. The other ViewTypes are TYPE_NORMAL
override fun getItemViewType(position: Int): Int { return if (isFooterView && position == itemCount - 1) TYPE_FOOTER else TYPE_NORMAL }
This method is very important, it is to calculate how many Items there are, and it is also used above.
It's very simple, add FooterView, add 1 for ItemCount
override fun getItemCount(): Int { return if (isFooterView && > 0) + 1 else }
6. Next create an empty ViewHolder, of course, you can reuse other created ViewHolders
class BottomViewHolder(view: View) : (view) { }
7. Rewrite onCreateViewHolder and parse different layouts according to viewType.
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): { if (viewType == TYPE_FOOTER) { val view = () .inflate(.focus_footer_view_no_line, parent, false) return BottomViewHolder(view) } return Other settingsVIewHolder }
8. Rewrite onBindViewHolder, and skip it if viewType is TYPE_FOOTER
override fun onBindViewHolder(holder: , position: Int) { if (getItemViewType(position) == TYPE_FOOTER) return Your binding event does not need to be moved }
9. Finally, let’s talk about getAdapterPosition and getLayoutPosition
The suggestions are as follows:
When you need to bind data, use(); to get it in real time
When you click/slide and other monitoring events, use (); to get the location
Most of the time, there is no difference between() and(), because the difference in data between the two is within 16ms
Summarize
This is the end of this article about adding footerview to Android RecyclerView. For more related content to add footerview to RecyclerView, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!