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

标签 android android-coordinatorlayout floating-action-button

我的一个项目是使用一个通用的行为类来隐藏/显示效果很好的 fab 按钮。现在,对一些布局要求进行更改,向上滚动时的 show fab 无法正常工作。

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


这是原始的工作 fragment 布局:

<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 似乎导致了这些问题,但我不确定原因。这是设计使然的问题吗?还是我遗漏了什么?

最佳答案

很好的解决了这个问题。 FloatingActionButtonCoordinatorLayout 存在某种类型的错误或奇怪的事情。

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

解决方案是确保在调用 FloatingActionButton.hide() 后将 FloatingActionButton 的可见性设置为 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();
    }
}

关于android - FloatingActionButton 滚动感知问题与 FrameLayout 内的 RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43921224/

相关文章:

android - 滚动/粘性 FAB float 操作按钮 android

android - 类型不匹配。必需 : Alignment. 水平找到:对齐

android - Android 模拟器中的蓝牙支持

android - 如何从 viewpager 和 pageradapter 中删除项目

android-recyclerview - CollapsingToolbarLayout 不与 RecyclerView 滚动行为折叠

android - 为什么向上滚动时滚动会停在折叠的工具栏处? (不扩大显示图像)

android - 我想弄清楚如何在单击抽屉导航中的 FAB 菜单时添加暗淡的背景

java - 在 ViewPager 中时,ListFragment 在事务后显示空屏幕

android - 如何处理 gridview 的滑动手势 (android)

android - 协调器布局,工具栏上边距奇怪的背景