Show/Hide FloatingActionButton on ViewPager swipe

后端 未结 2 690
爱一瞬间的悲伤
爱一瞬间的悲伤 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: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:

提交回复
热议问题