Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.graphics.Canvas;
- import android.graphics.Paint;
- import android.graphics.Rect;
- import android.support.annotation.ColorInt;
- import android.support.v4.view.ViewCompat;
- import android.support.v7.widget.RecyclerView;
- import android.view.View;
- public class DividerItemDecoration extends RecyclerView.ItemDecoration {
- public static final int HORIZONTAL = 1;
- public static final int VERTICAL = 2;
- public static final int BOTH = 3;
- private Paint paint;
- private int dividerHeight;
- private int orientation;
- private DividerItemDecoration() {
- paint = new Paint(Paint.ANTI_ALIAS_FLAG);
- paint.setStyle(Paint.Style.FILL);
- }
- public void setDividerHeight(int dividerHeight) {
- this.dividerHeight = dividerHeight;
- }
- public void setColor(int color) {
- paint.setColor(color);
- }
- public int getOrientation() {
- return orientation;
- }
- public void setOrientation(int orientation) {
- this.orientation = orientation;
- }
- @Override
- public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
- if (orientation == VERTICAL) {
- drawVertical(c, parent);
- } else if (orientation == HORIZONTAL) {
- drawHorizontal(c, parent);
- } else if (orientation == BOTH) {
- drawVertical(c, parent);
- drawHorizontal(c, parent);
- }
- }
- protected void drawVertical(Canvas c, RecyclerView parent) {
- final int left = parent.getPaddingLeft();
- final int right = parent.getWidth() - parent.getPaddingRight();
- final int childCount = parent.getChildCount();
- for (int i = 0; i < childCount; i++) {
- final View child = parent.getChildAt(i);
- if (parent.getChildAdapterPosition(child) == (parent.getAdapter().getItemCount() - 1)) {
- continue;
- }
- final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
- final int bottom;
- final int top;
- top = child.getBottom() + params.bottomMargin + Math.round(ViewCompat.getTranslationY(child));
- bottom = top + dividerHeight;
- c.drawRect(left, top, right, bottom, paint);
- }
- }
- protected void drawHorizontal(Canvas c, RecyclerView parent) {
- final int top = parent.getPaddingTop();
- final int bottom = parent.getHeight() - parent.getPaddingBottom();
- final int childCount = parent.getChildCount();
- for (int i = 0; i < childCount; i++) {
- final View child = parent.getChildAt(i);
- if (parent.getChildAdapterPosition(child) == (parent.getAdapter().getItemCount() - 1)) {
- continue;
- }
- final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
- .getLayoutParams();
- final int right;
- final int left;
- left = child.getRight() + params.rightMargin + Math.round(ViewCompat.getTranslationX(child));
- right = left + dividerHeight;
- c.drawRect(left, top, right, bottom, paint);
- }
- }
- @Override
- public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
- if (orientation == VERTICAL) {
- outRect.set(0, 0, 0, dividerHeight);
- } else if (orientation == HORIZONTAL){
- outRect.set(0, 0, dividerHeight, 0);
- } else if (orientation == BOTH) {
- outRect.set(0, 0, dividerHeight, dividerHeight);
- }
- }
- public static class Builder {
- private int dividerHeight;
- private int color;
- private int orientation;
- public Builder dividerHeight(int dividerHeight) {
- this.dividerHeight = dividerHeight;
- return this;
- }
- public Builder color(@ColorInt int color) {
- this.color = color;
- return this;
- }
- public Builder orientation(int orientation) {
- this.orientation = orientation;
- return this;
- }
- public DividerItemDecoration build() {
- DividerItemDecoration divider = new DividerItemDecoration();
- if (dividerHeight == 0) {
- // Set default divider height to 1px.
- divider.setDividerHeight(1);
- } else if (dividerHeight > 0) {
- divider.setDividerHeight(dividerHeight);
- } else {
- throw new IllegalArgumentException("Divider's height can't be negative.");
- }
- if (color == 0) {
- throw new IllegalArgumentException("Don't forget to set color");
- } else {
- divider.setColor(color);
- }
- if (orientation != HORIZONTAL && orientation != VERTICAL && orientation != BOTH) {
- throw new IllegalArgumentException("Invalid orientation");
- } else {
- divider.setOrientation(orientation);
- }
- return divider;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement