Skip to content

Instantly share code, notes, and snippets.

@Firsto
Created July 18, 2017 07:52
Show Gist options
  • Save Firsto/494d42ef82b744ff4ca1826194908832 to your computer and use it in GitHub Desktop.
Save Firsto/494d42ef82b744ff4ca1826194908832 to your computer and use it in GitHub Desktop.
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import gb.sweetlifecl.R;
public class OrderListDividerDecorator extends RecyclerView.ItemDecoration {
private final Drawable mDivider;
private final float offset;
private int leftPadding;
private int topPadding;
private int rightPadding;
private int bottomPadding;
public OrderListDividerDecorator(Context context, Drawable divider) {
offset = context.getResources().getDimension(R.dimen.margin8);
mDivider = divider;
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
int dividerLeft = parent.getPaddingLeft();
int dividerRight = parent.getWidth() - parent.getPaddingRight();
for (int i = 0; i < parent.getChildCount(); i++) {
View child = parent.getChildAt(i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
int dividerTop = child.getBottom() + params.bottomMargin;
int dividerBottom = dividerTop + mDivider.getIntrinsicHeight();
mDivider.setBounds(dividerLeft + leftPadding, dividerTop + topPadding, dividerRight - rightPadding, dividerBottom - bottomPadding);
mDivider.draw(c);
}
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
if (parent.getChildAdapterPosition(view) == 0) return;
if (view.getId() == R.id.order_footer_item) {
outRect.bottom = (int) (mDivider.getIntrinsicHeight() + offset);
}
outRect.top = mDivider.getIntrinsicHeight();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment