Created
May 25, 2015 15:18
-
-
Save ryanamaral/93b5bd95412baf85a81a to your computer and use it in GitHub Desktop.
RecyclerView.ItemDecoration implementation for equal padding between the list items except first and last items.
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
<resources> | |
<dimen name="paddingItemDecorationDefault">10dp</dimen> | |
<dimen name="paddingItemDecorationEdge">20dp</dimen> | |
</resources> |
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
/** | |
* ItemDecoration implementation for equal padding between the list items except first and last items. | |
* @author Ryan Amaral | |
* @version 1.0 | |
*/ | |
public class PaddingItemDecoration extends RecyclerView.ItemDecoration { | |
private int mPaddingPx; | |
private int mPaddingEdgesPx; | |
public PaddingItemDecoration(Activity activity) { | |
final Resources resources = activity.getResources(); | |
mPaddingPx = (int) resources.getDimension(R.dimen.paddingItemDecorationDefault); | |
mPaddingEdgesPx = (int) resources.getDimension(R.dimen.paddingItemDecorationEdge); | |
} | |
@Override | |
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { | |
super.getItemOffsets(outRect, view, parent, state); | |
final int itemPosition = parent.getChildAdapterPosition(view); | |
if (itemPosition == RecyclerView.NO_POSITION) { | |
return; | |
} | |
int orientation = getOrientation(parent); | |
final int itemCount = state.getItemCount(); | |
int left = 0; | |
int top = 0; | |
int right = 0; | |
int bottom = 0; | |
/** HORIZONTAL */ | |
if (orientation == LinearLayoutManager.HORIZONTAL) { | |
/** all positions */ | |
left = mPaddingPx; | |
right = mPaddingPx; | |
/** first position */ | |
if (itemPosition == 0) { | |
left += mPaddingEdgesPx; | |
} | |
/** last position */ | |
else if (itemCount > 0 && itemPosition == itemCount - 1) { | |
right += mPaddingEdgesPx; | |
} | |
} | |
/** VERTICAL */ | |
else { | |
/** all positions */ | |
top = mPaddingPx; | |
bottom = mPaddingPx; | |
/** first position */ | |
if (itemPosition == 0) { | |
top += mPaddingEdgesPx; | |
} | |
/** last position */ | |
else if (itemCount > 0 && itemPosition == itemCount - 1) { | |
bottom += mPaddingEdgesPx; | |
} | |
} | |
if (!isReverseLayout(parent)) { | |
outRect.set(left, top, right, bottom); | |
} else { | |
outRect.set(right, bottom, left, top); | |
} | |
} | |
private boolean isReverseLayout(RecyclerView parent) { | |
if (parent.getLayoutManager() instanceof LinearLayoutManager) { | |
LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager(); | |
return layoutManager.getReverseLayout(); | |
} else { | |
throw new IllegalStateException("PaddingItemDecoration can only be used with a LinearLayoutManager."); | |
} | |
} | |
private int getOrientation(RecyclerView parent) { | |
if (parent.getLayoutManager() instanceof LinearLayoutManager) { | |
LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager(); | |
return layoutManager.getOrientation(); | |
} else { | |
throw new IllegalStateException("PaddingItemDecoration can only be used with a LinearLayoutManager."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment