Last active
July 9, 2019 18:39
-
-
Save TheReprator/5add8f9d4851c7a99eceec8df3e6348c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mp.wallypark.utility.itemDecorators; | |
import android.content.Context; | |
import android.graphics.Canvas; | |
import android.graphics.Paint; | |
import android.graphics.Rect; | |
import android.support.annotation.ColorRes; | |
import android.support.annotation.DimenRes; | |
import android.support.v4.content.ContextCompat; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.View; | |
import java.util.Locale; | |
import mp.data.modal.MRecyclerListItem; | |
import mp.utility.LogUtil; | |
public class HeaderSimpleDividerDecoration extends RecyclerView.ItemDecoration { | |
private int dividerHeight; | |
private Paint dividerPaint; | |
private boolean isHorizontal; | |
private boolean isPadding; | |
public HeaderSimpleDividerDecoration(Context context, @DimenRes int divider_height, @ColorRes int color) { | |
dividerPaint = new Paint(); | |
dividerPaint.setColor(getColor(context, color)); | |
dividerHeight = context.getResources().getDimensionPixelSize(divider_height); | |
} | |
public HeaderSimpleDividerDecoration(Context context, @DimenRes int divider_height, | |
@ColorRes int color, boolean isHorizontal, boolean isPadding) { | |
dividerPaint = new Paint(); | |
this.isHorizontal = isHorizontal; | |
this.isPadding = isPadding; | |
dividerPaint.setColor(getColor(context, color)); | |
dividerHeight = context.getResources().getDimensionPixelSize(divider_height); | |
} | |
private int getColor(Context context, @ColorRes int drawable) { | |
return ContextCompat.getColor(context, drawable); | |
} | |
@Override | |
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { | |
super.getItemOffsets(outRect, view, parent, state); | |
int position = parent.getChildAdapterPosition(view); | |
if(-1 >= position) | |
return; | |
int viewType = parent.getAdapter().getItemViewType(position); | |
if (MRecyclerListItem.TYPE_NORMAL == viewType) { | |
// outRect.set(0, 0, 0, mHeightDp); | |
if (isHorizontal) | |
outRect.right = dividerHeight; | |
else | |
outRect.bottom = dividerHeight; | |
} else | |
outRect.setEmpty(); | |
} | |
@Override | |
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { | |
int childCount = parent.getChildCount() -1; | |
int left = parent.getPaddingLeft(); | |
int right = parent.getWidth() - parent.getPaddingRight(); | |
int top = parent.getPaddingTop(); | |
int bottom = parent.getHeight() - parent.getPaddingBottom(); | |
int itemCount = parent.getAdapter().getItemCount(); | |
for (int i = 0; i < childCount ; i++) { | |
View view = parent.getChildAt(i); | |
int position = parent.getChildAdapterPosition(view); | |
int viewType = parent.getAdapter().getItemViewType(position); | |
if (MRecyclerListItem.TYPE_NORMAL == viewType) { | |
if (isHorizontal) { | |
float leftDraw = view.getLeft(); | |
float rightDraw = view.getRight() + dividerHeight; | |
c.drawRect(leftDraw, top, rightDraw, bottom, dividerPaint); | |
} else { | |
int nextItem = position + 1; | |
if(nextItem < itemCount) | |
{ | |
int nextViewType = parent.getAdapter().getItemViewType(nextItem); | |
if(MRecyclerListItem.TYPE_NORMAL != nextViewType) | |
continue; | |
} | |
float topDraw = view.getBottom(); | |
float bottomDraw = view.getBottom() + dividerHeight; | |
if (isPadding) { | |
left = parent.getPaddingLeft() + view.getPaddingLeft(); | |
right = parent.getWidth() - (parent.getPaddingRight() + view.getPaddingRight()); | |
} | |
c.drawRect(left, topDraw, right, bottomDraw, dividerPaint); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
1)mRecyclerview.addItemDecoration(new HeaderSimpleDividerDecoration(context,
R.dimen.scd_recyclierview_divider_big, R.color.view_profile_edit_view));
R.dimen.scd_recyclierview_divider_big, R.color.primary_red, false, true));