Created
July 24, 2019 19:15
-
-
Save pavelsust/d54f73f830716ab53085b3a2748a68c9 to your computer and use it in GitHub Desktop.
ItemDecoration for recycleview horizontal and vertical
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
package com.utils; | |
import android.graphics.Rect; | |
import android.support.v7.widget.GridLayoutManager; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.View; | |
public class EqualSpacingItemDecoration extends RecyclerView.ItemDecoration { | |
private final int spacing; | |
private int displayMode; | |
public static final int HORIZONTAL = 0; | |
public static final int VERTICAL = 1; | |
public static final int GRID = 2; | |
public EqualSpacingItemDecoration(int spacing) { | |
this(spacing, -1); | |
} | |
public EqualSpacingItemDecoration(int spacing, int displayMode) { | |
this.spacing = spacing; | |
this.displayMode = displayMode; | |
} | |
@Override | |
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { | |
int position = parent.getChildViewHolder(view).getAdapterPosition(); | |
int itemCount = state.getItemCount(); | |
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager(); | |
setSpacingForDirection(outRect, layoutManager, position, itemCount); | |
} | |
private void setSpacingForDirection(Rect outRect, | |
RecyclerView.LayoutManager layoutManager, | |
int position, | |
int itemCount) { | |
// Resolve display mode automatically | |
if (displayMode == -1) { | |
displayMode = resolveDisplayMode(layoutManager); | |
} | |
switch (displayMode) { | |
case HORIZONTAL: | |
outRect.left = spacing; | |
outRect.right = position == itemCount - 1 ? spacing : 0; | |
outRect.top = spacing; | |
outRect.bottom = spacing; | |
break; | |
case VERTICAL: | |
outRect.left = spacing; | |
outRect.right = spacing; | |
outRect.top = spacing; | |
outRect.bottom = position == itemCount - 1 ? spacing : 0; | |
break; | |
case GRID: | |
if (layoutManager instanceof GridLayoutManager) { | |
GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager; | |
int cols = gridLayoutManager.getSpanCount(); | |
int rows = itemCount / cols; | |
outRect.left = spacing; | |
outRect.right = position % cols == cols - 1 ? spacing : 0; | |
outRect.top = spacing; | |
outRect.bottom = position / cols == rows - 1 ? spacing : 0; | |
} | |
break; | |
} | |
} | |
private int resolveDisplayMode(RecyclerView.LayoutManager layoutManager) { | |
if (layoutManager instanceof GridLayoutManager) return GRID; | |
if (layoutManager.canScrollHorizontally()) return HORIZONTAL; | |
return VERTICAL; | |
} | |
} | |
/// item decoration use in layout | |
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext()); | |
recyclerView.addItemDecoration(new EqualSpacingItemDecoration(30, EqualSpacingItemDecoration.VERTICAL)); | |
recyclerView.setLayoutManager(linearLayoutManager); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment