RecyclerView页眉和页脚 [英] RecyclerView header and footer
问题描述
也许这个问题已经被问过,但我似乎无法找到一个precise答案或解决方案。我开始使用RecyclerView,我实现它使用LinearLayoutManager。现在我想添加自定义页眉和页脚的项目,从在我RecyclerView其余项目有所不同。页眉和页脚不应该是粘的,我希望他们能够用滚动的其余项目。有人能指出一些例子,如何做到这一点,或只是分享想法。我将AP preciate非常喜欢。 THX
Maybe this question has been asked before, but I could not seem to find a precise answer or solution. I started using the RecyclerView, and I implemented it using the LinearLayoutManager. Now I want to add custom header and footer items, that differ from the rest of the items in my RecyclerView. The header and footer should not be sticky, I want them to scroll with the rest of the items. Can somebody point out some example how to do this or just share ideas. I will appreciate it very much. Thx
推荐答案
在你的适配器添加此类:
in your adapter add this class:
private class VIEW_TYPES {
public static final int Header = 1;
public static final int Normal = 2;
public static final int Footer = 3;
}
然后重写这样下面的方法:
then Override the following method like this:
@Override
public int getItemViewType(int position) {
if(items.get(position).isHeader)
return VIEW_TYPES.Header;
else if(items.get(position).isFooter)
return VIEW_TYPES.Footer;
else
return VIEW_TYPES.Normal;
}
现在在onCreateViewHolder方法充气基于视图类型::
Now in the onCreateViewHolder method inflate your layout based on the view type::
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View rowView;
switch (i)
{
case VIEW_TYPES.Normal:
rowView=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.normal, viewGroup, false);
break;
case VIEW_TYPES.Header:
rowView=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.header, viewGroup, false);
break;
case VIEW_TYPES.Footer:
rowView=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.footer, viewGroup, false);
break;
default:
rowView=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.normal, viewGroup, false);
break;
}
return new ViewHolder (rowView);
}
希望这可以帮助。
Hope this can help.
这篇关于RecyclerView页眉和页脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!