【问题标题】:25.1.0 Android support lib is breaking fab behavior25.1.0 Android 支持库正在破坏工厂行为
【发布时间】:2016-12-14 12:29:39
【问题描述】:

我升级到最新的支持库版本并且滚动 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();
        }
    }
}

【问题讨论】:

  • 我也遇到了同样的问题,但现在仍然如此。终于有人发帖了!

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


【解决方案1】:

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

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

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

【讨论】:

  • “当前”是否意味着预计会改回来?你知道这方面是否存在错误或任何讨论?
  • 我认为这是一个错误,因为这种行为变化是在 25.1.0 中引入的,但我不知道有任何问题单或 Google 的意见。
【解决方案2】:

上述自定义 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();
    }
}

【讨论】:

  • 由于原代码在else if部分使用了child.getVisibility() != View.VISIBLE,这包括INVISIBLEGONE。请避免发布对其他答案仅有微小改进的答案;改为评论或建议修改。
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 2017-04-29
    • 1970-01-01
    • 2017-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-23
    • 1970-01-01
    • 2010-10-03
    相关资源
    最近更新 更多