Skip to content

Commit c650025

Browse files
Merge pull request #136 from Omega-R/develop
Fixed itemStace and dividers
2 parents e6701e0 + 05698f2 commit c650025

File tree

3 files changed

+109
-5
lines changed

3 files changed

+109
-5
lines changed

omegarecyclerviewlibs/src/main/java/com/omega_r/libs/omegarecyclerview/OmegaRecyclerView.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,8 @@ public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
610610

611611
public abstract static class Adapter<VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> {
612612

613-
private RecyclerView recyclerView;
613+
@Nullable
614+
protected RecyclerView recyclerView;
614615

615616
public boolean isDividerAllowedAbove(int position) {
616617
return true;

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

Lines changed: 19 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,21 @@ 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+
if (countBeginEndPositions > 1) {
28+
if (position % countBeginEndPositions != 0 || isShowBeginDivider()) helper.setOtherStart(outRect, space);
29+
if (position / (countBeginEndPositions - 1) > 0 && isShowEndDivider()) helper.setOtherEnd(outRect, space);
30+
}
31+
}
32+
33+
private int getCountBeginEndPositions(RecyclerView recyclerView) {
34+
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
35+
if (layoutManager instanceof GridLayoutManager) {
36+
return ((GridLayoutManager) layoutManager).getSpanCount();
37+
} else return 1;
2338
}
2439

2540
}

omegarecyclerviewlibs/src/main/java/com/omega_r/libs/omegarecyclerview/item_decoration/decoration_helpers/DividerDecorationHelper.java

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ public static DividerDecorationHelper getHelper(int orientation, RecyclerView pa
4646

4747
public abstract int getEnd(Rect rect);
4848

49+
public abstract void setOtherStart(Rect rect, int start);
50+
51+
public abstract void setOtherEnd(Rect rect, int end);
52+
53+
public abstract int getOtherStart(Rect rect);
54+
55+
public abstract int getOtherEnd(Rect rect);
56+
4957
public int getOffset(int offset) {
5058
return offset;
5159
}
@@ -78,6 +86,26 @@ public int getEnd(Rect rect) {
7886
return rect.bottom;
7987
}
8088

89+
@Override
90+
public void setOtherStart(Rect rect, int start) {
91+
rect.left = start;
92+
}
93+
94+
@Override
95+
public void setOtherEnd(Rect rect, int end) {
96+
rect.right = end;
97+
}
98+
99+
@Override
100+
public int getOtherStart(Rect rect) {
101+
return rect.left;
102+
}
103+
104+
@Override
105+
public int getOtherEnd(Rect rect) {
106+
return rect.right;
107+
}
108+
81109
}
82110

83111
class ReverseVerticalDividerDecorationHelper extends DividerDecorationHelper {
@@ -106,6 +134,26 @@ public int getEnd(Rect rect) {
106134
return rect.top;
107135
}
108136

137+
@Override
138+
public void setOtherStart(Rect rect, int start) {
139+
rect.right = start;
140+
}
141+
142+
@Override
143+
public void setOtherEnd(Rect rect, int end) {
144+
rect.left = end;
145+
}
146+
147+
@Override
148+
public int getOtherStart(Rect rect) {
149+
return rect.right;
150+
}
151+
152+
@Override
153+
public int getOtherEnd(Rect rect) {
154+
return rect.left;
155+
}
156+
109157
@Override
110158
public int getOffset(int offset) {
111159
return -offset;
@@ -139,6 +187,26 @@ public int getEnd(Rect rect) {
139187
return rect.right;
140188
}
141189

190+
@Override
191+
public void setOtherStart(Rect rect, int start) {
192+
rect.top = start;
193+
}
194+
195+
@Override
196+
public void setOtherEnd(Rect rect, int end) {
197+
rect.bottom = end;
198+
}
199+
200+
@Override
201+
public int getOtherStart(Rect rect) {
202+
return rect.top;
203+
}
204+
205+
@Override
206+
public int getOtherEnd(Rect rect) {
207+
return rect.bottom;
208+
}
209+
142210
}
143211

144212
class ReverseHorizontalDividerDecorationHelper extends DividerDecorationHelper {
@@ -167,6 +235,26 @@ public int getEnd(Rect rect) {
167235
return rect.left;
168236
}
169237

238+
@Override
239+
public void setOtherStart(Rect rect, int start) {
240+
rect.bottom = start;
241+
}
242+
243+
@Override
244+
public void setOtherEnd(Rect rect, int end) {
245+
rect.top = end;
246+
}
247+
248+
@Override
249+
public int getOtherStart(Rect rect) {
250+
return rect.bottom;
251+
}
252+
253+
@Override
254+
public int getOtherEnd(Rect rect) {
255+
return rect.top;
256+
}
257+
170258
@Override
171259
public int getOffset(int offset) {
172260
return -offset;

0 commit comments

Comments
 (0)