简体   繁体   中英

How to make fab visible when scroll up starting and hide when scroll down starting

I want to make fab hide as soon as scrolling down starting and show as soon as scrolling up starts how can I do this, right now I'm trying this but its appearing only when it reaches top and hiding when reaches bottom:

 @Override
public boolean onStartNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child, final View directTargetChild, final View target, final int nestedScrollAxes) {
    // Ensure we react to vertical scrolling
    return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL
            || super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes);
}

@Override
public void onNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child, final View target, final int dxConsumed, final int dyConsumed, final int dxUnconsumed, final int dyUnconsumed) {
    super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);

    if (CoordinatorLayout.SCROLL_INDICATOR_START == 16 && dyConsumed > 0 && child.getVisibility() == View.VISIBLE) {
        child.hide();
    } else if (CoordinatorLayout.SCROLL_INDICATOR_START == 16 && dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {
        child.show();
    }
} 

If there is a recyclerView in your layout this code will help you:

recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
        override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
            if (dy > 0) {
                fab?.hide()
            } else if (dy < 0) {
                fab?.show()
            }
        }
    })

Please do this way , I hope it will help for you.

 @Override
 public boolean onStartNestedScroll(final CoordinatorLayout coordinatorLayout, final View child,final View directTargetChild, final View target,final int nestedScrollAxes) {

// Ensure we react to vertical scrolling

 return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL
        || super.onStartNestedScroll(coordinatorLayout, child,
           directTargetChild, target, nestedScrollAxes);
 }

 @Override
 public void onNestedScroll(final CoordinatorLayout coordinatorLayout, final View child,final View target, final int dxConsumed, final int dyConsumed, final int dxUnconsumed, final int dyUnconsumed) {

 super.onNestedScroll(coordinatorLayout, child, target,dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
 if (dyConsumed > 0) {

  // User scrolled down -> hide the FAB

  List<View> dependencies = coordinatorLayout.getDependencies(child);
   for (View view : dependencies) {
     if (view instanceof FloatingActionButton) {
      ((FloatingActionButton) view).hide();
    }
  }
} else if (dyConsumed < 0) {
  // User scrolled up -> show the FAB
  List<View> dependencies = coordinatorLayout.getDependencies(child);
  for (View view : dependencies) {
    if (view instanceof FloatingActionButton) {
      ((FloatingActionButton) view).show();
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM