我有tablayout和查看pager2如下所示:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="34dp"
android:background="@color/bgd_color_light"
android:clipChildren="false"
android:clipToPadding="false"
app:tabBackground="@color/bgd_color_light"
app:tabGravity="fill"
app:tabIndicator="@drawable/tab_indicator_round"
app:tabIndicatorAnimationMode="elastic"
app:tabIndicatorColor="@color/app_color"
app:tabIndicatorGravity="stretch"
app:tabMode="auto"
app:tabRippleColor="@color/transparent"
app:tabSelectedTextColor="@color/white"
app:tabTextAppearance="@style/CustomTabText" />
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/vp2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:clipToPadding="false"
android:orientation="horizontal" />
</LinearLayout>
字符串
我使用标签布局调解器移动通过标签.我有17标签,当我点击第5或更多的数字标签,查看寻呼机滚动到最后一个位置.不正确的标签Map.这里是片段适配器代码:
package com.ai.wallpaper.adapters
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Lifecycle
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.ai.wallpaper.fragments.WallpaperFragment
import com.ai.wallpaper.response.models.Category
class WallpaperPagerAdapter(fragmentManager: FragmentManager, lifecycle: Lifecycle, private
val isPremium: Boolean, private val categoryList: MutableList<Category>) :
FragmentStateAdapter(fragmentManager, lifecycle) {
override fun createFragment(position: Int): Fragment {
return WallpaperFragment.newInstance(categoryList[position].id, isPremium)
}
override fun getItemCount(): Int {
return categoryList.size
}
}
// adapter attach
vp2.adapter = wallpaperPagerAdapter
TabLayoutMediator(tabLayout, vp2) { tab, position ->
//tab.text = catList[position].name
tab.text = "Tab $position"
//WallpaperHttpRequest.printMessage("VP2", position)
}.attach()
型
1条答案
按热度按时间vpfxa7rd1#
我认为这是因为你没有使用片段管理器来示例化这些片段。默认情况下,适配器使用片段管理器来创建前几个页面,但随后你返回匿名创建的片段。而且因为片段管理器只能使用默认工厂,所以你需要将这些参数作为参数传递。
字符串