How to change FloatingActionButton between Tabs?

后端 未结 7 805
囚心锁ツ
囚心锁ツ 2021-01-30 02:28

I\'m trying to implement FloatingActionButton from Google Design Support Library into two of three tabs, and according to t

7条回答
  •  星月不相逢
    2021-01-30 03:17

    Below is the simple way to achieve your desired result

    enter image description here

    Add two (or equivalent to your tab actions) FloatingActionButton in your main activity like below

    
    
        
    
        
    
        
    
    
    
    
    
    
    
    
    

    Now in your MainActivity.java use Fab's default functions to hide and show on each tab selection like below

    private void animateFab(int position) {
        switch (position) {
            case 0:
                fabChat.show();
                fabPerson.hide();
                break;
            case 1:
                fabPerson.show();
                fabChat.hide();
                break;
    
            default:
                fabChat.show();
                fabPerson.hide();
                break;
        }
    }
    

    Call animateFab function as below

    TabLayout.OnTabSelectedListener onTabSelectedListener = new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            animateFab(tab.getPosition());
        }
    
        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
    
        }
    
        @Override
        public void onTabReselected(TabLayout.Tab tab) {
    
        }
    };
    
    ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    
        }
    
        @Override
        public void onPageSelected(int position) {
            animateFab(position);
        }
    
        @Override
        public void onPageScrollStateChanged(int state) {
    
        }
    };
    

提交回复
热议问题