两个RecyclerView.ItemDecoration相互重叠

时间:2016-01-14 05:10:00

标签: android android-recyclerview

在我们的应用程序中,我们在RecyclerView中使用两个ItemDecorations。一个是StickyHeader来显示日期,另一个是标题来显示New Messages标题。问题是当它们被添加到同一个项目时,它们会相互重叠。我怎样才能防止这种情况发生。

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

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

enter image description here

1 个答案:

答案 0 :(得分:0)

我使用here中的代码来解决两个ItemDecoration重叠。 (有关详细信息,请查看@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);
    }
}