Skip to content

Commit c5fbc6b

Browse files
committed
Initial commit
1 parent 9ddf880 commit c5fbc6b

File tree

12 files changed

+254
-9
lines changed

12 files changed

+254
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ captures/
3939
.idea/gradle.xml
4040
.idea/dictionaries
4141
.idea/libraries
42+
.idea/
4243

4344
# Keystore files
4445
*.jks

.idea/caches/build_file_checksums.ser

0 Bytes
Binary file not shown.

app/src/main/java/com/github/donglua/epoxy/more/demo/DemoActivity.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
import android.support.v7.app.AppCompatActivity;
44
import android.os.Bundle;
5+
import android.support.v7.widget.LinearLayoutManager;
56
import android.support.v7.widget.RecyclerView;
67

78
public class DemoActivity extends AppCompatActivity {
9+
private DemoEpoxyController demoEpoxyController;
10+
private LoadMoreRecyclerViewScrollListener loadMoreScrollListener;
811

912
@Override
1013
protected void onCreate(Bundle savedInstanceState) {
@@ -13,9 +16,42 @@ protected void onCreate(Bundle savedInstanceState) {
1316

1417
RecyclerView recyclerView = findViewById(R.id.recycler_view);
1518

16-
DemoEpoxyController demoEpoxyController = new DemoEpoxyController();
19+
final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
20+
21+
recyclerView.setLayoutManager(layoutManager);
22+
loadMoreScrollListener = new LoadMoreRecyclerViewScrollListener(layoutManager) {
23+
@Override
24+
public void fetchNextPage() {
25+
fetchNextPageByApi();
26+
}
27+
};
28+
recyclerView.addOnScrollListener(loadMoreScrollListener);
29+
demoEpoxyController = new DemoEpoxyController(loadMoreScrollListener);
30+
1731
recyclerView.setAdapter(demoEpoxyController.getAdapter());
1832

1933
demoEpoxyController.requestModelBuild();
2034
}
35+
36+
private void fetchNextPageByApi() {
37+
new Thread(new Runnable() {
38+
@Override
39+
public void run() {
40+
try {
41+
Thread.sleep(1000);
42+
} catch (InterruptedException ignore) { }
43+
44+
runOnUiThread(new Runnable() {
45+
@Override
46+
public void run() {
47+
demoEpoxyController.nextPzge();
48+
49+
if (demoEpoxyController.getPage() > 4) {
50+
loadMoreScrollListener.setHasMoreToLoad(false);
51+
}
52+
}
53+
});
54+
}
55+
}).start();
56+
}
2157
}
Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,47 @@
11
package com.github.donglua.epoxy.more.demo;
22

3+
import com.airbnb.epoxy.AutoModel;
34
import com.airbnb.epoxy.EpoxyController;
5+
import com.airbnb.epoxy.EpoxyModel;
46

57
public class DemoEpoxyController extends EpoxyController {
8+
9+
private int page = 1;
10+
11+
private LoadMoreRecyclerViewScrollListener listener;
12+
13+
@AutoModel LoadingView_ loadingView;
14+
15+
public DemoEpoxyController(LoadMoreRecyclerViewScrollListener listener) {
16+
this.listener = listener;
17+
}
18+
619
@Override
720
protected void buildModels() {
821
int count = 0;
9-
for (int i = 0; i < 20; i++) {
10-
++count;
11-
new DemoItemView_().id(i).text("Item " + count).addTo(this);
22+
for (int i = 0; i < page; i++) {
23+
24+
for (int j = 0; j < 20; j++) {
25+
++count;
26+
new DemoItemView_().id(count).text("Item " + count).addTo(this);
27+
}
28+
1229
}
30+
31+
loadingView.addIf(new EpoxyModel.AddPredicate() {
32+
@Override
33+
public boolean addIf() {
34+
return listener.hasMoreToLoad();
35+
}
36+
}, this);
37+
}
38+
39+
void nextPzge() {
40+
++page;
41+
requestModelBuild();
42+
}
43+
44+
public int getPage() {
45+
return page;
1346
}
1447
}

app/src/main/java/com/github/donglua/epoxy/more/demo/DemoItemView.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.github.donglua.epoxy.more.demo;
22

3-
import android.content.Context;
43
import android.support.annotation.NonNull;
54
import android.view.View;
65
import android.widget.TextView;
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package com.github.donglua.epoxy.more.demo;
2+
3+
import android.support.v7.widget.GridLayoutManager;
4+
import android.support.v7.widget.LinearLayoutManager;
5+
import android.support.v7.widget.RecyclerView;
6+
import android.support.v7.widget.StaggeredGridLayoutManager;
7+
8+
/**
9+
* @See https://gist.github.com/nesquena/d09dc68ff07e845cc622
10+
*/
11+
public abstract class EndlessRecyclerViewScrollListener extends RecyclerView.OnScrollListener {
12+
// The minimum amount of items to have below your current scroll position
13+
// before loading more.
14+
private int visibleThreshold = 5;
15+
// The current offset index of data you have loaded
16+
private int currentPage = 0;
17+
// The total number of items in the dataset after the last load
18+
private int previousTotalItemCount = 0;
19+
// True if we are still waiting for the last set of data to load.
20+
private boolean loading = true;
21+
// Sets the starting page index
22+
private int startingPageIndex = 0;
23+
24+
private RecyclerView.LayoutManager mLayoutManager;
25+
26+
public EndlessRecyclerViewScrollListener(LinearLayoutManager layoutManager) {
27+
this.mLayoutManager = layoutManager;
28+
}
29+
30+
public EndlessRecyclerViewScrollListener(GridLayoutManager layoutManager) {
31+
this.mLayoutManager = layoutManager;
32+
visibleThreshold = visibleThreshold * layoutManager.getSpanCount();
33+
}
34+
35+
public EndlessRecyclerViewScrollListener(StaggeredGridLayoutManager layoutManager) {
36+
this.mLayoutManager = layoutManager;
37+
visibleThreshold = visibleThreshold * layoutManager.getSpanCount();
38+
}
39+
40+
public int getLastVisibleItem(int[] lastVisibleItemPositions) {
41+
int maxSize = 0;
42+
for (int i = 0; i < lastVisibleItemPositions.length; i++) {
43+
if (i == 0) {
44+
maxSize = lastVisibleItemPositions[i];
45+
}
46+
else if (lastVisibleItemPositions[i] > maxSize) {
47+
maxSize = lastVisibleItemPositions[i];
48+
}
49+
}
50+
return maxSize;
51+
}
52+
53+
// This happens many times a second during a scroll, so be wary of the code you place here.
54+
// We are given a few useful parameters to help us work out if we need to load some more data,
55+
// but first we check if we are waiting for the previous load to finish.
56+
@Override
57+
public void onScrolled(RecyclerView view, int dx, int dy) {
58+
int lastVisibleItemPosition = 0;
59+
int totalItemCount = mLayoutManager.getItemCount();
60+
61+
if (mLayoutManager instanceof StaggeredGridLayoutManager) {
62+
int[] lastVisibleItemPositions = ((StaggeredGridLayoutManager) mLayoutManager).findLastVisibleItemPositions(null);
63+
// get maximum element within the list
64+
lastVisibleItemPosition = getLastVisibleItem(lastVisibleItemPositions);
65+
} else if (mLayoutManager instanceof GridLayoutManager) {
66+
lastVisibleItemPosition = ((GridLayoutManager) mLayoutManager).findLastVisibleItemPosition();
67+
} else if (mLayoutManager instanceof LinearLayoutManager) {
68+
lastVisibleItemPosition = ((LinearLayoutManager) mLayoutManager).findLastVisibleItemPosition();
69+
}
70+
71+
// If the total item count is zero and the previous isn't, assume the
72+
// list is invalidated and should be reset back to initial state
73+
if (totalItemCount < previousTotalItemCount) {
74+
this.currentPage = this.startingPageIndex;
75+
this.previousTotalItemCount = totalItemCount;
76+
if (totalItemCount == 0) {
77+
this.loading = true;
78+
}
79+
}
80+
// If it’s still loading, we check to see if the dataset count has
81+
// changed, if so we conclude it has finished loading and update the current page
82+
// number and total item count.
83+
if (loading && (totalItemCount > previousTotalItemCount)) {
84+
loading = false;
85+
previousTotalItemCount = totalItemCount;
86+
}
87+
88+
// If it isn’t currently loading, we check to see if we have breached
89+
// the visibleThreshold and need to reload more data.
90+
// If we do need to reload some more data, we execute onLoadMore to fetch the data.
91+
// threshold should reflect how many total columns there are too
92+
if (!loading && (lastVisibleItemPosition + visibleThreshold) > totalItemCount) {
93+
currentPage++;
94+
onLoadMore(currentPage, totalItemCount, view);
95+
loading = true;
96+
}
97+
}
98+
99+
// Call this method whenever performing new searches
100+
public void resetState() {
101+
this.currentPage = this.startingPageIndex;
102+
this.previousTotalItemCount = 0;
103+
this.loading = true;
104+
}
105+
106+
// Defines the process for actually loading more data based on page
107+
public abstract void onLoadMore(int page, int totalItemsCount, RecyclerView view);
108+
109+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.github.donglua.epoxy.more.demo;
2+
3+
public interface LoadMoreListener {
4+
5+
boolean hasMoreToLoad();
6+
7+
void fetchNextPage();
8+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.github.donglua.epoxy.more.demo;
2+
3+
import android.support.v7.widget.GridLayoutManager;
4+
import android.support.v7.widget.LinearLayoutManager;
5+
import android.support.v7.widget.RecyclerView;
6+
import android.support.v7.widget.StaggeredGridLayoutManager;
7+
8+
public abstract class LoadMoreRecyclerViewScrollListener extends EndlessRecyclerViewScrollListener implements LoadMoreListener {
9+
10+
private boolean isHasMoreToLoad = true;
11+
12+
public LoadMoreRecyclerViewScrollListener(LinearLayoutManager layoutManager) {
13+
super(layoutManager);
14+
}
15+
16+
public LoadMoreRecyclerViewScrollListener(GridLayoutManager layoutManager) {
17+
super(layoutManager);
18+
}
19+
20+
public LoadMoreRecyclerViewScrollListener(StaggeredGridLayoutManager layoutManager) {
21+
super(layoutManager);
22+
}
23+
24+
@Override
25+
public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
26+
if (hasMoreToLoad()) {
27+
fetchNextPage();
28+
}
29+
}
30+
31+
@Override
32+
public boolean hasMoreToLoad() {
33+
return isHasMoreToLoad;
34+
}
35+
36+
public void setHasMoreToLoad(boolean hasMore) {
37+
isHasMoreToLoad = hasMore;
38+
}
39+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.github.donglua.epoxy.more.demo;
2+
3+
import android.view.View;
4+
5+
import com.airbnb.epoxy.EpoxyModel;
6+
import com.airbnb.epoxy.EpoxyModelClass;
7+
8+
@EpoxyModelClass(layout = R.layout.item_loading)
9+
public abstract class LoadingView extends EpoxyModel<View> {
10+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="wrap_content"
5+
android:orientation="horizontal"
6+
android:gravity="center"
7+
android:padding="16dp">
8+
9+
<ProgressBar
10+
android:layout_width="32dp"
11+
android:layout_height="32dp" />
12+
13+
</LinearLayout>

app/src/main/res/layout/item_text.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
android:orientation="vertical"
55
android:id="@+id/textView"
66
android:padding="16dp"
7+
android:background="@android:color/white"
78
android:gravity="center_vertical"
89
android:layout_width="match_parent"
910
android:layout_height="wrap_content"

build.gradle

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ buildscript {
88
}
99
dependencies {
1010
classpath 'com.android.tools.build:gradle:3.1.2'
11-
12-
13-
// NOTE: Do not place your application dependencies here; they belong
14-
// in the individual module build.gradle files
1511
}
1612
}
1713

0 commit comments

Comments
 (0)