How to change FloatingActionButton between Tabs?

后端 未结 7 752
囚心锁ツ
囚心锁ツ 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:01

    Expanding upon blackcj's & kirtan403's answers, I have also added the ability to hide the fab on a chosen tab (in this case the 1st tab), which answer's bernzkie's question under blackcj's answer.

    To achieve this I first declared int[]'s with 3 items, each for the fab of each of the 3 tabs. I set the first item in each to 0 because that will be the 1st tab's invisible fab that doesn't need resources.

    int[] colorIntArray = {0, R.color.fab_red, R.color.fab_green};
    int[] iconIntArray = {0, R.drawable.fab_pencil, R.drawable.fab_chevron};
    

    I then set an if statement in the onCreate method of the Activity that features the fab and tabs. This statement hides the fab and scales it down, so that when it is made visible again, it can be made to only scale up, not down unnecessarily, then up again. I set the scale to match final scale of blackcj's scale down animation.

        if (tabLayout.getSelectedTabPosition() == 0) {
            // if on the 1st tab
            fab.hide();
            // scale down to only scale up when switching from 1st tab
            fab.setScaleX(0.2f);
            fab.setScaleY(0.2f);
        }
    

    Then outside the onCreate method, I added blackcj's animateFab method, with kirtan403's rotate modification. However, I modified the animateFab method to also have a conditional statement, where:

    • if it returns to the 1st tab, the fab is hidden (it scales down automatically when hiding);

    • when switching from a tab where the fab is already full size and visible to another tab where it is meant to be visible, it performs the full scale down, change & scale up animation;

    • when switching from the tab with the hidden fab (in this case the 1st tab), the fab is made visible, then scaled up (not scaled down, then scaled up) with the custom animation.

      protected void animateFab(final int position) {
          fab.clearAnimation();
          if (tabLayout.getSelectedTabPosition() == 0) {
              // if on the 1st tab
              fab.hide();
          } else if (fab.getScaleX() == 1f) {
              // if the fab is full scale
              // Scale down animation
              ScaleAnimation shrink =  new ScaleAnimation(1f, 0.2f, 1f, 0.2f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
              shrink.setDuration(150);     // animation duration in milliseconds
              shrink.setInterpolator(new DecelerateInterpolator());
              shrink.setAnimationListener(new Animation.AnimationListener() {
                  @Override
                  public void onAnimationStart(Animation animation) {
      
                  }
      
                  @Override
                  public void onAnimationEnd(Animation animation) {
                      // Change FAB color and icon
                      fab.setBackgroundTintList(getResources().getColorStateList(colorIntArray[position]));
                      fab.setImageDrawable(getResources().getDrawable(iconIntArray[position], null));
      
                      // Scale up animation
                      ScaleAnimation expand = new ScaleAnimation(0.2f, 1f, 0.2f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                      expand.setDuration(100);     // animation duration in milliseconds
                      expand.setInterpolator(new AccelerateInterpolator());
      
                      // Rotate Animation
                      Animation rotate = new RotateAnimation(60.0f, 0.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                      rotate.setDuration(200);
                      rotate.setInterpolator(new DecelerateInterpolator());
      
                      // Add both animations to animation state
                      AnimationSet animationSet = new AnimationSet(false); //false means don't share interpolators
                      animationSet.addAnimation(expand);
                      animationSet.addAnimation(rotate);
                      fab.startAnimation(animationSet);
                  }
      
                  @Override
                  public void onAnimationRepeat(Animation animation) {
      
                  }
              });
              fab.startAnimation(shrink);
          } else {
              // if the fab is already scaled down
              // Change FAB color and icon
              fab.setBackgroundTintList(getResources().getColorStateList(colorIntArray[position]));
              fab.setImageDrawable(getResources().getDrawable(iconIntArray[position], null));
              fab.show();
      
              // Scale up animation
              ScaleAnimation expand = new ScaleAnimation(0.2f, 1f, 0.2f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
              expand.setDuration(100);     // animation duration in milliseconds
              expand.setInterpolator(new AccelerateInterpolator());
      
              // Rotate Animation
              Animation rotate = new RotateAnimation(60.0f, 0.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
              rotate.setDuration(200);
              rotate.setInterpolator(new DecelerateInterpolator());
      
              // Add both animations to animation state
              AnimationSet animationSet = new AnimationSet(false); //false means don't share interpolators
              animationSet.addAnimation(expand);
              animationSet.addAnimation(rotate);
              fab.startAnimation(animationSet);
          }
      
      }
      

    Then I just added blackcj's unchanged tab selection listener to the onCreate method.

        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
                animateFab(tab.getPosition());
            }
    
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
    
            }
    
            @Override
            public void onTabReselected(TabLayout.Tab tab) {
    
            }
        });
    

    Hope this helps, it certainly works flawlessly for me. Thanks to blackcj & kirtan403. :)

提交回复
热议问题