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

*爱你&永不变心* 提交于 2020-01-01 03:05:23

问题


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!!!


回答1:


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.




回答2:


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);


来源:https://stackoverflow.com/questions/41699160/how-to-show-hide-fab-on-scroll-recycler-view-with-coordinator-parent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!