AdapterDataObserver's onItemRangeInsterted() is never called when using Paginate
hendraanggrian opened this issue · comments
I have setup a repository for this issue.
recyclerView.getAdapter().registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
Toast.makeText(MainActivity.this, "inserted", Toast.LENGTH_SHORT).show();
}
@Override
public void onItemRangeRemoved(int positionStart, int itemCount) {
Toast.makeText(MainActivity.this, "removed", Toast.LENGTH_SHORT).show();
}
});
Basically, when using this library, I am not able to receive notification when items in adapter are added using AdapterDataObserver
. onItemRangeRemoved()
and onItemRangeChanged()
are triggered, but not onItemRangeInserted()
.
I assume this is because Paginate has changed the adapter set to the RecyclerView. If so, is there a way to get the actual adapter from Paginate?
Hi, I cannot access the repository that you have provided on the link above.
I will try to answer. Yes, if you are using a loading row (which is used by default) your original adapter that is set on RecyclerView will be wrapped with WrapperAdapter which adds a loading row for you.
If you call recyclerView.getAdapter().registerAdapterDataObserver()
after Paginate initialization you will get an instance of that WrapperAdapter
instead your adapter.
You can always register AdapterDataObserver on instance of your adapter since that is something that you have control over.
Anyhow, I could not reproduce the issue that you are stating. I registered observers on both original and wrapped adapter. When I add items to the original adapter and call originalAdapter.notifyItemRangeInserted(start, itemCount)
both observer gets the call as expected. The same goes for removal.
Make sure that you call notifyItemRangeInserted
and not notifyDataSetChanged
if you want the observer to be called.
Here are some logs for a sample that have 2 items initially and load 2 items for each page.
paginate D/Observer: WRAPPED INSERTED: posStart: 2 count: 2
paginate D/Observer: ORIGINAL INSERTED: posStart: 2 count: 2
paginate D/Observer: WRAPPED INSERTED: posStart: 4 count: 2
paginate D/Observer: ORIGINAL INSERTED: posStart: 4 count: 2
paginate D/Observer: WRAPPED INSERTED: posStart: 6 count: 2
paginate D/Observer: ORIGINAL INSERTED: posStart: 6 count: 2
paginate D/Observer: WRAPPED INSERTED: posStart: 8 count: 2
paginate D/Observer: ORIGINAL INSERTED: posStart: 8 count: 2
paginate D/Observer: WRAPPED REMOVED: posStart: 4 count: 1
paginate D/Observer: ORIGINAL REMOVED: posStart: 4 count: 1
paginate D/Observer: WRAPPED INSERTED: posStart: 9 count: 2
paginate D/Observer: ORIGINAL INSERTED: posStart: 9 count: 2
I'm closing this issue for now. Feel free to reopen with example to reproduce it.