TabLayout 'scrollable' 模式滑动问题及解决方法

在使用TabLayout时,当app:tabMode设置为'scrollable'模式时,滑动选项卡时可能会遇到一个问题:滑动不会直接滑到目标选项卡,而是先滑到原先选项卡的内容位置。这是因为'scrollable'模式下所有选项卡都会平分TabLayout的宽度,而滑动时会滑到下一个选项卡的中心位置,因此会先滑到原先选项卡的中心位置,再滑到目标选项卡的中心位置。

如果想要直接滑到目标选项卡,可以将app:tabMode设置为'fixed',这样每个选项卡的宽度就会根据内容自适应,滑动时也会直接滑到目标选项卡。

示例代码:

<com.google.android.material.tabs.TabLayout
    android:id='@+id/tabLayout'
    android:layout_width='match_parent'
    android:layout_height='wrap_content'
    app:tabMode='fixed'>
</com.google.android.material.tabs.TabLayout>

通过将app:tabMode设置为'fixed',可以解决TabLayout在'scrollable'模式下滑动时遇到的问题,实现更流畅的滑动体验。

TabLayout 'scrollable' 模式滑动问题及解决方法

原文地址: https://www.cveoy.top/t/topic/oJPv 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录