Skip to content

Commit 72e3626

Browse files
committed
Add support to GridLayoutManager
1 parent 264572c commit 72e3626

File tree

4 files changed

+23
-8
lines changed

4 files changed

+23
-8
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ buildscript {
33
jcenter()
44
}
55
dependencies {
6-
classpath 'com.android.tools.build:gradle:2.3.0'
6+
classpath 'com.android.tools.build:gradle:2.3.3'
77
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7'
88
}
99
}

gravitysnaphelper/src/main/java/com/github/rubensousa/gravitysnaphelper/GravityDelegate.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import android.os.Build;
55
import android.support.annotation.NonNull;
6+
import android.support.v7.widget.GridLayoutManager;
67
import android.support.v7.widget.LinearLayoutManager;
78
import android.support.v7.widget.OrientationHelper;
89
import android.support.v7.widget.RecyclerView;
@@ -150,6 +151,11 @@ private View findStartView(RecyclerView.LayoutManager layoutManager,
150151

151152
if (layoutManager instanceof LinearLayoutManager) {
152153
int firstChild = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
154+
int offset = 1;
155+
156+
if (layoutManager instanceof GridLayoutManager) {
157+
offset += ((GridLayoutManager) layoutManager).getSpanCount() - 1;
158+
}
153159

154160
if (firstChild == RecyclerView.NO_POSITION) {
155161
return null;
@@ -174,7 +180,7 @@ private View findStartView(RecyclerView.LayoutManager layoutManager,
174180
// to avoid having the last item not completely visible.
175181
boolean endOfList = ((LinearLayoutManager) layoutManager)
176182
.findLastCompletelyVisibleItemPosition()
177-
== layoutManager.getItemCount() - 1;
183+
>= layoutManager.getItemCount() - offset;
178184

179185
if (visibleWidth > 0.5f && !endOfList) {
180186
return child;
@@ -185,7 +191,7 @@ private View findStartView(RecyclerView.LayoutManager layoutManager,
185191
} else {
186192
// If the child wasn't returned, we need to return
187193
// the next view close to the start.
188-
return layoutManager.findViewByPosition(firstChild + 1);
194+
return layoutManager.findViewByPosition(firstChild + offset);
189195
}
190196
}
191197

@@ -197,6 +203,11 @@ private View findEndView(RecyclerView.LayoutManager layoutManager,
197203

198204
if (layoutManager instanceof LinearLayoutManager) {
199205
int lastChild = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
206+
int offset = 1;
207+
208+
if (layoutManager instanceof GridLayoutManager) {
209+
offset += ((GridLayoutManager) layoutManager).getSpanCount() - 1;
210+
}
200211

201212
if (lastChild == RecyclerView.NO_POSITION) {
202213
return null;
@@ -217,7 +228,7 @@ private View findEndView(RecyclerView.LayoutManager layoutManager,
217228
// If we're at the start of the list, we shouldn't snap
218229
// to avoid having the first item not completely visible.
219230
boolean startOfList = ((LinearLayoutManager) layoutManager)
220-
.findFirstCompletelyVisibleItemPosition() == 0;
231+
.findFirstCompletelyVisibleItemPosition() <= offset - 1;
221232

222233
if (visibleWidth > 0.5f && !startOfList) {
223234
return child;
@@ -227,7 +238,7 @@ private View findEndView(RecyclerView.LayoutManager layoutManager,
227238
return null;
228239
} else {
229240
// If the child wasn't returned, we need to return the previous view
230-
return layoutManager.findViewByPosition(lastChild - 1);
241+
return layoutManager.findViewByPosition(lastChild - offset);
231242
}
232243
}
233244
return null;

gravitysnaphelper/src/main/java/com/github/rubensousa/gravitysnaphelper/GravityPagerSnapHelper.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
import android.support.annotation.NonNull;
55
import android.support.annotation.Nullable;
6+
import android.support.v7.widget.GridLayoutManager;
7+
import android.support.v7.widget.LinearLayoutManager;
68
import android.support.v7.widget.PagerSnapHelper;
79
import android.support.v7.widget.RecyclerView;
810
import android.view.View;
@@ -27,6 +29,11 @@ public GravityPagerSnapHelper(int gravity, boolean enableSnapLastItem,
2729
@Override
2830
public void attachToRecyclerView(@Nullable RecyclerView recyclerView)
2931
throws IllegalStateException {
32+
if (!(recyclerView.getLayoutManager() instanceof LinearLayoutManager)
33+
|| recyclerView.getLayoutManager() instanceof GridLayoutManager) {
34+
throw new IllegalStateException("GravityPagerSnapHelper needs a RecyclerView" +
35+
" with a LinearLayoutManager");
36+
}
3037
delegate.attachToRecyclerView(recyclerView);
3138
super.attachToRecyclerView(recyclerView);
3239
}

gravitysnaphelper/src/main/java/com/github/rubensousa/gravitysnaphelper/GravitySnapHelper.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,8 @@
1919

2020
import android.support.annotation.NonNull;
2121
import android.support.annotation.Nullable;
22-
import android.support.v7.widget.LinearLayoutManager;
2322
import android.support.v7.widget.LinearSnapHelper;
24-
import android.support.v7.widget.OrientationHelper;
2523
import android.support.v7.widget.RecyclerView;
26-
import android.view.Gravity;
2724
import android.view.View;
2825

2926

0 commit comments

Comments
 (0)