Skip to content

Commit 8929028

Browse files
committed
Add support GridLayoutManager for itemSpace
1 parent 2a7fc4d commit 8929028

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

omegarecyclerviewlibs/src/main/java/com/omega_r/libs/omegarecyclerview/item_decoration/SpaceItemDecoration.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.omega_r.libs.omegarecyclerview.item_decoration;
22

33
import android.graphics.Rect;
4-
import androidx.annotation.NonNull;
5-
import androidx.recyclerview.widget.RecyclerView;
64

75
import com.omega_r.libs.omegarecyclerview.item_decoration.decoration_helpers.DividerDecorationHelper;
86

7+
import androidx.annotation.NonNull;
8+
import androidx.recyclerview.widget.GridLayoutManager;
9+
import androidx.recyclerview.widget.RecyclerView;
10+
911
public class SpaceItemDecoration extends BaseItemDecoration {
1012

1113
private final int space;
@@ -18,8 +20,16 @@ public SpaceItemDecoration(int showDivider, int space) {
1820
@Override
1921
void getItemOffset(@NonNull Rect outRect, @NonNull RecyclerView parent,
2022
@NonNull DividerDecorationHelper helper, int position, int itemCount) {
21-
if (isShowBeginDivider() && position < 1 || position >= 1) helper.setStart(outRect, space);
22-
if (isShowEndDivider() && position == itemCount - 1) helper.setEnd(outRect, space);
23+
int countBeginEndPositions = getCountBeginEndPositions(parent);
24+
if (isShowBeginDivider() || countBeginEndPositions <= position) helper.setStart(outRect, space);
25+
if (isShowEndDivider() && position == itemCount - countBeginEndPositions) helper.setEnd(outRect, space);
26+
}
27+
28+
private int getCountBeginEndPositions(RecyclerView recyclerView) {
29+
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
30+
if (layoutManager instanceof GridLayoutManager) {
31+
return ((GridLayoutManager) layoutManager).getSpanCount();
32+
} else return 1;
2333
}
2434

2535
}

0 commit comments

Comments
 (0)