bgogetap / StickyHeaders

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

Sticky header is not removed when RecyclerView is gone #56

Closed bonnyfone closed 6 years ago

bonnyfone commented 7 years ago

Is there any clean way to hide/remove the sticky header when the RecyclerView's visibility changes?

bgogetap commented 7 years ago

Currently, no. The header id is R.id.header_view so you can look for that and, if not null, hide it.

I think this is a good enhancement and shouldn't be hard to implement in the library using an OnGlobalLayoutListener. I'll get this set up to be automatic in a release sometime this weekend.

JayNewstrom commented 7 years ago

Could you instead hide the parent layout, rather than the RecyclerView?

bonnyfone commented 6 years ago

Cool, @bgogetap thanks for the tip!

bonnyfone commented 6 years ago

Btw it can be a bit tricky to hide the "proper" header if you have multiple RecyclerViews with StickyHeaders under the same parent (e.g. a TabLayout switching between different lists)

vunder commented 6 years ago

Have the same problem. Single activity that show single fragment. One of there fragmets is recycler view with sticky headers. I try to hide header when fragment is detached. However this works only the first time. In case I'm switching back to recycler view fragment and then switching to another fragment - header is not hiding. @Override public void onDetach() { getActivity().findViewById(R.id.header_view).setVisibility(View.GONE); super.onDetach(); }

bgogetap commented 6 years ago

Fixed in 0.5.0

vunder commented 6 years ago

Still has this problem even after udate to 0.5.0 I have RecyclerView within a fragment. IN case this is no scroll in the list - everything is fine. However after scroll and switch to another fragment sticky header sticks to the to top of the screen

                getSupportFragmentManager()
                        .beginTransaction()
                        .replace(R.id.fragment_container_main, ReviewFragment.newInstance())
                        .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
                        .commit();

Here is activity layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="ru.alexeystarchikov.moneywallet.MainActivity">

    <FrameLayout
        android:id="@+id/fragment_container_main"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

    </FrameLayout>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/main_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/main_bottom_navigation"/>

</LinearLayout>
bgogetap commented 6 years ago

Wrap your RecyclerView in the Fragment in a FrameLayout and that should solve it. The sticky header is getting added to the fragment container in the Activity layout (I assume). RecyclerView visibility doesn't change when the Fragment is replaced, it just gets detached/GC'd.

I might be able to add an attach state listener, but wrapping the RecyclerView in a FrameLayout would be the easiest solution for now.

I won't have time to look at this library until late next month probably.

vunder commented 6 years ago

Thanks. This works fine.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView android:id="@+id/scheduled_transaction_recycler_view"
                                            android:layout_width="match_parent"
                                            android:layout_height="match_parent">

    </android.support.v7.widget.RecyclerView>

</FrameLayout>

However better to solve this because such layout looks redundant