Created
June 27, 2019 03:42
-
-
Save flyfire/15268675a0bf5a950244c660428ea00a to your computer and use it in GitHub Desktop.
[GridItemDecoration] #RecyclerView #ItemDecoration
This file contains hidden or 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
| import android.content.Context; | |
| import android.graphics.Canvas; | |
| import android.graphics.Rect; | |
| import android.graphics.drawable.Drawable; | |
| import android.view.View; | |
| import androidx.recyclerview.widget.GridLayoutManager; | |
| import androidx.recyclerview.widget.RecyclerView; | |
| public class GridItemDecoration extends RecyclerView.ItemDecoration { | |
| private final Drawable mDivider; | |
| public GridItemDecoration(Context context, Drawable divider) { | |
| mDivider = divider; | |
| } | |
| @Override | |
| public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { | |
| super.onDraw(c, parent, state); | |
| drawVertical(c, parent); | |
| drawHorizontal(c, parent); | |
| } | |
| private void drawVertical(Canvas c, RecyclerView parent) { | |
| int childCount = parent.getChildCount(); | |
| for (int i = 0; i < childCount; i++) { | |
| View child = parent.getChildAt(i); | |
| RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); | |
| int left = child.getRight() + params.rightMargin; | |
| int top = child.getTop() - params.topMargin; | |
| int right = left + mDivider.getIntrinsicWidth(); | |
| int bottom = child.getBottom() + params.bottomMargin; | |
| mDivider.setBounds(left, top, right, bottom); | |
| mDivider.draw(c); | |
| } | |
| } | |
| private void drawHorizontal(Canvas c, RecyclerView parent) { | |
| int childCount = parent.getChildCount(); | |
| for (int i = 0; i < childCount; i++) { | |
| View child = parent.getChildAt(i); | |
| RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); | |
| int left = child.getLeft() - params.leftMargin; | |
| int top = child.getBottom() + params.bottomMargin; | |
| int right = child.getRight() + params.rightMargin; | |
| int bottom = top + mDivider.getMinimumHeight(); | |
| mDivider.setBounds(left, top, right, bottom); | |
| mDivider.draw(c); | |
| } | |
| } | |
| @Override | |
| public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) { | |
| super.getItemOffsets(outRect, itemPosition, parent); | |
| int right = mDivider.getIntrinsicWidth(); | |
| int bottom = mDivider.getIntrinsicHeight(); | |
| if (isLastSpan(itemPosition, parent)) { | |
| right = 0; | |
| } | |
| if (isLastRow(itemPosition, parent)) { | |
| bottom = 0; | |
| } | |
| outRect.set(0, 0, right, bottom); | |
| } | |
| public boolean isLastRow(int itemPosition, RecyclerView parent) { | |
| RecyclerView.LayoutManager layoutManager = parent.getLayoutManager(); | |
| if (layoutManager instanceof GridLayoutManager) { | |
| int spanCount = ((GridLayoutManager) layoutManager).getSpanCount(); | |
| int itemCount = parent.getAdapter().getItemCount(); | |
| if ((itemCount - itemPosition - 1) < spanCount) | |
| return true; | |
| } | |
| return false; | |
| } | |
| public boolean isLastSpan(int itemPosition, RecyclerView parent) { | |
| RecyclerView.LayoutManager layoutManager = parent.getLayoutManager(); | |
| if (layoutManager instanceof GridLayoutManager) { | |
| int spanCount = ((GridLayoutManager) layoutManager).getSpanCount(); | |
| if ((itemPosition + 1) % spanCount == 0) | |
| return true; | |
| } | |
| return false; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment