3
3
4
4
import android .os .Build ;
5
5
import android .support .annotation .NonNull ;
6
+ import android .support .v7 .widget .GridLayoutManager ;
6
7
import android .support .v7 .widget .LinearLayoutManager ;
7
8
import android .support .v7 .widget .OrientationHelper ;
8
9
import android .support .v7 .widget .RecyclerView ;
@@ -150,6 +151,11 @@ private View findStartView(RecyclerView.LayoutManager layoutManager,
150
151
151
152
if (layoutManager instanceof LinearLayoutManager ) {
152
153
int firstChild = ((LinearLayoutManager ) layoutManager ).findFirstVisibleItemPosition ();
154
+ int offset = 1 ;
155
+
156
+ if (layoutManager instanceof GridLayoutManager ) {
157
+ offset += ((GridLayoutManager ) layoutManager ).getSpanCount () - 1 ;
158
+ }
153
159
154
160
if (firstChild == RecyclerView .NO_POSITION ) {
155
161
return null ;
@@ -174,7 +180,7 @@ private View findStartView(RecyclerView.LayoutManager layoutManager,
174
180
// to avoid having the last item not completely visible.
175
181
boolean endOfList = ((LinearLayoutManager ) layoutManager )
176
182
.findLastCompletelyVisibleItemPosition ()
177
- == layoutManager .getItemCount () - 1 ;
183
+ >= layoutManager .getItemCount () - offset ;
178
184
179
185
if (visibleWidth > 0.5f && !endOfList ) {
180
186
return child ;
@@ -185,7 +191,7 @@ private View findStartView(RecyclerView.LayoutManager layoutManager,
185
191
} else {
186
192
// If the child wasn't returned, we need to return
187
193
// the next view close to the start.
188
- return layoutManager .findViewByPosition (firstChild + 1 );
194
+ return layoutManager .findViewByPosition (firstChild + offset );
189
195
}
190
196
}
191
197
@@ -197,6 +203,11 @@ private View findEndView(RecyclerView.LayoutManager layoutManager,
197
203
198
204
if (layoutManager instanceof LinearLayoutManager ) {
199
205
int lastChild = ((LinearLayoutManager ) layoutManager ).findLastVisibleItemPosition ();
206
+ int offset = 1 ;
207
+
208
+ if (layoutManager instanceof GridLayoutManager ) {
209
+ offset += ((GridLayoutManager ) layoutManager ).getSpanCount () - 1 ;
210
+ }
200
211
201
212
if (lastChild == RecyclerView .NO_POSITION ) {
202
213
return null ;
@@ -217,7 +228,7 @@ private View findEndView(RecyclerView.LayoutManager layoutManager,
217
228
// If we're at the start of the list, we shouldn't snap
218
229
// to avoid having the first item not completely visible.
219
230
boolean startOfList = ((LinearLayoutManager ) layoutManager )
220
- .findFirstCompletelyVisibleItemPosition () == 0 ;
231
+ .findFirstCompletelyVisibleItemPosition () <= offset - 1 ;
221
232
222
233
if (visibleWidth > 0.5f && !startOfList ) {
223
234
return child ;
@@ -227,7 +238,7 @@ private View findEndView(RecyclerView.LayoutManager layoutManager,
227
238
return null ;
228
239
} else {
229
240
// 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 );
231
242
}
232
243
}
233
244
return null ;
0 commit comments