MarkoMilos / Paginate

Library for creating simple pagination functionality upon RecyclerView and AbsListView
Apache License 2.0
1.35k stars 225 forks source link

onAttachedToRecyclerView & onDetachedFromRecyclerView not called #36

Closed yangsuni1024 closed 4 years ago

yangsuni1024 commented 6 years ago

My RecyclerViewAdapter used onAttachedToRecyclerView and onDetachedFromRecyclerView Method.

But WrapperAdapter in RecyclerPaginate not called it.

MarkoMilos commented 4 years ago

If you are using a loading row (which is set by default) then your source adapter will be wrapped with WrapperAdapter.

The issue that you are mentioning is related to this architectural decision. Your source adapter is not directly attached and observed by RecyclerView - the WrapperAdapter is.

That leads to immediate attaching and then detaching from RecyclerView which you can see in the logs e.g.:

paginate D/MyAdapter: onAttachedToRecyclerView 
paginate D/MyAdapter: onDetachedFromRecyclerView

If you do not use the loading row this won't be the case since your source adapter won't be wrapped. You can set up like following:

 Paginate.with(recyclerView, this)
            .addLoadingListItem(false)
            .build();

Next, that will be released soon will propagate attach/detach call from WrapperAdapter to the source adapter but this won't completely solve the issue since the adapter will be:

  1. attached
  2. detached and wrapped
  3. attached again

By doing so you will have a proper state that the adapter is actually "attached" but this but the glitch could be that you see one iteration of detaching then attaching when Paginate is setup. Depending on what you are doing in these callbacks this might or might not be an issue for you.

In logs you will see something like:

paginate D/MyAdapter: onAttachedToRecyclerView
paginate D/MyAdapter: onDetachedFromRecyclerView
paginate D/MyAdapter: onAttachedToRecyclerView

And when paginate.unbind() is called, source adapter is swapped back to RecyclerView so calls will be:

paginate D/MyAdapter: onDetachedFromRecyclerView
paginate D/MyAdapter: onAttachedToRecyclerView
MarkoMilos commented 4 years ago

The solution mentioned in the comment above has been implemented in the latest release (check release 1.0.0)

I'm closing this issue for now, feel free to reopen it with API suggestion for solving this problem or better yet pull request.