Jona
Jona

Reputation: 13555

FloatingActionButton scroll aware issues with RecyclerView inside FrameLayout

One of my projects is using a common behavior class to hide/show the fab button which works perfect. Now, do to some layout requirement changes, the show fab on scroll up is not working.

The CoordinatorLayout setup is standard and it contains a ViewPager which loads Fragments in it. The changes to the Fragment layout has caused the fab show behavior to no longer work correctly.


Here is the original working Fragment layout:

<SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RecyclerView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/filterMenu"
        android:clipToPadding="false" />
</SwipeRefreshLayout>

Here is the new Fragment layout which doesn't work:

<?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">

    <RelativeLayout
        android:id="@+id/emptyStateView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:translationY="@dimen/home_empty_state_y_offset">

        <ImageView
            android:id="@+id/emptyStateImage"
            android:layout_width="wrap_content"
            android:layout_height="160dp"
            android:layout_centerInParent="true"
            android:src="@drawable/home_empty_state_animation" />

    </RelativeLayout>

    <SwipeRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RecyclerView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/filterMenu"
            android:clipToPadding="false" />
    </SwipeRefreshLayout>
</FrameLayout>

It seems like the added FrameLayout is causing these issues but I'm not sure why. Is this a by design problem? or am I missing something?

Upvotes: 2

Views: 498

Answers (2)

Jona
Jona

Reputation: 13555

Well solved the issue. There is some type of bug or something odd with the FloatingActionButton and CoordinatorLayout.

FloatingActionButton.hide() makes the button visibility GONE. This seems to cause the CoordinatorLayout to ignore further events for FloatingActionButton. That is the reason why scrolling down didn't show the button again.

The solution was to make sure the visibility of the FloatingActionButton was set to INVISIBLE after calling FloatingActionButton.hide().

@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout, final FloatingActionButton child,
                           View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
    super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed,
            dyUnconsumed);

    if (dyConsumed > 0 && child.getVisibility() == View.VISIBLE)
    {
        // This fixes odd issue where fab doesn't show when scrolling down. Seems like the fab
        // is being set as GONE when hidden. This causes the events on this view to be ignored
        // by the CoordinatorLayout.
        child.hide(new FloatingActionButton.OnVisibilityChangedListener() {
            @Override
            public void onShown(FloatingActionButton fab) {
                super.onShown(fab);
            }

            @Override
            public void onHidden(FloatingActionButton fab) {
                super.onHidden(fab);


                child.setVisibility(View.INVISIBLE);
            }
        });
    }
    else if (dyConsumed <= 0 && child.getVisibility() != View.VISIBLE)
    {
        child.show();
    }
}

Upvotes: 6

Sunny
Sunny

Reputation: 137

I had the same problem after update support library to version 25.1.0. If you set views visibility to GONE in your behavior class, now those views are ignored. So possible solutions are to downgrade support library or update your behavior class - make views INVISIBLE instead of GONE.

Upvotes: 1

Related Questions