2016-12-14 46 views
17

我升级到最新的支持库版本,并且滚动FAB行为不起作用。当在RecyclerView上向下滚动时,它正确地向下滚动,但是当再次滚动时不是。它保持隐藏。降级到25.0.1似乎可以缓解这个问题。这里是供我参考的代码。25.1.0 Android支持库正在破坏晶圆厂行为

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

我一直有同样的问题,但我仍然是。终于有人发布了这个问题! – wax911

回答

26

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

“目前”是否意味着它会改回来?你知道这是否有错误或任何讨论? – khusrav

+0

我会认为这是一个错误,因为这种行为变化是在25.1.0中引入的,但我并不知道任何问题票据或Google的意见。 – jhorvat

+3

https://code.google.com/p/android/issues/detail?id=230298 – Luiz

-1

我有一点与TabView的相同问题(升级到25.1.0之后),它显示了第一次,但第二次(后复育)它变得不可见。

+0

那又如何?这不是一个答案。 Downvoted。 –

0

上面的自定义OnVisibilityChangedListener只是我的解决方案的一部分。随着更新设置晶圆厂无形的,它也需要更新在onNestedScroll()重写了否则,如果条件,现在测试的知名度是无形的,而不是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(); 
    } 
} 
+0

由于原代码在'else if'部分中使用'child.getVisibility()!= View.VISIBLE',因此它包含'INVISIBLE'和'GONE'。请避免发布仅对其他答案进行小幅改进的答案;改变评论或建议编辑。 –