1

我試圖隱藏兩個FAB上嵌套滾動滾動。我試圖實現自己的FAB行爲,但它沒有奏效。 FAB對滾動根本沒有反應。注:我刪除了部分佈局以適合帖子。如何隱藏嵌套滾動上的浮動操作按鈕

佈局

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    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:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="ru.berezin.maxim.im.budget_v3.userinterface.StartPage"> 

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/appcollapse" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 
    </android.support.design.widget.AppBarLayout> 

    <android.support.v4.widget.NestedScrollView 
     android:id="@+id/testingscroll" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scrollbars="none" 
     app:behavior_overlapTop="30dp" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

     <!--content including--> 
     <include layout="@layout/start_content_layout"/> 

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

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/start_page_fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="end|bottom" 
     android:layout_margin="16dp" 
     android:src="@drawable/plus" 
     app:layout_anchor="@id/testingscroll" 
     app:layout_anchorGravity="end|bottom" 
     app:layout_behavior="ru.berezin.maxim.im.budget_v3.FabOnScroll" 
     /> 

    <View 
     android:id="@+id/dummy" 
     android:layout_width="1dp" 
     android:layout_height="16dp" 
     app:layout_anchor="@id/start_page_fab" 
     app:layout_anchorGravity="top|right|end" 
     /> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/account_det_add_transfer" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="end|top" 
     android:layout_margin="16dp" 
     android:src="@drawable/minus" 
     app:layout_anchor="@id/dummy" 
     app:layout_anchorGravity="top|right|end" 
     app:layout_behavior="ru.berezin.maxim.im.budget_v3.FabOnScroll" 
     /> 
</android.support.design.widget.CoordinatorLayout> 

FabOnScroll類

public class FabOnScroll extends FloatingActionButton.Behavior { 
     public FabOnScroll(Context context, AttributeSet attrs) { 
      super(); 
     } 

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

      //child -> Floating Action Button 
      if (dyConsumed > 0) { 
       CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) child.getLayoutParams(); 
       int fab_bottomMargin = layoutParams.bottomMargin; 
       child.animate().translationY(child.getHeight() + fab_bottomMargin).setInterpolator(new LinearInterpolator()).start(); 
      } else if (dyConsumed < 0) { 
       child.animate().translationY(0).setInterpolator(new LinearInterpolator()).start(); 
      } 
     } 

     @Override 
     public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View directTargetChild, View target, int nestedScrollAxes) { 
      return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL; 
     } 

    } 

回答

1

所以,我固定它。不完全是我想要的,但它的工作。我沒有任何想法爲什麼,但由於某種原因,dyConsumed總是等於0.但我注意到,當我滾動時,dyUnconsumed正在改變。所以我只是添加dxUnconsumed檢查到我的If/else語句,它的工作。有人可以解釋爲什麼dyConsumed等於0?

0
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() 
{ 
@Override 
public void onScrolled(RecyclerView recyclerView, int dx, int dy) 
{ 
    if (dy > 0 ||dy<0 && fab.isShown()) 
    { 
     fab.hide(); 
    } 
} 

@Override 
public void onScrollStateChanged(RecyclerView recyclerView, int newState) 
{ 
    if (newState == RecyclerView.SCROLL_STATE_IDLE) 
    { 
     fab.show(); 
    } 

    super.onScrollStateChanged(recyclerView, newState); 
} 
}); 
+0

我沒有recyclerView。 NestedScroll包含帶文字的cardViews – Kroha

+0

用此更新您的代碼。希望它能工作 –

+0

if(dyConsumed> 0 && child.getVisibility()== View.VISIBLE){ child.hide(); } else if(dyConsumed <0 && child.getVisibility()== View.GONE){ child.show(); } –

1

試試下面的代碼到你的NestedScrollView ScrollChangeListener,並檢查這是否是你所期望的。它必須適用於誰在使用NestedScrollView而不是RecyclerView。

NestedScrollView nsv = (NestedScrollView) v.findViewById(R.id.nsv); 
    nsv.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() { 
     @Override 
     public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) { 
      if (scrollY > oldScrollY) { 
       fab.hide(); 
      } else { 
       fab.show(); 
      } 
     } 
    });