两个RecyclerView.ItemDecoration彼此重叠 [英] Two RecyclerView.ItemDecoration overlapping each other

查看:451
本文介绍了两个RecyclerView.ItemDecoration彼此重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的应用程序中,我们在RecyclerView中使用了两个ItemDecorations.一个是StickyHeader来显示日期,另一个是Header来显示New Messages头.问题在于,当将它们添加到同一项目时,它们会相互重叠.我如何防止这种情况发生.

In our app we use two ItemDecorations in RecyclerView. One is StickyHeader to show dates, and other is header to show New Messages header. Problem is that when they are added to same item, they overlap each other. How I can prevent this from happening.

StickyRecyclerHeadersDecoration headersDecoration = new StickyRecyclerHeadersDecoration(getAdapter());
        recyclerView.addItemDecoration(headersDecoration);

NewMessageHeaderDecoration newMessageHeader = new NewMessageHeaderDecoration();
        recyclerView.addItemDecoration(newMessageHeader);

推荐答案

我在此处 a>解决两个ItemDecoration重叠的问题. (有关更多详细信息,请通过@bejibx查看评论)

I used this code from here to solve two ItemDecoration overlap. (For more details check comment by @bejibx)

public void drawVertical(Canvas c, RecyclerView parent)
{
    RecyclerView.LayoutManager manager = parent.getLayoutManager();
    final int left = parent.getPaddingLeft();
    final int right = parent.getWidth() - parent.getPaddingRight();

    final int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++)
    {
        final View child = parent.getChildAt(i);
        final int top = manager.getDecoratedBottom(child);
        final int bottom = top + mDivider.getIntrinsicHeight();
        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(c);
    }
}

这篇关于两个RecyclerView.ItemDecoration彼此重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆