Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Floating action button not visible on scrolling after updating Google Support & Design Library

Tags:

I've tried updating com.android.support:appcompat and com.android.support:design from: 25.0.1 to 25.1.0, as follows:

compile 'com.android.support:appcompat-v7:25.0.1' compile 'com.android.support:design:25.0.1' 

to:

compile 'com.android.support:appcompat-v7:25.1.0' compile 'com.android.support:design:25.1.0' 

but I have found that my floating action button no longer appears when the activity scrolls. My FAB behaviour is defined by the following:

public class MyFabBehavior extends FloatingActionButton.Behavior {      public MyFabBehavior(Context context, AttributeSet attrs) {         super();     }      @Override     public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout,                                        FloatingActionButton child, View directTargetChild, View target, int nestedScrollAxes) {          // Ensure we react to vertical scrolling         return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL                 || super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes);      }      @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) {             // User scrolled up -> hide the FAB             animateFab(child, View.GONE);         } else if (dyConsumed > 0) {             // User scrolled down -> show the FAB             animateFab(child, View.VISIBLE);         }     }      static public void animateFab(FloatingActionButton fab, int visibility) {         // ignore visibility passed in, and just make fab visible regardless         if (fab.getVisibility() != View.VISIBLE) {             fab.show();         }     } } 

and my layout is as follows:

<android.support.design.widget.CoordinatorLayout     android:layout_width="match_parent"     android:layout_height="wrap_content"  >      <android.support.v4.widget.NestedScrollView         android:id="@+id/main_scrollview"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:orientation="vertical"         android:padding="8dp" >          ...      </android.support.v4.widget.NestedScrollView>      <android.support.design.widget.FloatingActionButton         app:layout_behavior="com.example.MyFabBehavior"         android:id="@+id/fab"         app:fabSize="normal"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="bottom|end"         android:layout_marginBottom="@dimen/fab_margin"         android:layout_marginRight="@dimen/fab_margin"         android:onClick="saveButton"         app:elevation="6dp"         app:pressedTranslationZ="12dp"         app:backgroundTint="@color/colorPrimary"         android:src="@drawable/ic_done_white_24dp" />  </android.support.design.widget.CoordinatorLayout> 
like image 490
drmrbrewer Avatar asked Dec 14 '16 22:12

drmrbrewer


People also ask

Where is the floating action button?

A floating action button (FAB) performs the primary, or most common, action on a screen. It appears in front of all screen content, typically as a circular shape with an icon in its center.

How do I import a floating action button?

Add the floating action button to your layoutThe size of the FAB, using the app:fabSize attribute or the setSize() method. The ripple color of the FAB, using the app:rippleColor attribute or the setRippleColor() method. The FAB icon, using the android:src attribute or the setImageDrawable() method.

What is extended floating action button in Android?

Extended Floating Action Button is the newly introduced class with Material Components library in Android. Material Components is introduced with SDK 28 or Android P. It's a superset of Support Design Library with lots of new additions and improvements.


2 Answers

Updating from support lib 25.0.1 to 25.1.0 changes the onNestedScroll method of CoordinatorLayout in that the call is skipped for views whose visibility is set to View.GONE.

Calling child.hide() on the floating action button sets the view's visibility to View.GONE, which means now (as of 25.1.0), the onNestedScroll method call will be skipped for the floating action button in the future (because it skips all views whose visibility is GONE).

A workaround for this would be to set the view's visibility to INVISIBLE whenever you hide it. This way, the onNestedScroll will not skip the view the next time a nested scroll is performed.

In order to achieve this, you can call

child.hide(new FloatingActionButton.OnVisibilityChangedListener() {             /**              * Called when a FloatingActionButton has been hidden              *              * @param fab the FloatingActionButton that was hidden.              */             @Override             public void onHidden(FloatingActionButton fab) {                 super.onShown(fab);                 fab.setVisibility(View.INVISIBLE);             }         }); 

in your onNestedScroll method.

Edit: This issue has been submitted to the AOSP Issue Tracker at https://code.google.com/p/android/issues/detail?id=230298

like image 158
Zach Avatar answered Oct 05 '22 10:10

Zach


in CoordinatorLayout 25.1.0 (

   for (int i = 0; i < childCount; i++) {             final View view = getChildAt(i);             if (view.getVisibility() == GONE) {                 // If the child is GONE, skip...                 continue;             } 

in 25.0.1

for (int i = 0; i < childCount; i++) {             final View view = getChildAt(i);             final LayoutParams lp = (LayoutParams) view.getLayoutParams();             if (!lp.isNestedScrollAccepted()) {                 continue;             }              final Behavior viewBehavior = lp.getBehavior();             if (viewBehavior != null) {                 viewBehavior.onNestedScroll(this, view, target, dxConsumed, dyConsumed,                         dxUnconsumed, dyUnconsumed);                 accepted = true;          } 
like image 41
aratj Avatar answered Oct 05 '22 09:10

aratj