Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

25.1.0 Android support lib is breaking fab behavior

I upgrade to the latest support library version and the Scrolling FAB behavior is not working. When scrolling down on a RecyclerView it correctly scrolls down, but when scrolling up again it is not. It stays hidden. Downgrading to 25.0.1 seems to mitigate this issue. For reference here is the code I use for this.

<android.support.v4.widget.DrawerLayout 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/drawer_layout"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:animateLayoutChanges="true"
  android:fitsSystemWindows="true"
  tools:context=".mainhost.MainActivity"
  tools:openDrawer="start">

  <android.support.design.widget.CoordinatorLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main_coordinator_layout_root_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".mainhost.MainActivity">

    <android.support.design.widget.AppBarLayout
      android:id="@+id/appbar_layout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:theme="@style/AppTheme.AppBarOverlay">

      <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways|snap">

        <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:focusableInTouchMode="true"
        app:layout_collapseMode="pin"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

      </android.support.design.widget.CollapsingToolbarLayout>

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

    <!-- Layout for content is here. This can be a RelativeLayout  -->

    <FrameLayout
      android:id="@+id/content_main"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_marginLeft="-3dp"
      android:layout_marginRight="-3dp"
      app:layout_behavior="@string/appbar_scrolling_view_behavior"
      tools:context="com.globyworks.citykey.mainhost.MainActivity" />


    <android.support.design.widget.FloatingActionButton
      android:id="@+id/fab"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="bottom|end"
      android:layout_margin="@dimen/fab_margin"
      app:layout_behavior="com.globyworks.citykey.helpers.ScrollingFABBehavior"
      android:src="@drawable/drawer_new_report_white" />

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


  <android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@android:color/white"
    app:menu="@menu/menu_drawer">

  </android.support.design.widget.NavigationView>


</android.support.v4.widget.DrawerLayout>

And the scrolling class:

    public class ScrollingFABBehavior extends FloatingActionButton.Behavior {


    public ScrollingFABBehavior(Context context, AttributeSet attrs) {
        super();
    }

    public boolean onStartNestedScroll(CoordinatorLayout parent, FloatingActionButton child, View directTargetChild, View target, int nestedScrollAxes) {

        return true;
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, FloatingActionButton child, View dependency) {
        if (dependency instanceof RecyclerView) {
            return true;
        }

        return false;
    }

    @Override
    public void onNestedScroll(CoordinatorLayout coordinatorLayout,
                               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) {
            child.hide();
        } else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {
            child.show();
        }
    }
}
like image 206
Rene Gens Avatar asked Dec 14 '16 12:12

Rene Gens


1 Answers

Currently CoordinatorLayout is skipping views set to GONE when looking for behaviours to call in its onNestedScroll method.

A quick workaround here is setting the FAB's visibility to INVISIBLE at the end of the FAB's hide animation.

if (dyConsumed > 0 && child.getVisibility() == View.VISIBLE) {
    child.hide(new FloatingActionButton.OnVisibilityChangedListener() {
        @Override
        public void onHidden(FloatingActionButton fab) {
            super.onHidden(fab);
            fab.setVisibility(View.INVISIBLE);
        }
    });
} else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {
    child.show();
}
like image 187
jhorvat Avatar answered Nov 15 '22 22:11

jhorvat