If you think the Android project octodroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package com.example.octodroid.views;
//www.java2s.comimport android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
publicclass DividerItemDecoration extends RecyclerView.ItemDecoration {
privatestaticfinalint[] ATTRS = newint[]{
android.R.attr.listDivider
};
private Drawable divider;
public DividerItemDecoration(Context context) {
final TypedArray a = context.obtainStyledAttributes(ATTRS);
divider = a.getDrawable(0);
a.recycle();
}
@Override
publicvoid onDraw(Canvas c, RecyclerView parent) {
drawVertical(c, parent);
}
publicvoid drawVertical(Canvas c, RecyclerView parent) {
finalint left = parent.getPaddingLeft();
finalint right = parent.getWidth() - parent.getPaddingRight();
finalint childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
finalint top = child.getBottom() + params.bottomMargin;
finalint bottom = top + divider.getIntrinsicHeight();
divider.setBounds(left, top, right, bottom);
divider.draw(c);
}
}
@Override
publicvoid getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
outRect.set(0, 0, 0, divider.getIntrinsicHeight());
}
}