25.1.0 Android支持lib正在打破fab行为

时间:2016-12-14 12:29:39

标签: android android-support-library floating-action-button

我升级到最新的支持库版本,并且滚动FAB行为无效。向下滚动RecyclerView时,它会向下滚动,但是当再次向上滚动时则不会向下滚动。它保持隐藏。降级到25.0.1似乎缓解了这个问题。这里参考的是我用于此的代码。

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/drawer_layout"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:animateLayoutChanges="true"
  android:fitsSystemWindows="true"
  tools:context=".mainhost.MainActivity"
  tools:openDrawer="start">

  <android.support.design.widget.CoordinatorLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main_coordinator_layout_root_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".mainhost.MainActivity">

    <android.support.design.widget.AppBarLayout
      android:id="@+id/appbar_layout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:theme="@style/AppTheme.AppBarOverlay">

      <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways|snap">

        <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:focusableInTouchMode="true"
        app:layout_collapseMode="pin"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

      </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <!-- Layout for content is here. This can be a RelativeLayout  -->

    <FrameLayout
      android:id="@+id/content_main"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_marginLeft="-3dp"
      android:layout_marginRight="-3dp"
      app:layout_behavior="@string/appbar_scrolling_view_behavior"
      tools:context="com.globyworks.citykey.mainhost.MainActivity" />


    <android.support.design.widget.FloatingActionButton
      android:id="@+id/fab"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="bottom|end"
      android:layout_margin="@dimen/fab_margin"
      app:layout_behavior="com.globyworks.citykey.helpers.ScrollingFABBehavior"
      android:src="@drawable/drawer_new_report_white" />

  </android.support.design.widget.CoordinatorLayout>


  <android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@android:color/white"
    app:menu="@menu/menu_drawer">

  </android.support.design.widget.NavigationView>


</android.support.v4.widget.DrawerLayout>

滚动类:

    public class ScrollingFABBehavior extends FloatingActionButton.Behavior {


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

    public boolean onStartNestedScroll(CoordinatorLayout parent, FloatingActionButton child, View directTargetChild, View target, int nestedScrollAxes) {

        return true;
    }

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

        return false;
    }

    @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 && child.getVisibility() == View.VISIBLE) {
            child.hide();
        } else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {
            child.show();
        }
    }
}

3 个答案:

答案 0 :(得分:26)

当查找要在其GONE方法中调用的行为时,当前CoordinatorLayout正在跳过设置为onNestedScroll的视图。

此处的快速解决方法是在end of the FAB's hide animation处将FAB的可见性设置为INVISIBLE

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

答案 1 :(得分:0)

上面的自定义OnVisibilityChangedListener只是我解决方案的一部分。通过更新将fab设置为INVISIBLE,还需要在onNestedScroll()覆盖中更新 else if 条件,以便现在测试可见性为INVISIBLE,而不是GONE:

@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 && 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.INVISIBLE){
        child.show();
    }
}

答案 2 :(得分:-1)

我对tabview有一些相同的问题(在升级到25.1.0之后)它显示第一次,但第二次(重新填充后)它变得不可见。