I want to hide/show FloatingActionButton
on scroll of RecyclerView
.
My XML
layout :
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview_eventlist"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_createevent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/fab_margin"
app:layout_anchor="@id/recyclerview_eventlist"
app:layout_anchorGravity="bottom|right|end"
app:layout_behavior="com.eventizon.behavior.ScrollAwareFABBehavior"
android:src="@drawable/ic_edit"
app:backgroundTint="@color/custom_color_1"
app:borderWidth="0dp" />
</android.support.design.widget.CoordinatorLayout>
DrawerLayout is the parent layout of this layout.
public class ScrollAwareFABBehavior extends FloatingActionButton.Behavior {
private static final String TAG = "ScrollAwareFABBehavior";
public ScrollAwareFABBehavior(Context context, AttributeSet attrs) {
super();
Log.e(TAG,"ScrollAwareFABBehavior");
}
@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout,
FloatingActionButton child, View target, int dxConsumed,
int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
// TODO Auto-generated method stub
super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed,
dxUnconsumed, dyUnconsumed);
Log.e(TAG,"onNestedScroll called");
if (dyConsumed > 0 && child.getVisibility() == View.VISIBLE) {
Log.e(TAG,"child.hide()");
child.hide();
} else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {
Log.e(TAG,"child.show()");
child.show();
}
}
}
Used this layout behaviour for FloatingActionButton
.
When I see logcat
only constructor is getting called. onNestedScroll()
doesn't get called when I scroll the list.
I used this in a RecyclerView.Adapter's onBindViewHolder method to set the bottom margin of the last item in the list to 72dp so that it will scroll up above the floating action button.
This does not require a dummy entry in the list.
The solution is in: F.A.B Hides but Doesn't Show
The problem is Android 25.0.x+ sets the view to GONE and thats why the listener is not reporting changes.
Ok, here is what you need:
First, since your FAB depends on the
RecyclerView
, add the following to your behavior class:Next, in order to receive
onNestedScroll()
calls, you need to override this:Good luck!
Update
Here is what your
ScrollAwareFABBehavior
should look like:Also, it was tested using
com.android.support:design:23.0.1
Easiest solution:
I have created a custom RecyclerView which has
OnUpDownScrollListener
,OnLeftRightScrollListener
ready:Code:
MBRecyclerView.java
Usage (UpDownScrollListener):
similarely you can handle the LeftRight scrolling by setting
I hope it can help somebody :)
Try this
enjoy.