I use in the RecyclerView and FAB application. The problem is that the FAB button closes some of the controls in the last item in the RecyclerView list. I see a way out of the situation in such a way as to allow the list to be scrolled so that all the elements except the last could go up. And by default, the last item in the list does not scroll above the bottom edge of the RecyclerView. How to change this behavior? PS: hiding the FAB button also does not fit when there are only two elements in the list: they both fit on the screen and nothing scrolls anywhere, while FAB closes part of the last element.

  • If you follow the logic, then the fab - a floating button is required to float, that is, when scrolling down, the fab must dive and when scrolling up it will float back. familiar, cool and with meaning. It is easy to implement animations. - Flippy
  • 3
    You can modify the adapter to show another transparent element at the end of the list. - tse
  • @ SergeyGrushin no ready-made example? I saw this, and even, probably, I will. but what comes to mind is not called "easy." - tse
  • Re - write the question so that people quickly find the answer if they want to do the same - Flippy

2 answers 2

The root markup element must be a CoordinatorLayout . Create a Behavior class for the FAB button

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

And in the tag FloatingActionButton

 app:layout_behavior="your.package.FABScrollBehavior" 

Now, when scrolling down, the fab will be compressed scaling to the center, and when scrolling up, it will unclench. You can replace this with a dive and pop animation. Find the show() and hide() methods in the code and understand everything.

Perhaps I will share a complete layout

 <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <android.support.design.widget.AppBarLayout android:id="@+id/appBarLayout" android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" app:layout_scrollFlags="scroll|enterAlways" app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" android:layout_height="?android:actionBarSize" android:background="#831296"> </android.support.v7.widget.Toolbar> </android.support.design.widget.AppBarLayout> <android.support.v7.widget.RecyclerView android:id="@+id/fabRVlist" android:gravity="center" app:layout_behavior="@string/appbar_scrolling_view_behavior" android:listSelector="#00000000" android:layout_height="match_parent" android:layout_width="match_parent"/> <android.support.design.widget.FloatingActionButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/facb" android:layout_margin="10dp" app:layout_anchor="@+id/fabRVlist" app:layout_behavior="ru.albatros.material.FABScrollBehavior" android:src="@drawable/fabrandom" app:layout_anchorGravity="bottom|end" /> </android.support.design.widget.CoordinatorLayout> 

I leave the link to my githab repository, in it I collected all such things and poyuzal all widgets from design.support.library

http://github.com/ATumbler/Material-Demos

    Options:

    1. When the last item is reached or when all items are placed on the screen, move the FAB to the top of the list;
    2. Give the user the opportunity to move the FAB around the screen so that he himself moves it where necessary;
    3. Given that both options above do not solve the problem, but give a bit more flexibility, but can annoy the user, you can still hide the FAB in general, and in the basement of the list, expand something like a toolbar with the contents of the FAB. It is not typical for an android to “get” something from under the bottom edge of the screen, but you can hide this panel and pull it out just below the bottom edge of the list.

    I understand that crutches, but the flexibility of managing the interface is rather a feature.

    • Why such crutches if there is a standard behavior in which fab disappears when scrolling down the list? - temq
    • one
      I added to the list a ghost element to the end of a height slightly higher than FAB - Sergey Gernichenko
    • @temq reread all the information of the author. The point is that even when the list is placed on the screen (scrolling is not required) - the fab gets in the way. When the list is long and scrolling has reached the last element - the fab, again, interferes. - Christ