-
-
Save MichaelHackett/691d6a2917b62697882549f9f13daaf8 to your computer and use it in GitHub Desktop.
A RecyclerView.ItemDecoration that adds 8 dp padding to the top of the first and bottom of the last item in a list (or RecyclerView in this case).
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.Rect; | |
import android.support.annotation.NonNull; | |
import android.support.v7.widget.RecyclerView; | |
import android.util.DisplayMetrics; | |
import android.util.TypedValue; | |
import android.view.View; | |
/** | |
* Adds 8dp padding to the top of the first and the bottom of the last item in the list, | |
* as specified in https://www.google.com/design/spec/components/lists.html#lists-specs | |
*/ | |
public class ListPaddingDecoration extends RecyclerView.ItemDecoration { | |
private final static int PADDING_IN_DIPS = 8; | |
private final int mPadding; | |
public ListPaddingDecoration(@NonNull Context context) { | |
DisplayMetrics metrics = context.getResources().getDisplayMetrics(); | |
mPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, PADDING_IN_DIPS, metrics); | |
} | |
@Override | |
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { | |
final int itemPosition = parent.getChildAdapterPosition(view); | |
if (itemPosition == RecyclerView.NO_POSITION) { | |
return; | |
} | |
if (itemPosition == 0) { | |
outRect.top = mPadding; | |
} | |
final RecyclerView.Adapter adapter = parent.getAdapter(); | |
if ((adapter != null) && (itemPosition == adapter.getItemCount() - 1)) { | |
outRect.bottom = mPadding; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment