Add Margins To Divider In Recyclerview
i am building an android app which is using RecyclerView. I want to add dividers to RecyclerView, which I did using this code: DividerItemDecoration dividerItemDecoration = new Div
Solution 1:
I think the most straightforward solution is to use the setDrawable method on the Decoration object and pass it an inset drawable with the inset values you want for the margins. Like so:
int[] ATTRS = newint[]{android.R.attr.listDivider};
TypedArraya= context.obtainStyledAttributes(ATTRS);
Drawabledivider= a.getDrawable(0);
intinset= getResources().getDimensionPixelSize(R.dimen.your_margin_value);
InsetDrawableinsetDivider=newInsetDrawable(divider, inset, 0, inset, 0);
a.recycle();
DividerItemDecorationitemDecoration=newDividerItemDecoration(context, DividerItemDecoration.VERTICAL);
itemDecoration.setDrawable(insetDivider);
recyclerView.addItemDecoration(itemDecoration);
Solution 2:
shape.xml
<?xml version="1.0" encoding="utf-8"?><shapexmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><solidandroid:color="#E6E7F0" /><sizeandroid:height="1dp" /></shape>
layer.xml
<?xml version="1.0" encoding="utf-8"?><layer-listxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:drawable="@drawable/shape"android:left="16dp"android:right="16dp" /></layer-list>
Code
valitemDecoration= DividerItemDecoration(context, DividerItemDecoration.VERTICAL)
itemDecoration.setDrawable(resources.getDrawable(R.drawable.layer, null))
recyclerView.addItemDecoration(itemDecoration)
Solution 3:
Use this and customize according to your requirement.
publicclassDividerItemDecorationextendsRecyclerView.ItemDecoration {
privatestaticfinalint[] ATTRS = newint[]{android.R.attr.listDivider};
private Drawable divider;
/**
* Default divider will be used
*/publicDividerItemDecoration(Context context) {
finalTypedArraystyledAttributes= context.obtainStyledAttributes(ATTRS);
divider = styledAttributes.getDrawable(0);
styledAttributes.recycle();
}
/**
* Custom divider will be used
*/publicDividerItemDecoration(Context context, int resId) {
divider = ContextCompat.getDrawable(context, resId);
}
@OverridepublicvoidonDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
intleft= parent.getPaddingLeft();
intright= parent.getWidth() - parent.getPaddingRight();
intchildCount= parent.getChildCount();
for (inti=0; i < childCount; i++) {
Viewchild= parent.getChildAt(i);
RecyclerView.LayoutParamsparams= (RecyclerView.LayoutParams) child.getLayoutParams();
inttop= child.getBottom() + params.bottomMargin;
intbottom= top + divider.getIntrinsicHeight();
divider.setBounds(left, top, right, bottom);
divider.draw(c);
}
}
}
Solution 4:
You can create your own item decoration for recycler view. Here is code for the same.
publicclassSimpleItemDecoratorextendsRecyclerView.ItemDecoration {
int space;
boolean isHorizontalLayout;
publicSimpleItemDecorator(int space) {
this.space = space;
}
publicSimpleItemDecorator(int space, boolean isHorizontalLayout) {
this.space = space;
this.isHorizontalLayout = isHorizontalLayout;
}
@OverridepublicvoidgetItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
if(isHorizontalLayout)
{
outRect.bottom=space;
outRect.right=space;
outRect.left=space;
outRect.top=space;
} else {
outRect.bottom = space;
if (parent.getChildAdapterPosition(view) == 0)
outRect.top = space;
else
outRect.top = 0;
}
}
}
And to use it with your recyclerview you can do like this:
recyclerView.addItemDecoration(newSimpleItemDecorator(5));
Solution 5:
Elaborating on @SeptimusX75: if you (like me) prefer to do UI stuff in XML you can create an inset drawable file. You'll have to create a second XML file but since in return your code gets cleaner I say it's worth it :-).
divider_base.xml(the actual divider):
<shapexmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><sizeandroid:height="1dp" /><solidandroid:color="@color/dividerColor" /></shape>
divider.xml(the inset):
<insetxmlns:android="http://schemas.android.com/apk/res/android"android:drawable="@drawable/divider_base"android:insetRight="20dp"android:insetLeft="20dp"></inset>
Post a Comment for "Add Margins To Divider In Recyclerview"