问题描述
如果您已遇到此问题并已解决帮助。
无法使用TabLayoutManager,我认为这与材料设计和viewpager2的版本有关,我对它们都使用了最新版本。
versions.material =“ 1.3.0-alpha02” 版本.material =“ 1.3.0-alpha02”
versions.material="1.3.0-alpha02"
implementation 'androidx.viewpager2:viewpager2:1.1.0-alpha01'
像这样
解决方法
这是我实现ViewPager2 / TabLayout的方式
如果清理/重建不起作用,请尝试“文件”>“使缓存无效并重新启动”
我只使用了此实现:
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation "com.google.android.material:material:1.2.1"
我的XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.tabs.TabLayout
android:id="@+id/my_tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"
style="@style/Widget.MaterialComponents.TabLayout.Colored"/>
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</LinearLayout>
我的片段
import com.google.android.material.tabs.TabLayoutMediator
override fun onViewCreated(view: View,savedInstanceState: Bundle?) {
binding.viewPager.adapter =
ViewPagerAdapter(it.data,childFragmentManager,viewLifecycleOwner.lifecycle)
TabLayoutMediator(binding.myTablayout,binding.viewPager) { tab,position ->
tab.text = tabTitle(it.data[position])
}.attach()
}