2020-07-01 17:08:28 +00:00
|
|
|
package awais.instagrabber.customviews.helpers;
|
|
|
|
|
|
|
|
import android.graphics.Rect;
|
|
|
|
import android.view.View;
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
|
|
|
|
public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {
|
2021-05-07 11:34:31 +00:00
|
|
|
private final int halfSpace;
|
|
|
|
|
|
|
|
private boolean hasHeader;
|
2020-07-01 17:08:28 +00:00
|
|
|
|
|
|
|
public GridSpacingItemDecoration(int spacing) {
|
2021-05-07 11:34:31 +00:00
|
|
|
halfSpace = spacing / 2;
|
2020-07-01 17:08:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
|
2021-05-07 11:34:31 +00:00
|
|
|
if (hasHeader && parent.getChildAdapterPosition(view) == 0) {
|
|
|
|
outRect.bottom = halfSpace;
|
|
|
|
outRect.left = -halfSpace;
|
|
|
|
outRect.right = -halfSpace;
|
|
|
|
return;
|
|
|
|
}
|
2020-10-17 10:07:03 +00:00
|
|
|
if (parent.getPaddingLeft() != halfSpace) {
|
2021-05-07 11:34:31 +00:00
|
|
|
parent.setPadding(halfSpace, hasHeader ? 0 : halfSpace, halfSpace, halfSpace);
|
2020-10-17 10:07:03 +00:00
|
|
|
parent.setClipToPadding(false);
|
2020-07-01 17:08:28 +00:00
|
|
|
}
|
2020-10-17 10:07:03 +00:00
|
|
|
outRect.top = halfSpace;
|
|
|
|
outRect.bottom = halfSpace;
|
|
|
|
outRect.left = halfSpace;
|
|
|
|
outRect.right = halfSpace;
|
2020-07-01 17:08:28 +00:00
|
|
|
}
|
2021-05-07 11:34:31 +00:00
|
|
|
|
|
|
|
public void setHasHeader(final boolean hasHeader) {
|
|
|
|
this.hasHeader = hasHeader;
|
|
|
|
}
|
2020-07-01 17:08:28 +00:00
|
|
|
}
|