tivi icon indicating copy to clipboard operation
tivi copied to clipboard

Cannot keep the scroll position of horizontal nested RecyclerView in DiscoverFragment

Open Nghicv opened this issue 6 years ago • 5 comments

Cannot keep the scroll position of horizontal nested RecyclerView in DiscoverFragment when I navigate to "navigation_trending" then back to DiscoverFragment

Nghicv avatar Oct 06 '19 08:10 Nghicv

Related: https://github.com/airbnb/epoxy/issues/561

chrisbanes avatar Oct 06 '19 12:10 chrisbanes

When I scroll up/down the position state is kept. I think It related to Navigation I created a pull request to fix this issue, please take a look at it https://github.com/chrisbanes/tivi/pull/482

Nghicv avatar Oct 06 '19 14:10 Nghicv

Re-opening since we had to revert in #485

chrisbanes avatar Oct 07 '19 23:10 chrisbanes

@chrisbanes As I checked, Sometimes, the view is bound before we call onRestoreInstanceState, this lead to the crash. from BaseEpoxyAdapter:

public void onRestoreInstanceState(@Nullable Bundle inState) {
    // To simplify things we enforce that state is restored before views are bound, otherwise it
    // is more difficult to update view state once they are bound
    if (boundViewHolders.size() > 0) {
      throw new IllegalStateException(
          "State cannot be restored once views have been bound. It should be done before adding "
              + "the adapter to the recycler view.");
    }

    if (inState != null) {
      viewHolderState = inState.getParcelable(SAVED_STATE_ARG_VIEW_HOLDERS);
      if (viewHolderState == null) {
        throw new IllegalStateException(
            "Tried to restore instance state, but onSaveInstanceState was never called.");
      }
    }
  }

I don't know why it happens. But, I think we can call onRestoreInstanceState safely by catching IllegalStateException exception when calling onRestoreInstanceState. @chrisbanes how do you think?. If It is ok, I will open a new pull request to fix the issue. Thank you

Nghicv avatar Oct 08 '19 02:10 Nghicv

Do you have any update for this issue? I face the same problems :( @chrisbanes

thanhnamitit avatar Apr 27 '20 11:04 thanhnamitit