Last active
May 23, 2019 16:12
-
-
Save kevivforever/fd004743c1ac92dd25a140edf95bf230 to your computer and use it in GitHub Desktop.
Recyclerview Helper
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
public class Divider extends RecyclerView.ItemDecoration { | |
private Drawable mDivider; | |
private int mOrientation; | |
public Divider(Context context, int orientation) { | |
mDivider = ContextCompat.getDrawable(context, R.drawable.divider); | |
if (orientation != LinearLayoutManager.VERTICAL) { | |
throw new IllegalArgumentException("This Item Decoration can be used only with a RecyclerView that uses a LinearLayoutManager with vertical orientation"); | |
} | |
mOrientation = orientation; | |
} | |
@Override | |
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { | |
if (mOrientation == LinearLayoutManager.VERTICAL) { | |
drawHorizontalDivider(c, parent, state); | |
} | |
} | |
private void drawHorizontalDivider(Canvas c, RecyclerView parent, RecyclerView.State state) { | |
int left, top, right, bottom; | |
left = parent.getPaddingLeft(); | |
right = parent.getWidth() - parent.getPaddingRight(); | |
int count = parent.getChildCount(); | |
for (int i = 0; i < count; i++) { | |
View current = parent.getChildAt(i); | |
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) current.getLayoutParams(); | |
top = current.getBottom() + params.bottomMargin; | |
bottom = top + mDivider.getIntrinsicHeight(); | |
mDivider.setBounds(left, top, right, bottom); | |
mDivider.draw(c); | |
} | |
} | |
@Override | |
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { | |
if (mOrientation == LinearLayoutManager.VERTICAL) { | |
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight()); | |
} | |
} | |
} |
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
public class ItemOffsetDecoration extends RecyclerView.ItemDecoration { | |
private int mItemOffset; | |
private int spanCount; | |
public ItemOffsetDecoration(int itemOffset) { | |
mItemOffset = itemOffset; | |
} | |
public ItemOffsetDecoration(@NonNull Context context, @DimenRes int itemOffsetId, int spanCount) { | |
this(context.getResources().getDimensionPixelSize(itemOffsetId)); | |
this.spanCount = spanCount; | |
} | |
@Override | |
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, | |
@NonNull RecyclerView.State state) { | |
int position = parent.getChildAdapterPosition(view); // item position | |
int column = position % spanCount; // item column | |
outRect.left = mItemOffset - column * mItemOffset / spanCount; // spacing - column * ((1f / spanCount) * spacing) | |
outRect.right = (column + 1) * mItemOffset / spanCount; // (column + 1) * ((1f / spanCount) * spacing) | |
if (position < spanCount) { // top edge | |
outRect.top = mItemOffset; | |
} | |
outRect.bottom = mItemOffset; // item bottom | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment