Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.content.Context;
- import android.graphics.Rect;
- import android.support.annotation.DimenRes;
- import android.support.annotation.NonNull;
- import android.support.v7.widget.RecyclerView;
- import android.view.View;
- /**
- * Created by wrewolf on 13.02.18.
- */
- public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {
- private int columnCount;
- private int f_spasing;
- private int h_spasing;
- public GridSpacingItemDecoration(@NonNull Context context, @DimenRes int itemOffsetId, @NonNull int columnCount) {
- this(context.getResources().getDimensionPixelSize(itemOffsetId), columnCount);
- }
- public GridSpacingItemDecoration(int spacing, int columnCount) {
- this.columnCount = columnCount;
- this.f_spasing = spacing;
- this.h_spasing = spacing / 2;
- }
- @Override
- public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
- super.getItemOffsets(outRect, view, parent, state);
- int position = parent.getChildAdapterPosition(view); // item position
- outRect.top = 0;
- outRect.left = 0;
- outRect.right = 0;
- outRect.bottom = 0;
- if (position > 0) {
- outRect.top = f_spasing;
- if (this.columnCount == 2) {
- if (position % 2 == 0) {
- outRect.left = h_spasing;
- outRect.right = f_spasing;
- } else {
- outRect.left = f_spasing;
- outRect.right = h_spasing;
- }
- } else {
- if (position % columnCount == 0) {
- outRect.left = h_spasing;
- outRect.right = f_spasing;
- } else if (position % columnCount == 1) {
- outRect.left = f_spasing;
- outRect.right = h_spasing;
- } else {
- outRect.left = h_spasing;
- outRect.right = h_spasing;
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment