更新Google支持和服务后,滚动中的浮动操作按钮不可见设计库 [英] Floating action button not visible on scrolling after updating Google Support & Design Library

查看:66
本文介绍了更新Google支持和服务后,滚动中的浮动操作按钮不可见设计库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将com.android.support:appcompatcom.android.support:design从25.0.1更新为25.1.0,如下所示:

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'

收件人:

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

但是我发现活动滚动时不再显示我的浮动操作按钮.我的FAB行为由以下定义:

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

我的布局如下:

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

推荐答案

从支持库25.0.1更新到25.1.0会更改CoordinatorLayoutonNestedScroll方法,因为对于设置了可见性的视图,该调用被跳过到View.GONE.

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.

在浮动操作按钮上调用child.hide()会将视图的可见性设置为View.GONE,这意味着现在(从25.1.0开始),将来会为浮动操作按钮跳过onNestedScroll方法调用(因为它会跳过可见度为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).

为此的一种解决方法是,每当隐藏视图时,将视图的可见性设置为INVISIBLE.这样,onNestedScroll在下次执行嵌套滚动时将不会跳过视图.

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

在您的onNestedScroll方法中.

此问题已通过 https://code提交给AOSP问题跟踪器. google.com/p/android/issues/detail?id=230298

这篇关于更新Google支持和服务后,滚动中的浮动操作按钮不可见设计库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆