Floating action button layout_behavior in multiple fragments

Viewed 968

I'm having a problem with the suppport floating action button. My Layout is structured like this:

MainActivity:

<android.support.design.widget.CoordinatorLayout>
    <android.support.design.widget.AppBarLayout>

        <android.support.design.widget.CollapsingToolbarLayout>
            <ImageView/>
            <LinearLayout>
                <TextView/>
                <android.support.v7.widget.AppCompatRatingBar/>
            </LinearLayout>
            <android.support.v7.widget.Toolbar/>
            <android.support.design.widget.TabLayout/>
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:src="@drawable/ic_action_add"
        app:layout_anchor="@+id/fragment_container"
        app:layout_anchorGravity="bottom|end"
        app:layout_behavior="...ScrollAwareFABBehavior"
        app:elevation="4dp" />

I'm using this FAB in multiple fragments, but the ScrollAwareBehavior (hiding/showing when scrolling the NestedScroll) is only working in the first fragment. (First one is using NestedScrollView, second one is using RecyclerView)

Is there any chance i can "detach" the FAB from the first NestedScrollView and attach it to the RecyclerView in the second fragment ?

I thought using the xml tags: layout_behavior and anchor would be doing this job. Maybe recreating the FAB in every fragment would be a workaround?

Appreciate your thoughts on this topic.

EDIT: As workaround I'm using this for now:

mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            if (dy > 0 && MainActivity.fab.isShown())
                MainActivity.fab.hide();
            else if(dy < 0 && !MainActivity.fab.isShown()){
                MainActivity.fab.show();
            }
        }
    });
0 Answers
Related