Skip to content

Commit ee29a4a

Browse files
committed
Improve on Scroll double currentPage when shimmer running
1 parent 468dbd2 commit ee29a4a

File tree

2 files changed

+53
-38
lines changed

2 files changed

+53
-38
lines changed

app/src/main/java/com/ramanaptr/sample/MainActivity.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ class MainActivity : AppCompatActivity() {
218218

219219
// Example handling data in single layout
220220
val dataList = arrayListOf<SampleData>().apply {
221+
222+
// if (currentPage >= 2) return@apply // limit the page for test stop populate the data
221223
for (i in 1..size) {
222224
add(SampleData("Key $i", "Value $i"))
223225
}
@@ -257,7 +259,7 @@ class MainActivity : AppCompatActivity() {
257259
// populate the data list
258260
val dataList = arrayListOf<SampleData>().apply {
259261

260-
// if (currentPage >= 2) return@apply // limit the page for test stop populate the data
262+
// if (currentPage >= 2) return@apply // limit the page for test stop populate the data
261263
for (i in 1..size) {
262264

263265
// example handling data using ezViewType in layout 2
@@ -272,7 +274,7 @@ class MainActivity : AppCompatActivity() {
272274
// example handling data using ezViewType in layout 1
273275
// you must implement ezViewType because Ez-Recycler must to know attach the data into view you assign
274276
val sampleDataOne =
275-
SampleData("Page ${currentPage + 1} \nKey $i", "\nValue $i")
277+
SampleData("Page ${currentPage + 1} \n\nKey $i", "\n\nValue $i")
276278
sampleDataOne.ezViewType = EzViewType.LAYOUT_1
277279
add(sampleDataOne)
278280
}

ez-recyleview/src/main/java/com/ramanaptr/widget/EzRecyclerView.java

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class EzRecyclerView<Data extends EzBaseData> extends RecyclerView {
3838
private EzPaginationListener ezPaginationListener;
3939
private boolean isFirstLoadEzRecyclerView = true;
4040
private boolean isRecyclerViewLoading = false;
41+
private boolean isShimmerRunning = false;
4142
private int tempShimmerSize = 0;
4243
private int startShimmerSize = 0;
4344
private int endShimmerSize = 0;
@@ -72,7 +73,7 @@ private void initComponent() {
7273

7374
public void destroy() {
7475
flagEzRecyclerViewFirstLoad();
75-
flagOnLoading();
76+
flagOnStartLoading();
7677
removeOnScrollListener(onScrollListener);
7778
ezPaginationListener = null;
7879
baseAdapter = null;
@@ -87,7 +88,7 @@ public void destroy() {
8788
}
8889

8990
/**
90-
* we'll delete this method soon or not support
91+
* we'll delete this method soon or not support in the future
9192
* Use instead #setData(Class)
9293
*/
9394
@Deprecated
@@ -173,14 +174,15 @@ public void startShimmer(int shimmerSize) {
173174
data.setEzViewType(EzViewType.SHIMMER_EFFECT);
174175
shimmer.add(data);
175176
}
176-
addAll(shimmer);
177177
flagEzRecyclerViewFirstLoadDone();
178-
flagOnLoading();
178+
flagOnStartLoading();
179+
flagShimmerStart();
180+
addAll(shimmer);
179181
}
180182
}
181183

182184
/**
183-
* we'll delete this method soon or not support
185+
* we'll delete this method soon or not support in the future
184186
* Use instead #startShimmer(int) and use #setData(Class) before you start for shimmer effect
185187
*/
186188
@Deprecated
@@ -197,81 +199,90 @@ public void startShimmer(int shimmerSize, Data data) {
197199
data.setEzViewType(EzViewType.SHIMMER_EFFECT);
198200
shimmer.add(data);
199201
}
200-
addAll(shimmer);
201202
flagEzRecyclerViewFirstLoadDone();
202-
flagOnLoading();
203+
flagOnStartLoading();
204+
addAll(shimmer);
203205
}
204206
}
205207

206208
public void hideShimmer() {
207209
if (endShimmerSize > 0) {
208-
baseAdapter.removeRange(startShimmerSize, (endShimmerSize + startShimmerSize), this);
210+
baseAdapter.removeRange(startShimmerSize, (endShimmerSize + startShimmerSize));
209211
endShimmerSize = 0;
210212
endShimmerSize = tempShimmerSize;
211213
flagEzRecyclerViewFirstLoad();
214+
flagShimmerStop();
215+
216+
// Remove all views when data list is zero
217+
if (baseAdapter.getDataList().size() <= 0) {
218+
removeAllViews();
219+
}
212220
}
213221
}
214222

215223
private void settingAnimator() {
216224
}
217225

218226
public void add(@NonNull Data data) {
219-
flagCompleteLoading();
227+
flagStopLoading();
220228
baseAdapter.add(data);
221229
}
222230

223231
public void addAll(@NonNull List<Data> dataList) {
224-
if (dataList.size() <= 0) {
225-
flagOnLoading();
226-
currentPage = 0;
227-
return;
228-
}
229-
flagCompleteLoading();
230-
baseAdapter.addAll(dataList);
232+
post(() -> {
233+
if (dataList.size() <= 0) {
234+
flagOnStartLoading();
235+
currentPage = 0;
236+
return;
237+
}
238+
flagStopLoading();
239+
baseAdapter.addAll(dataList);
240+
});
231241
}
232242

233243
public void replace(@NonNull Data data) {
234-
flagCompleteLoading();
244+
flagStopLoading();
235245
baseAdapter.replace(data);
236246
}
237247

238248
public void replaceAll(@NonNull List<Data> dataList) {
239249
if (dataList.size() <= 0) {
240-
flagOnLoading();
250+
flagOnStartLoading();
241251
currentPage = 0;
242252
return;
243253
}
244-
flagCompleteLoading();
254+
flagStopLoading();
245255
removeAllViews();
246256
baseAdapter.replaceAll(dataList);
247257
}
248258

249259
public void remove(@NonNull int position) {
250-
flagCompleteLoading();
260+
flagStopLoading();
251261
removeViewAt(position);
252262
baseAdapter.remove(position);
253263
}
254264

255265
public void remove(@NonNull Data data) {
256-
flagCompleteLoading();
266+
flagStopLoading();
257267
baseAdapter.remove(data);
258268
}
259269

260270
public void removeAll() {
261-
flagCompleteLoading();
271+
flagStopLoading();
272+
removeAllViews();
262273
baseAdapter.removeAll();
263274
}
264275

265276
public void refresh() {
266-
flagCompleteLoading();
277+
flagStopLoading();
267278
baseAdapter.refresh();
268279
}
269280

270281
public void resetAllViewsAndShimmer() {
271282
offset = 0;
272283
limit = 0;
273284
endShimmerSize = tempShimmerSize;
274-
flagOnLoading();
285+
flagOnStartLoading();
275286
flagEzRecyclerViewFirstLoad();
276287
removeAll();
277288
removeAllViews();
@@ -316,7 +327,7 @@ public void onScrolled(@NotNull RecyclerView recyclerView, int horizontalScrollS
316327
pastVisibleItems = mLayoutManager.findFirstVisibleItemPosition();
317328
}
318329

319-
if (!isRecyclerViewLoading) {
330+
if (!isRecyclerViewLoading && !isShimmerRunning) {
320331
if ((visibleItemCount + pastVisibleItems) >= totalItemCount) {
321332
offset += limit;
322333
currentPage++;
@@ -342,7 +353,7 @@ public void onScrolled(@NotNull RecyclerView recyclerView, int horizontalScrollS
342353
pastVisibleItems = mLayoutManager.findFirstVisibleItemPosition();
343354
}
344355

345-
if (!isRecyclerViewLoading) {
356+
if (!isRecyclerViewLoading && !isShimmerRunning) {
346357
if ((visibleItemCount + pastVisibleItems) >= totalItemCount) {
347358
offset += limit;
348359
currentPage++;
@@ -362,11 +373,19 @@ private void flagEzRecyclerViewFirstLoadDone() {
362373
isFirstLoadEzRecyclerView = false;
363374
}
364375

365-
public void flagCompleteLoading() {
376+
public void flagStopLoading() {
366377
isRecyclerViewLoading = false;
367378
}
368379

369-
public void flagOnLoading() {
380+
public void flagOnStartLoading() {
381+
isShimmerRunning = true;
382+
}
383+
384+
public void flagShimmerStop() {
385+
isShimmerRunning = false;
386+
}
387+
388+
public void flagShimmerStart() {
370389
isRecyclerViewLoading = true;
371390
}
372391

@@ -499,14 +518,8 @@ public void removeRange(int min, int max) {
499518
notifyDataSetChanged();
500519
}
501520

502-
public void removeRange(int min, int max, EzRecyclerView<Data> ezRecyclerView) {
503-
this.dataList.removeRange(min, max);
504-
notifyDataSetChanged();
505-
506-
// Remove all views when data list is zero
507-
if (dataList.size() <= 0) {
508-
ezRecyclerView.removeAllViews();
509-
}
521+
public ListData<Data> getDataList() {
522+
return dataList;
510523
}
511524

512525
public void refresh() {

0 commit comments

Comments
 (0)