如何在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
}
}
不要忘记通过添加1来更新getCount()返回的值,并区分OnBindViewHolder中的两种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屋!