F.A.B Hides but Doesn't Show

本秂侑毒 提交于 2019-12-06 03:04:25
Anthonyeef

Which support library version are you using in your project?

If you are using the latest one ( I mean 25.0.x), this is because fab.hide() method set the visibility to be View.GONE. This makes nested scroll listener stop to check fab, the second time you try to scroll the list.

More detail can be found here: https://code.google.com/p/android/issues/detail?id=230298

And I search a bit found this similar question already have a nice answer: Floating action button not visible on scrolling after updating Google Support & Design Library

So a possible work around would be to override fab.hide method, not to set the visibility to GONE but INVISIBLE instead.

And I think this may be fixed from upstream later, so just keep an eye on it.

Because the update of Android to 25.0.x+ you should set the fab button with the listener to INVISBLE to show again the fab button, my solution is the next:

public class ScrollAwareFABBehavior extends FloatingActionButton.Behavior {
private static final String TAG = "ScrollAwareFABBehavior";

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


@Override
public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull FloatingActionButton child, @NonNull View directTargetChild, @NonNull View target, int axes, int type) {
    return true;
}

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

    return false;
}

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

    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();
    }
}
}

** Tested in a Nexus 6P with Android Oreo.

Use double check methods as suggested before. A fab can be showed and invisible at the same time, so for flawless showing fab:

fab.show(); fab.setVisibility(View.VISIBLE);

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!