Smooth animation between tabs when using selector - Android

后端 未结 2 1736
旧时难觅i
旧时难觅i 2021-02-08 22:25

I would like to get the background of the tab animated smoothly to the selected position (just like the default tabIndicator animates between tabs).

Here is

相关标签:
2条回答
  • 2021-02-08 23:19

    Check the @Maulik answer. It is what you are looking for.
    Just to integrate the answer, without a custom shape (in your example you are using it) you can just use:

     <com.google.android.material.tabs.TabLayout
          app:tabIndicatorGravity="stretch"
          app:tabIndicatorColor="@color/...."
          ..>
    

    The INDICATOR_GRAVITY_STRETCH allows you to stretch the tab selection indicator across the entire height and width. In this way you can just use the app:tabIndicatorColor without drawable to set the selected color used.
    The animation is provided by default.

    0 讨论(0)
  • 2021-02-08 23:30

    We've achieved the same effect in our app using TabLayout.

    The trick for smooth animation is to use tabIndicator attirbute instead of tabBackground!

    You can define a shape drawable (not selector) and assign it to TabLayout by:

    app:tabIndicator="@drawable/tab_layout_fg"
    

    This will add a tab indicator with smooth animation that you want to achieve. If you find that tabIndicator color is not reflecting as defined in the drawable file, you can tint the tabIndicator using tabIndicatorColor attribute.

    Note: Make sure you're using latest design support library v28.0.0 or material components library for Android X.

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