【问题标题】:TabLayoutMediator Infinite TabsTabLayoutMediator 无限选项卡
【发布时间】:2020-08-20 10:31:53
【问题描述】:

我正在尝试使用带有 ViewPager2 的选项卡编写一个水平日期选择器,但遇到了 TabLayoutMediator。这似乎是我想要完成的完美解决方案,但是当我在 TabLayouMediator 上调用 attach() 时,如果有足够的选项卡,它会锁定主线程,在这种情况下为 40 或 50 毫秒。有没有办法解决?我希望它只会加载和创建可见的选项卡,但它似乎会遍历所有项目并为每个项目实例化选项卡。

这是一个基本示例:

...

val viewPagerAdapter = DemoCollectionAdapter(this)
val tabLayout = binding.tabLayout
val viewPager = binding.viewPager
viewPager.adapter = viewPagerAdapter

TabLayoutMediator(tabLayout, viewPager) { tab, position ->
    tab.text = position.toString()
}.attach()

...

class DemoCollectionAdapter(fragment: Fragment) : FragmentStateAdapter(fragment) {

    override fun getItemCount(): Int = 1000

    override fun createFragment(position: Int): Fragment {
        val fragment = DemoObjectFragment()
        fragment.arguments = Bundle().apply {
            putInt("arg", position + 1)
        }
        return fragment
    }
}

class DemoObjectFragment : Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val binding = DailyBinding.inflate(inflater, container, false)

        binding.positionTextView.text = requireArguments().getInt("arg").toString()

        return binding.root
    }

}

在滑动视图的文档 (https://developer.android.com/guide/navigation/navigation-swipe-view-2#add_tabs_using_a_tablayout) 中,它说您可以拥有无​​限数量的选项卡,但是这会锁定尝试创建它们的主线程似乎很奇怪。

任何关于如何完成此任务的帮助都将不胜感激!

【问题讨论】:

  • 你解决了吗?
  • 不使用标签。我最终使用了两个一起工作的浏览器

标签: android datepicker androidx android-tablayout android-viewpager2


【解决方案1】:

我最终使用了两个共同解决这个问题的浏览器

【讨论】:

  • 嗨拉姆塞兹。您能否发布解决方案以帮助其他人解决同样的问题:Tips for Answering。亲切的问候。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
  • 2019-05-17
  • 1970-01-01
  • 2013-01-19
相关资源
最近更新 更多