Show/Hide FloatingActionButton on ViewPager swipe

后端 未结 2 688
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-08 16:13

I have an activity that has 3 tabs. Each tabs page is a fragment which displays a RecyclerView. One of the fragment has FloatingActionButton in it. I\'m implementing this Button

相关标签:
2条回答
  • 2021-02-08 16:25

    Add

    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    

    to the RecyclerView too.

    0 讨论(0)
  • 2021-02-08 16:48

    The best option would be to just put the FloatingActionButton in the Activity, and call show() and hide() in a ViewPager.OnPageChangeListener. This way you get nice enter/exit animations that conform to Material Design guidelines.

    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    
        }
    
        @Override
        public void onPageSelected(int position) {
            if (position == 0) {
                fabAdd.show();
            } else {
                fabAdd.hide();
            }
        }
    
        @Override
        public void onPageScrollStateChanged(int state) {
    
        }
    });
    

    Result:

    0 讨论(0)
提交回复
热议问题