当我们在 ViewPager2 上左右滑动时,错误的 TabLayout 指示器会褪色

问题描述 投票:0回答:0

我们正在使用

ViewPager2
TabLayout
.

我们尝试通过以下方式禁用动画https://stackoverflow.com/a/62579862/72437

我们的代码看起来像

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:parentTag="android.widget.LinearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.google.android.material.tabs.TabLayout
        app:tabMode="fixed"
        app:tabGravity="fill"
        app:tabIndicatorColor="#ff0000"
        app:tabIndicatorHeight="2dp"
        app:tabIconTint="@color/tab_icon_selector"
        app:tabIndicatorAnimationMode="fade"
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="40dp" />

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
</merge>

// autoRefresh = true
// smoothScroll = false
new TabLayoutMediator(tabLayout, viewPager, true, false,
        (tab, position) -> {
            tab.setCustomView(R.layout.emoji_category_tab);

            tab.setIcon(EmojiCategory.values()[position].resourceId);
        }

).attach();

当我们明确点击

TabLayout
的选项卡时,效果非常好。没有动画。

点击 TabLayout 的选项卡

但是,当我们在 ViewPager2 上左右滑动时,选项卡指示器的淡入淡出效果不佳。

在 ViewPager2 上左右滑动(错误的指示器褪色)

gif 文件可能无法清楚地显示这种糟糕的着色。您可以在此视频中查看糟糕的褪色效果 - https://stackoverflow.com/a/61304247/72437

我希望指示器颜色不透明。但事实并非如此。

我可以知道,我们做错了什么吗?谢谢。


为了让问题更明显,我把

fade
改成了
app:tabIndicatorAnimationMode="linear"

这是结果

看来,如果我在 ViewPager2 上左右滑动,

TabLayout
的指示器上会执行不完整的动画。但为什么?

android android-tablayout android-viewpager2
© www.soinside.com 2019 - 2024. All rights reserved.