-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPaginationScrollListener.java
More file actions
41 lines (33 loc) · 1.43 KB
/
PaginationScrollListener.java
File metadata and controls
41 lines (33 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
public class PaginationScrollListener extends RecyclerView.OnScrollListener {
@NonNull
private final IOnNextScrollListener listener;
@NonNull
private int pageSize;
public PaginationScrollListener(IOnNextScrollListener listener, int pageSize) {
this.listener = listener;
this.pageSize = pageSize;
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
if (manager instanceof LinearLayoutManager) {
LinearLayoutManager layoutManager = (LinearLayoutManager) manager;
int visibleItemCount = layoutManager.getChildCount();
int totalItemCount = layoutManager.getItemCount();
int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
if ((visibleItemCount + firstVisibleItemPosition) >= totalItemCount
&& firstVisibleItemPosition >= 0
&& totalItemCount >= pageSize) {
listener.onGetNext();
}
}
}
}