bgogetap / StickyHeaders

Easily add Sticky Headers to your RecyclerView
Apache License 2.0
521 stars 88 forks source link

Lib incompatible with FragmentContainerView #101

Closed mobilekosmos closed 4 years ago

mobilekosmos commented 4 years ago

The lib seems to be incompatible with the new FragmentContainerView that should be used instead of the deprecated "fragment". We are getting an illegal state exception while scrolling:

java.lang.IllegalStateException: Views added to a FragmentContainerView must be associated with a Fragment. View android.widget.LinearLayout{7e6e6e1 I.E...... ......ID 0,0-0,0 #7f0901af app:id/header_view} is not associated with a Fragment. at androidx.fragment.app.FragmentContainerView.addView(FragmentContainerView.java:276) at android.view.ViewGroup.addView(ViewGroup.java:4868) at android.view.ViewGroup.addView(ViewGroup.java:4841) at com.brandongogetap.stickyheaders.StickyHeaderPositioner.attachHeader(StickyHeaderPositioner.java:242) at com.brandongogetap.stickyheaders.StickyHeaderPositioner.updateHeaderState(StickyHeaderPositioner.java:73) at com.brandongogetap.stickyheaders.StickyLayoutManager.scrollVerticallyBy(StickyLayoutManager.java:95) at androidx.recyclerview.widget.RecyclerView.scrollStep(RecyclerView.java:1841) at androidx.recyclerview.widget.RecyclerView.scrollByInternal(RecyclerView.java:1940) at androidx.recyclerview.widget. 2020-03-27 03:17:43.909 22570-22570/com.sophos.smsec E/AndroidRuntime: at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:7915) at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:7876) at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:8091) at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:219) at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method) at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:199) at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:8042) at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:8120) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:972) at android.view.Choreographer.doCallbacks(Choreographer.java:796) at android.view.Choreographer.doFrame(Choreographer.java:724) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:957) at android.os.Handler.handleCallback(Handler.java:907)

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical">

    <include layout="@layout/generic_toolbar_collapsing" />

    <!-- FrameLayout needed as workaround to StickyHeader issue https://github.com/bgogetap/StickyHeaders/issues/54 -->
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <!-- RecyclerView parent must be either a FrameLayout or CoordinatorLayout when using the StickyHeaders lib. -->

<!--        <androidx.fragment.app.FragmentContainerView DOES NOT WORK YET -->
        <fragment
            android:id="@+id/list_fragment"
            class="com.myapp..."
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:layout="@android:layout/list_content" />
    </FrameLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
bgogetap commented 4 years ago

What does your Fragment layout look like?

RecyclerView should be inside a FrameLayout in your Fragment. You wouldn't need the wrapping FrameLayout in the XML you posted in that case.

mobilekosmos commented 4 years ago

"RecyclerView should be inside a FrameLayout in your Fragment." It doesn't matter if the FrameLayout is here or in the fragment!

mobilekosmos commented 4 years ago

And to answer your question, the fragment is only a recyclerView.

bgogetap commented 4 years ago

If your Fragment layout was a FrameLayout that held a RecyclerView, then everything would be fine.

You shouldn't be dependent on the host view of your Fragment, even without using the FragmentContainerView.

mobilekosmos commented 4 years ago

"If your Fragment layout was a FrameLayout that held a RecyclerView, then everything would be fine." Why do you say this? At the end the result is the same! The parent of the recyclerView is always the FrameLayout, it's doesn't matter where you put the frameLayout. And about dependency, also this lib shouldn't be dependent on using a frameLayout as a container to start with^^

bgogetap commented 4 years ago

Why do you say this? At the end the result is the same!

The end result is the sticky header won't be added to the FragmentContainerView, which is what is throwing the exception.

And about dependency, also this lib shouldn't be dependent on using a frameLayout as a container to start with^^

Thank you for your feedback. Feel free to fork this library or use another if it doesn't fit your needs.