5

I used the below code to add dividers to my list. But my question is how can I add margin_start to those dividers so the that it will look something like the attached image. Thank in advance.

DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
    layoutManager.getOrientation());
recyclerView.addItemDecoration(dividerItemDecoration);

sample

Sai
  • 15,188
  • 20
  • 81
  • 121
  • could you add padding to the recyclerView itself instead? – Mercato May 13 '17 at 15:38
  • [See this answer. By far the best and easiest solution](https://stackoverflow.com/questions/40434248/how-to-indent-the-divider-in-a-linear-layout-recyclerview-ie-add-padding-marg) – Bamerza Oct 10 '18 at 21:35

2 Answers2

0

I found the best solution

<inset xmlns:android="http://schemas.android.com/apk/res/android"
       android:insetLeft="40dp"
       android:insetRight="40dp" >
    <shape>
        <size android:height="1dp"/>
        <solid android:color="@color/recyclerview_divider" />
    </shape>
</inset>

From: https://stackoverflow.com/a/40434249/5093308

Zhou Hongbo
  • 1,297
  • 13
  • 25
-1

You need to override method getItemOffsets(Rect, View, RecyclerView, RecyclerView.State).


See example:

DividerItemDecoration dividerItemDecoration = new 
    DividerItemDecoration(recyclerView.getContext(),layoutManager().getOrientation()){

    private int startMargin = Utils.fromDpToPx(10f);

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        if (parent.getChildAdapterPosition(view) == 0){
            outRect.set(0, startMargin, 0, 0);
        }
    }
};
recyclerView.addItemDecoration(dividerItemDecoration);
Vlad
  • 497
  • 7
  • 12
  • This changes the margins of the list items (which is not what we want) and not the divider (which is what we want). – davidchuyaya Jul 26 '18 at 03:38