turing-tech / MaterialScrollBar

An Android library that brings the Material Design 5.1 sidebar to pre-5.1 devices.
Apache License 2.0
778 stars 126 forks source link

AdapterNotSetupForIndicatorException on fragment recreation #33

Closed hanggrian closed 7 years ago

hanggrian commented 8 years ago

I have created a RecyclerView adapter implementing INameableAdapter for DragScrollBar. It works flawlessly during first creation of fragment in ViewPager. However when I swipe 2 fragments to the left/right, the fragment is destroyed, therefore it will be automatically recreated. This message pops out during those recreation progress: com.turingtechnologies.materialscrollbar.CustomExceptions$AdapterNotSetupForIndicatorException: In order to add this indicator, the adapter for your recyclerView MUST implement INameableAdapter.

Currently I have to stop fragment recreation with mViewPager.setOffscreenPageLimit(...) to avoid crashing.

turing-tech commented 8 years ago

That's very odd. Would you mind posting the relevant code from the fragment in question so I can get a look at what might be happening/try and replicate?

hanggrian commented 8 years ago

I've found out that app:recyclerView="@+id/recyclerView" attribute in com.turingtechnologies.materialscrollbar.DragScrollBar is the cause of the error. It might work well in an Activity of only 1 RecyclerView with id recyclerView. However, in an Activity with multiple fragments of which each fragment contains RecyclerView with the same ids, DragScrollBar could not determine which RecyclerView to bind.

My immediate solution is to give unique id to the particular RecyclerView with DragScrollBar.

turing-tech commented 8 years ago

Okay, awesome. I'll look into other solutions internally to avoid this problem.

naushad-madakiya commented 7 years ago

@hendraanggrian how did you achieve this by giving unique id for recyclerView? because I am using single fragment 2 times in ViewPager and facing the same issue.

hanggrian commented 7 years ago

@naushad-madakiya I was using different fragment in each page of ViewPager.

If you are using the same fragment 2 times, then you would have duplicate RecyclerView ids, which I believe is yet to be supported with this library. But I think you can have abstract method for using different ids each time a fragment is created (no id defined in xml), something like this:

public abstract MyFragment extends Fragment {

    @IdRes public abstract int getRecyclerViewId();

    public View onCreateView(...) {
        ViewGroup parent = inflate();
        if(int i=0; i<parent.getChildCount(); i++) {
            if(parent.getChildAt(i) instanceof RecyclerView) {
                parent.getChildAt(i).setId(getRecyclerViewId());
                // and set DragScrollBar's recyclerview id programatically
            }
        }
        return parent;
    }
}

This is just some idea, I haven't tested it yet.

naushad-madakiya commented 7 years ago

@hendraanggrian thanks, I will check that and let you know what happens.