lsjwzh / RecyclerViewPager

Deprecated
Apache License 2.0
3.53k stars 667 forks source link

Cannot change whether this adapter has stable IDs while the adapter has registered observers. #83

Open MrVilkaman opened 8 years ago

MrVilkaman commented 8 years ago

I used next code in my fragment

if (adapter == null) {
    adapter = new TargetAdapter();
    getPresenter().loadTarget();
}
viewPager.setAdapter(adapter);

When I open new fragment and returned back i got exception in last line:

 java.lang.IllegalStateException: Cannot change whether this adapter has stable IDs while the adapter has registered observers.
                      at android.support.v7.widget.RecyclerView$Adapter.setHasStableIds(RecyclerView.java:5535)
                      at com.lsjwzh.widget.recyclerviewpager.RecyclerViewPagerAdapter.setHasStableIds(RecyclerViewPagerAdapter.java:57)
                      at com.lsjwzh.widget.recyclerviewpager.RecyclerViewPagerAdapter.<init>(RecyclerViewPagerAdapter.java:21)
                      at com.lsjwzh.widget.recyclerviewpager.RecyclerViewPager.ensureRecyclerViewPagerAdapter(RecyclerViewPager.java:495)
                      at com.lsjwzh.widget.recyclerviewpager.RecyclerViewPager.setAdapter(RecyclerViewPager.java:123)
                      at ...targetlist.TargetlistScreenFragment.setupRecyclerView(TargetlistScreenFragment.java:61)

When I used normal RecyclerView my code works well!

justincpollard commented 8 years ago

I encountered the same problem yesterday. While this may be a bug in the library, why do you need to set the adapter again? I assume the setAdapter() call in is onResume ... why not in onCreate?

MrVilkaman commented 8 years ago

@justincpollard I do it in onCreateView. When I was come back from another fragment, I have new instance of RecyclerViewPager but old instance of adapter

justincpollard commented 8 years ago

Aw, I see. Well you could just create a new adapter to back the new RecyclerViewPager, right? If you're loading something from network maybe you could do that in onCreate, then simply set those items on the new adapter in onCreateView?

MrVilkaman commented 8 years ago

@justincpollard I can do so, but my approach is works in the usual RecyclerView.

lsjwzh commented 8 years ago

I will check it ~~~ Thx for your issue!

walee-balogun commented 8 years ago

I'm experiencing the same issue, any luck ?

java.lang.IllegalStateException: Cannot change whether this adapter has stable IDs while the adapter has registered observers. at android.support.v7.widget.RecyclerView$Adapter.setHasStableIds(RecyclerView.java:5535) at com.lsjwzh.widget.recyclerviewpager.RecyclerViewPagerAdapter.setHasStableIds(RecyclerViewPagerAdapter.java:57) at com.lsjwzh.widget.recyclerviewpager.RecyclerViewPagerAdapter.(RecyclerViewPagerAdapter.java:21) at com.lsjwzh.widget.recyclerviewpager.RecyclerViewPager.ensureRecyclerViewPagerAdapter(RecyclerViewPager.java:495) at com.lsjwzh.widget.recyclerviewpager.RecyclerViewPager.setAdapter(RecyclerViewPager.java:123) at ...targetlist.TargetlistScreenFragment.setupRecyclerView(TargetlistScreenFragment.java:61)

tmgsca commented 8 years ago

Also having the same problem. I'm currently using Android Annotations to inject the adapter so i can't really control it's lifecycle.

KishorFlutter commented 7 years ago

What if we replace ensureRecyclerViewPagerAdapter(...) function

@SuppressWarnings("unchecked")
    @NonNull
    protected RecyclerViewPagerAdapter ensureRecyclerViewPagerAdapter(Adapter adapter) {
        return (adapter instanceof RecyclerViewPagerAdapter)
                ? (RecyclerViewPagerAdapter) adapter
                : new RecyclerViewPagerAdapter(this, adapter);

    }

with

@SuppressWarnings("unchecked")
    @NonNull
    protected RecyclerViewPagerAdapter ensureRecyclerViewPagerAdapter(Adapter adapter) {
        return new RecyclerViewPagerAdapter(this, adapter);
    }

I think it will create new Adapter every time.

ncapdevi commented 7 years ago

Getting the same issues when using the RecyclerViewPager as a nested horizontal row within a traditional vertical RecyclerView, when it goes through and rebinds the row, it hits this issue.

4ndro1d commented 7 years ago

Any solutions on this issue?

lumyus commented 7 years ago

Facing the same issue when extending a class with recyclerviewpager, that was previously extending a regular recyclerview. Any updates?

lumyus commented 7 years ago

I got a workaround that did the trick for me. Remove the following line from the class that extends RecyclerViewPager: @Override public void setHasStableIds(boolean hasStableIds) { origin.setHasStableIds(hasStableIds); } Have a good one and thanks @lsjwzh for the library!