带有 TabLayout 的 ViewPager2 无法正确导航大量选项卡

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

我有 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 个或更多数字选项卡时,视图寻呼机滚动到最后位置。选项卡映射不正确。这是片段适配器代码:

 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()
android android-fragments android-tablayout fragmentstatepageradapter fragmentstateadapter
1个回答
0
投票

我认为这是因为您在不使用片段管理器的情况下实例化这些片段。默认情况下,适配器使用片段管理器创建前几页,但随后您返回匿名创建的片段。并且由于片段管理器只能使用默认工厂,因此您需要将这些参数作为参数传递。

override fun createFragment(position: Int): Fragment
    {
        return fragmentManager.fragmentFactory.instantiate(
            classLoader,
            WallpaperFragment::class.java.name
        ).also {
            (it as WallpaperFragment).let { fr ->
                fr.arguments = Bundle().also { bundle -> bundle.putInt("ID_ARG", categoryList[position].id)
                bundle.putInt("PPREMIUM_ARG", isPremium) }
            }
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.