Created
July 13, 2017 11:43
-
-
Save Miha-x64/4e8754e72a1e65e3ca742744ea501f16 to your computer and use it in GitHub Desktop.
RecyclerView decoration which adds indentation in the beginning and in the end of content.
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 net.aquadc.commonandroid.lists; | |
import android.graphics.Rect; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.View; | |
public class FirstLastItemSpacesDecoration extends RecyclerView.ItemDecoration { | |
private final int directSpace; | |
private final int reverseSpace; | |
public FirstLastItemSpacesDecoration(int space, boolean layoutReversed) { | |
if (layoutReversed) { | |
directSpace = 0; | |
reverseSpace = space; | |
} else { | |
directSpace = space; | |
reverseSpace = 0; | |
} | |
} | |
@Override | |
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { | |
if (parent.getChildAdapterPosition(view) == state.getItemCount()-1) { | |
outRect.bottom = directSpace; | |
outRect.top = reverseSpace; | |
} | |
if (parent.getChildAdapterPosition(view) == 0) { | |
outRect.bottom = reverseSpace; | |
outRect.top = directSpace; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment