/ RecyclerView

A simple demo to demonstrate how to draw dividers for RecyclerView

A simple demo to demonstrate how to draw dividers for RecyclerView

Dividers for RecyclerView

This is just a simple demo app to demonstrate how to draw dividers for RecyclerView.

RecyclerView doesn't provide straightforward interface for drawing dividers of lists. But actually it provides us a much more flexible way to draw dividers.

As described in Meterial Design Guidelines:

The divider sits within the baseline of the tile.

So I would suggest using OverlayDivider or InsetDivider. OverlayDivider draws dividers on the tiles, needing no extra inset. InsetDivider can draw dividers on or underneath the tiles.

Usage

  • UnderneathDivider
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.addItemDecoration(new UnderneathDivider(this, UnderneathDivider.VERTICAL_LIST));
  • OverlayDivider
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.addItemDecoration(new OverlayDivider(this, OverlayDivider.VERTICAL_LIST));
  • InsetDivider
ItemDecoration divider = new InsetDivider.Builder(this)
                            .orientation(InsetDivider.VERTICAL_LIST) 
                            .dividerHeight(getResources().getDimensionPixelSize(R.dimen.divider_height))
                            .color(getResources().getColor(R.color.colorAccent))
                            .insets(getResources().getDimensionPixelSize(R.dimen.divider_inset), 0)
                            .overlay(true)
                            .build(); 
                            
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.addItemDecoration(divider);

GitHub