1
- /*
2
- * Copyright (c) 2015 Spotify AB
3
- *
4
- * Licensed to the Apache Software Foundation (ASF) under one
5
- * or more contributor license agreements. See the NOTICE file
6
- * distributed with this work for additional information
7
- * regarding copyright ownership. The ASF licenses this file
8
- * to you under the Apache License, Version 2.0 (the
9
- * "License"); you may not use this file except in compliance
10
- * with the License. You may obtain a copy of the License at
11
- *
12
- * http://www.apache.org/licenses/LICENSE-2.0
13
- *
14
- * Unless required by applicable law or agreed to in writing,
15
- * software distributed under the License is distributed on an
16
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17
- * KIND, either express or implied. See the License for the
18
- * specific language governing permissions and limitations
19
- * under the License.
20
- */
21
-
22
-
23
1
package com .spotify .sdk .android ;
24
2
25
3
import android .support .v7 .widget .LinearLayoutManager ;
@@ -32,7 +10,8 @@ public abstract class ResultListScrollListener extends RecyclerView.OnScrollList
32
10
33
11
private final LinearLayoutManager mLayoutManager ;
34
12
35
- private int mItemCount ;
13
+ private static final int SCROLL_BUFFER = 3 ;
14
+ private int mCurrentItemCount = 0 ;
36
15
37
16
private boolean mAwaitingItems = true ;
38
17
@@ -41,7 +20,7 @@ public ResultListScrollListener(LinearLayoutManager layoutManager) {
41
20
}
42
21
43
22
public void reset () {
44
- mItemCount = 0 ;
23
+ mCurrentItemCount = 0 ;
45
24
}
46
25
47
26
@ Override
@@ -51,14 +30,14 @@ public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
51
30
int itemCount = mLayoutManager .getItemCount ();
52
31
int itemPosition = mLayoutManager .findLastVisibleItemPosition ();
53
32
54
- if (mAwaitingItems && itemCount > mItemCount ) {
55
- mItemCount = itemCount ;
33
+ if (mAwaitingItems && itemCount > mCurrentItemCount ) {
34
+ mCurrentItemCount = itemCount ;
56
35
mAwaitingItems = false ;
57
36
}
58
37
59
- Log .d (TAG , String .format ("loading %s, item count: %s/%s, itemPosition %s" , mAwaitingItems , mItemCount , itemCount , itemPosition ));
38
+ Log .d (TAG , String .format ("loading %s, item count: %s/%s, itemPosition %s" , mAwaitingItems , mCurrentItemCount , itemCount , itemPosition ));
60
39
61
- if (!mAwaitingItems && ( itemCount <= itemPosition + 1 ) ) {
40
+ if (!mAwaitingItems && itemPosition + 1 >= itemCount - SCROLL_BUFFER ) {
62
41
mAwaitingItems = true ;
63
42
onLoadMore ();
64
43
}
0 commit comments