TabLayoutMediator 不保留 TabItem 属性
TabLayoutMediator not preserving TabItem attributes
我使用带有预设 TabItems 的 ViewPager2 和 TabLayout 进行了简单设置:
...
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabs"
android:layout_width="0dp"
android:layout_height="64dp"
app:tabTextColor="@color/c0696D7"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/palettesToggle"
app:layout_constraintTop_toTopOf="parent"
app:tabBackground="@color/cEEEEEE"
app:tabIndicatorColor="@color/cFFFFFF"
app:tabGravity="fill"
app:tabUnboundedRipple="true"
app:tabIconTint="@color/palettes_icon_tint"
app:tabIndicatorGravity="stretch"
app:tabMode="fixed">
<com.google.android.material.tabs.TabItem android:icon="@drawable/palettes_layers" />
<com.google.android.material.tabs.TabItem android:icon="@drawable/palettes_view" />
<com.google.android.material.tabs.TabItem android:icon="@drawable/palettes_properties" />
<com.google.android.material.tabs.TabItem android:icon="@drawable/palettes_blocks" />
<com.google.android.material.tabs.TabItem android:icon="@drawable/palettes_blocks" />
<com.google.android.material.tabs.TabItem android:icon="@drawable/palettes_settings" />
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tabs" />
...
和以下接线代码:
TabLayoutMediator(tabs, viewPager) { tab, position ->
}.attach()
以下设置忽略了 TabItem
图标属性,我看到了空标签。似乎 TabLayoutMediator
完全覆盖了已定义的 xml 属性,我必须将这些属性重置为 TabConfigurationStrategy
回调的一部分。
我错过了什么吗?
方法 TabLayoutMediator.attach()
calls the method tabLayout.removeAllTabs();
删除所有选项卡 然后再次添加选项卡。
同时检查 official doc:
When creating an instance of this class, you must supply an implementation of TabLayoutMediator.TabConfigurationStrategy
in which you set the text of the tab, and/or perform any styling of the tabs that you require.
类似于:
new TabLayoutMediator(tabs, viewpager, new TabLayoutMediator.TabConfigurationStrategy() {
@Override public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
//Configure your tabs...
tab.setText(...);
tab.setIcon(...);
}
}).attach();
或:
TabLayoutMediator(tabs, viewpager,
TabLayoutMediator.TabConfigurationStrategy { tab, position ->
//Configure tabs..
when (position) {
0 -> { tab.text = "..."}
1 -> { tab.text = "..."}
}
}).attach()
我使用带有预设 TabItems 的 ViewPager2 和 TabLayout 进行了简单设置:
...
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabs"
android:layout_width="0dp"
android:layout_height="64dp"
app:tabTextColor="@color/c0696D7"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/palettesToggle"
app:layout_constraintTop_toTopOf="parent"
app:tabBackground="@color/cEEEEEE"
app:tabIndicatorColor="@color/cFFFFFF"
app:tabGravity="fill"
app:tabUnboundedRipple="true"
app:tabIconTint="@color/palettes_icon_tint"
app:tabIndicatorGravity="stretch"
app:tabMode="fixed">
<com.google.android.material.tabs.TabItem android:icon="@drawable/palettes_layers" />
<com.google.android.material.tabs.TabItem android:icon="@drawable/palettes_view" />
<com.google.android.material.tabs.TabItem android:icon="@drawable/palettes_properties" />
<com.google.android.material.tabs.TabItem android:icon="@drawable/palettes_blocks" />
<com.google.android.material.tabs.TabItem android:icon="@drawable/palettes_blocks" />
<com.google.android.material.tabs.TabItem android:icon="@drawable/palettes_settings" />
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tabs" />
...
和以下接线代码:
TabLayoutMediator(tabs, viewPager) { tab, position ->
}.attach()
以下设置忽略了 TabItem
图标属性,我看到了空标签。似乎 TabLayoutMediator
完全覆盖了已定义的 xml 属性,我必须将这些属性重置为 TabConfigurationStrategy
回调的一部分。
我错过了什么吗?
方法 TabLayoutMediator.attach()
calls the method tabLayout.removeAllTabs();
删除所有选项卡 然后再次添加选项卡。
同时检查 official doc:
When creating an instance of this class, you must supply an implementation of
TabLayoutMediator.TabConfigurationStrategy
in which you set the text of the tab, and/or perform any styling of the tabs that you require.
类似于:
new TabLayoutMediator(tabs, viewpager, new TabLayoutMediator.TabConfigurationStrategy() {
@Override public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
//Configure your tabs...
tab.setText(...);
tab.setIcon(...);
}
}).attach();
或:
TabLayoutMediator(tabs, viewpager,
TabLayoutMediator.TabConfigurationStrategy { tab, position ->
//Configure tabs..
when (position) {
0 -> { tab.text = "..."}
1 -> { tab.text = "..."}
}
}).attach()