Two RecyclerView.ItemDecoration overlapping each other

送分小仙女□ 提交于 2020-01-16 05:25:09

问题


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);


回答1:


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);
    }
}


来源:https://stackoverflow.com/questions/34782129/two-recyclerview-itemdecoration-overlapping-each-other

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!