Zahra.HY
Zahra.HY

Reputation: 1692

how to show/hide FAB on scroll Recycler view with coordinator parent

I have an activity with coordinator layout.inside activity there is a fragment with Recycler view and float button.how can I show/hide float button when Scroll Recycler view and avoid to use fab behavior?!

in activity layout: CoordinatorLayout----->AppBarLayout---->Toolbar and FrameLayout and Bottom bar view

in fragment layout: RelativeLayout---->Recycler view and float button

I want to implement something like Google+ home page. how can I implement this scenario?


Temporary I used this solution for my problem:

using coordinator layout of activity by interface in my fragment and show/hide fab with fab behavior ... until I find better solution!!!

Upvotes: 15

Views: 13905

Answers (5)

M_rey3000
M_rey3000

Reputation: 205

You can add this property in your floating action button:

app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"

Upvotes: 5

Arsen Tagaev
Arsen Tagaev

Reputation: 449

Solution in Kotlin

recycler_view = findViewById(R.id.recycler_view)

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

            }
        })

Upvotes: 1

a.yanyev
a.yanyev

Reputation: 31

Here is working solution:

class HideOnScrollFabBehavior(context: Context?, attrs: AttributeSet?) : FloatingActionButton.Behavior() {

    // changes visibility from GONE to INVISIBLE when fab is hidden because
    // due to CoordinatorLayout.onStartNestedScroll() implementation
    // child view's (here, fab) onStartNestedScroll won't be called anymore
    // because it's visibility is GONE
    private val listener = object : FloatingActionButton.OnVisibilityChangedListener() {
        override fun onHidden(fab: FloatingActionButton?) {
            fab?.visibility = INVISIBLE
        }
    }

    override fun onStartNestedScroll(coordinatorLayout: CoordinatorLayout, child: FloatingActionButton, directTargetChild: View, target: View, axes: Int, type: Int): Boolean {
        return axes == ViewCompat.SCROLL_AXIS_VERTICAL // Ensure we react to vertical scrolling
                || super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, axes, type)
    }

    override fun onNestedScroll(coordinatorLayout: CoordinatorLayout, child: FloatingActionButton, target: View, dxConsumed: Int, dyConsumed: Int, dxUnconsumed: Int, dyUnconsumed: Int, type: Int, consumed: IntArray) {
        super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, type, consumed)
        if (!target.canScrollVertically(1) && dyConsumed > 0 && child.visibility == VISIBLE) {
            // hide the FAB when scroll view reached its bottom
            child.hide(listener)
        } else if (dyConsumed < 0 && child.visibility != VISIBLE) {
            // show the FAB on scroll up
            child.show()
        }
    }

}

Upvotes: 1

Leandro Borges Ferreira
Leandro Borges Ferreira

Reputation: 12792

This code works just fine:

 mRecycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    if(dy > 0){
                        mFab.hide();
                    } else{
                        mFab.show();
                    }

                    super.onScrolled(recyclerView, dx, dy);
                }
            });

You cannot do:

app:layout_anchor="@id/listView"
app:layout_anchorGravity="bottom|end"

Look here:

There is no support built-in for CoordinatorLayout to work with ListView according to this Google post.

Upvotes: 33

Angel Koh
Angel Koh

Reputation: 13525

I modified Leondro's method such that the FAB will hide when there's scrolling and show when the scrolling stops.

scrollListener = new RecyclerView.OnScrollListener() {  
    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        switch (newState) {
            case RecyclerView.SCROLL_STATE_IDLE:
                fab.show();
                break;
            default:
                fab.hide();
                break;
        }
        super.onScrollStateChanged(recyclerView, newState);
    }
}; 

rv.clearOnScrollListeners();
rv.addOnScrollListener(scrollListener);

Upvotes: 6

Related Questions