Show/Hide FloatingActionButton while scrolling




 <android.support.design.widget.FloatingActionButton  
     android:id="@+id/fab"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_margin="@dimen/fab_margin"  
     android:src="@drawable/gallary_white"  
     app:layout_anchor="@id/app_bar"  
     app:layout_behavior="your-package.FABScrollBehavior"  
     app:layout_anchorGravity="bottom|end" />  


 public class FABScrollBehavior extends FloatingActionButton.Behavior {  
   public FABScrollBehavior(Context context, AttributeSet attributeSet){  
     super();  
   }  
   @Override  
   public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View directTargetChild, View target, int nestedScrollAxes) {  
     return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL;  
   }  
   @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.GONE){  
       child.show();  
     }  
   }  
 }  

Comments