'How to add margin start to DividerItemDecoration Android

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



Solution 1:[1]

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

Solution 2:[2]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Zhou Hongbo
Solution 2 Vlad