如何在 RecyclerView 的末尾添加按钮? [英] How to add a button at the end of RecyclerView?
问题描述
我想在 RecyclerView 的末尾显示一个按钮.
I want to show a button at the end of RecyclerView.
ListView 有一个方法 addFooterView(),如何用 RecylerView 做同样的事情.
With ListView there was a method addFooterView(), how to do the same with RecylerView.
推荐答案
一种方法是让页脚视图成为适配器的ViewType".为此,覆盖 getItemViewType 以返回上一个项目的不同值.
One way to do it would be to make your footer view a "ViewType" of your adapter. In order to do that, overrides getItemViewType to return a different value for your last item.
@Override
public int getItemViewType(int position) {
return (position == mData.size()) ? VIEW_TYPE_FOOTER : VIEW_TYPE_CELL;
}
然后在onCreateViewHolder中,处理不同的viewType.
Then in the onCreateViewHolder, handle the different viewType.
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == VIEW_TYPE_CELL) {
//Create viewholder for your default cell
}
else {
//Create viewholder for your footer view
}
}
别忘了更新getCount()返回的值加1,区分OnBindViewHolder中的2种ViewHolder(以instanceof为例).
Don't forget to update the value return by getCount() by adding 1, and to distinguish the 2 types of ViewHolder in OnBindViewHolder (with instanceof for example).
这篇关于如何在 RecyclerView 的末尾添加按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!