FloatingActionButton 滚动感知问题与 FrameLayout 内的 RecyclerView

FloatingActionButton scroll aware issues with RecyclerView inside FrameLayout

我的一个项目正在使用 class 到 hide/show 完美的 fab 按钮。现在,对一些布局要求进行更改,向上滚动时的 show fab 无法正常工作。

CoordinatorLayout 设置是标准的,它包含一个在其中加载 Fragment 的 ViewPager。对 Fragment 布局的更改导致 fab show 行为不再正常工作。


这是原始的工作片段布局:

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

这是不起作用的新 Fragment 布局:

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

添加的 FrameLayout 似乎导致了这些问题,但我不确定为什么。这是设计使然的问题吗?还是我遗漏了什么?

将支持库更新到版本 25.1.0 后,我遇到了同样的问题。如果您在行为 class 中将视图可见性设置为 GONE,现在这些视图将被忽略。因此可能的解决方案是降级支持库或更新您的行为 class - 使视图 INVISIBLE 而不是 GONE.

很好的解决了这个问题。 FloatingActionButtonCoordinatorLayout.

存在某种类型的错误或奇怪的事情

FloatingActionButton.hide() 使按钮可见 GONE。这似乎导致 CoordinatorLayout 忽略 FloatingActionButton 的进一步事件。这就是向下滚动没有再次显示按钮的原因。

解决方案是确保 FloatingActionButton 的可见性在调用 FloatingActionButton.hide() 后设置为 INVISIBLE

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