RecyclerView页眉和页脚

也许以前有人问过这个问题,但我似乎找不到确切的答案或解决方案。我开始使用RecyclerView,并使用LinearLayoutManager实施了它。现在,我想添加自定义的页眉和页脚项目,这些项目与RecyclerView中的其余项目不同。页眉和页脚不应发粘,我希望它们与其余项目一起滚动。可以有人指出如何做到这一点或只是分享想法的例子。我会非常感激。谢谢



慕的地10843
浏览 600回答 3
3回答

倚天杖

在您的适配器中添加此类:private class VIEW_TYPES {        public static final int Header = 1;        public static final int Normal = 2;        public static final int Footer = 3;}然后覆盖这样的以下方法:@Overridepublic 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方法中,根据视图类型为您的布局充气:@Overridepublic 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);}现在,在onBindViewHolder方法中,基于视图持有者绑定布局:@Override    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {        int viewType = getItemViewType(position);        switch(viewType) {            case VIEW_TYPES.Header: // handle row header                break;            case VIEW_TYPES.Footer: // handle row footer                break;            case VIEW_TYPES.Normal: // handle row item                break;        }    }希望这会有所帮助。

慕婉清6462132

使用ItemDecorations无需修改任何其他代码,这非常容易:recyclerView.addItemDecoration(new HeaderDecoration(this,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;recyclerView,&nbsp; R.layout.test_header));保留一些用于绘制的空间,填充要绘制的布局,然后在保留的空间中绘制它。装饰代码:public class HeaderDecoration extends RecyclerView.ItemDecoration {&nbsp; &nbsp; private View mLayout;&nbsp; &nbsp; public HeaderDecoration(final Context context, RecyclerView parent, @LayoutRes int resId) {&nbsp; &nbsp; &nbsp; &nbsp; // inflate and measure the layout&nbsp; &nbsp; &nbsp; &nbsp; mLayout = LayoutInflater.from(context).inflate(resId, parent, false);&nbsp; &nbsp; &nbsp; &nbsp; mLayout.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {&nbsp; &nbsp; &nbsp; &nbsp; super.onDraw(c, parent, state);&nbsp; &nbsp; &nbsp; &nbsp; // layout basically just gets drawn on the reserved space on top of the first view&nbsp; &nbsp; &nbsp; &nbsp; mLayout.layout(parent.getLeft(), 0, parent.getRight(), mLayout.getMeasuredHeight());&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < parent.getChildCount(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; View view = parent.getChildAt(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (parent.getChildAdapterPosition(view) == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c.save();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final int height = mLayout.getMeasuredHeight();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final int top = view.getTop() - height;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c.translate(0, top);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mLayout.draw(c);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c.restore();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {&nbsp; &nbsp; &nbsp; &nbsp; if (parent.getChildAdapterPosition(view) == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outRect.set(0, mLayout.getMeasuredHeight(), 0, 0);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outRect.setEmpty();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

qq_笑_17

您可以使用此GitHub]库以RecyclerView最简单的方式向您的页面添加页眉或页脚。您需要在项目中添加HFRecyclerView库,也可以从Gradle中获取它:compile 'com.mikhaellopez:hfrecyclerview:1.0.0'该库基于@hister的工作
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android