Basically My problem is not with DiffUtil & AsyncListDiffer but the issue is in the LayoutManager item count. Please look at my code:
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
isScrolled = true;
}
}
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
currentItem = manager.getChildCount();
totalItem = manager.getItemCount();
scrolledOutItems = manager.findFirstVisibleItemPosition();
if (isScrolled && currentItem + scrolledOutItems == totalItem) {
progress.setVisibility(View.VISIBLE);
isScrolled = false;
fetchData();
}
}
});
private void fetchData() {
new Handler(Looper.myLooper()).postDelayed(() -> {
setData();
adapter.submitList(arrayList);
progress.setVisibility(View.GONE);
}, 1000);
}
void setData() {
arrayList.add(new VideoModel(UUID.randomUUID(),"https://xxxxxxx/rap-324.mp4"));
arrayList.add(new VideoModel(UUID.randomUUID(), "https://xxxxxxxx/ythori.mp4",));
arrayList.add(new VideoModel(UUID.randomUUID(), "https://xxxxxxxx/minee.mp4"));
arrayList.add(new VideoModel(UUID.randomUUID(), "https://xxxxxxxx/standw.mp4"));
}
Behavior I'm getting:
- For the first 2-3 scroll it is updating the data properly.
- But after that when I'm scrolling down the
totalItemCount
is not matchingcurrentItem
&scrolledOutItems
.
Note :
Yes I have unique items because each item has UUID.
Yes my item model class has
equals()
method.Yes I have properly implemented DifUtils and AsyncListDffer.
I can share my Items counts in onScrolled Method :
I have created a log
inside onScorlled()
method like this: Log.d("COUNT","scrolledOutItems :"+scrolledOutItems+" "+"currentItem :"+currentItem+" "+"totalItem :"+totalItem);
and the out put is->
Case 1:
scrolledOutItems :14 currentItem :2 totalItem :16
scrolledOutItems :14 currentItem :2 totalItem :16
scrolledOutItems :14 currentItem :3 totalItem :20
scrolledOutItems :14 currentItem :3 totalItem :20
scrolledOutItems :14 currentItem :3 totalItem :24
scrolledOutItems :14 currentItem :3 totalItem :24
Look the the above log as I'm adding 4 items every time it was working fine until total items 16 means total 16/4=4 scrolls, but after adding next 4 items total items became 20 but suddenly how is the next 4 items added?
Case 2:
scrolledOutItems :2 currentItem :2 totalItem :4
scrolledOutItems :2 currentItem :3 totalItem :8
scrolledOutItems :2 currentItem :3 totalItem :12
Same here also.
But if I scrolling up again then scrolling down it is working,I can clearly say the issue is in the item count.