Art
Art

Reputation: 771

Floating Action Button and RecyclerView

Is it possible to show Floating Action Button when all RecyclerView items fit screen. The problem is - when Floating Action Button is hidden and RecyclerView becomes almost empty - there is no place to scroll and so Floating Action Button not shown.

Upvotes: 1

Views: 3398

Answers (3)

Art
Art

Reputation: 771

I have solved my problem in such way:

  1. Adding RecyclerView.AdapterDataObserver to adapter
  2. After data have changed I am registering ViewTreeObserver.OnPreDrawListener on recyclerView private RecyclerView.AdapterDataObserver adapterDataObserver = new RecyclerView.AdapterDataObserver() { @Override public void onChanged() { super.onChanged(); recyclerViewEmptySupport.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { @Override public boolean onPreDraw() { recyclerViewEmptySupport.getViewTreeObserver().removeOnPreDrawListener(this); scrollingFABBehavior.recyclerViewChanged(recyclerViewEmptySupport, dialogsListAdapter, fab); return false; } }); } };

  3. In this listener I inform FAB that recyclerView MAY be empty enough (or may be not) to forcely be shown

Upvotes: 0

Itapu Vinay
Itapu Vinay

Reputation: 697

You can achieve the desired behavior by using a Coordinator Layout. The below links are pretty good references to start with.

  1. https://mzgreen.github.io/2015/06/23/How-to-hideshow-Toolbar-when-list-is-scrolling(part3)/

  2. https://medium.com/ribot-labs/exploring-the-new-android-design-support-library-b7cda56d2c32

Upvotes: 2

Ashish Rawat
Ashish Rawat

Reputation: 5829

Yes you have to use co-ordinate layout like this https://lab.getbase.com/introduction-to-coordinator-layout-on-android/

Upvotes: 2

Related Questions