Skip to content Skip to sidebar Skip to footer

Android -fab Behaviour With Half List

I have the FAB working when recyclerview has enough items to scroll, but i need to handle the case when recyclerview does not scroll (the total of items do not cover the screen). A

Solution 1:

You have to handle another case independently from CoordinatorLayout.

override a function layoutDependsOn:

@OverridepublicbooleanlayoutDependsOn(CoordinatorLayout parent, FloatingActionButton child, View dependency) {
    returnsuper.layoutDependsOn(parent, child, dependency) || dependency instanceofRecyclerView;
}

onNestedScroll should also handle another case:

if (target instanceof RecyclerView) {
    handleRecyclerViewScrolling(target, child);
    return;
}

handleRecyclerViewScrolling should look like:

privatevoidhandleRecyclerViewScrolling(View target, FloatingActionButton child) {
    if (scrollListener != null) {
        return;
    }
    RecyclerViewrecyclerView= (RecyclerView) target;
    scrollListener = newRecyclerViewScrollListener(child);
    recyclerView.addOnScrollListener(scrollListener);
}

scrollListener should be a field in your FABBehavior class. Also declare inner class inside FABBehavior:

privateclassRecyclerViewScrollListenerextendsRecyclerView.OnScrollListener {
    FloatingActionButton mChild;

    publicRecyclerViewScrollListener(FloatingActionButton child) {
        this.mChild = child;
    }

    @OverridepublicvoidonScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
        if (newState == RecyclerView.SCROLL_STATE_IDLE) {
            mChild.show();
        } else {
            mChild.hide();
        }
    }

    @OverridepublicvoidonScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        if (!recyclerView.canScrollVertically(Integer.MAX_VALUE)) {
            mChild.show();
        }
    }
}

RecyclerViewScrollListener hides FAB, when it is scrolling and shows it when it is in idle state.

Post a Comment for "Android -fab Behaviour With Half List"