Skip to content Skip to sidebar Skip to footer

Draw Multiple Lines On A Recyclerview Background

I'm trying to draw multiple horizontal lines on a RecyclerView background. These lines have to be at a precise position, because there is a list of elements which have to fit betwe

Solution 1:

It looks like you want to draw list dividers. I think you want to use ItemDecoration

When writing a decorator you want to make sure you account for translationY (handles item add/remove animation) and item offsets from other decorations (e.g. layoutManager.getDecoratedBottom(view))

publicclassDividerItemDecorationextendsRecyclerView.ItemDecoration {
    privatestaticfinalint[] ATTRS = newint[]{
            android.R.attr.listDivider
    };

    private Drawable mDivider;

    publicDividerItemDecoration(Context context) {
        finalTypedArraya= context.obtainStyledAttributes(ATTRS);
        mDivider = a.getDrawable(0);
        a.recycle();
    }

    @OverridepublicvoidonDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        intleft= parent.getLeft();
        intright= parent.getRight();
        RecyclerView.LayoutManagerlayoutManager= parent.getLayoutManager();

        intchildCount= parent.getChildCount();
        for (inti=0; i < childCount; i++) {
            Viewchild= parent.getChildAt(i);
            intty= (int) (child.getTranslationY() + 0.5f);
            inttop= layoutManager.getDecoratedBottom(child) + ty;
            intbottom= top + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }
}


recyclerView.addItemDecoration(newDividerItemDecoration(context));

Post a Comment for "Draw Multiple Lines On A Recyclerview Background"