具有 3 个以上选项卡的 ViewPager

ViewPager with more than 3 Tabs

我正尝试在我的应用程序中使用片段实现 3 个以上的选项卡。在 FragmentPagerAdapter 中,有一种方法 getItem returns Frgament 所以我的问题是我们应该使用什么逻辑来缩短代码,因为当我们 return 所有可能是 10 的片段时它会变得如此混乱或更多

这是我的 PagerAdapter

class PageAdapter(fragmentManager: FragmentManager, private val tabCount: Int) :
FragmentPagerAdapter(fragmentManager, tabCount) {

override fun getCount() = tabCount

override fun getItem(position: Int): Fragment {
    when (position) {
        0 -> return NewsCategoryOneFragment()
        1 -> return NewsCategoryTwoFragment()
        2 -> return NewsCategoryThreeFragment()
        3 -> return NewsCategoryFourFragment()
        4 -> return NewsCategoryFiveFragment()
    }
    return NewsCategoryOneFragment()
}

override fun getPageTitle(position: Int): CharSequence? {
    when (position) {
        0 -> return "Buzzfeed"
        1 -> return "CNN"
        2 -> return "ESPN"
        3 -> return "Fortune"
        4 -> return "Fox News"
    }
    return null
}
}

任何人都可以建议。 谢谢

您应该将选项卡列表传递到页面适配器并从您的选项卡列表按位置创建片段。

例如:

data class TabItem(val title: String)

val tabItemList = listOf(
    TabItem("Buzzfeed"),
    TabItem("CNN"),
    TabItem("CNN"),
    TabItem("Fortune"),
    TabItem("Fox News")
)

class PageAdapter(
    fragmentManager: FragmentManager,
    private val tabItemList: List<TabItem>
) : FragmentPagerAdapter(fragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {

    override fun getCount() = tabItemList.size

    override fun getItem(position: Int): Fragment {
        return NewsCategoryFragment.newInstance(position)
    }

    override fun getPageTitle(position: Int): CharSequence? {
        return tabItemList[position].title
    }
}

class NewsCategoryFragment : Fragment() {
    // ...

    companion object {
        const val KEY_CATEGORY = "category"

        fun newInstance(category: Int): NewsCategoryFragment {
            return NewsCategoryFragment().apply {
                arguments = Bundle().apply { putInt(KEY_CATEGORY, category) }
            }
        }
    }
}

好的,首先为 pageTitle 创建一个 BaseFragment 并进一步 properties/funtions:

open class BaseFragment: Fragment() {
    var pageTitle: String = "Default Title"
}

您的自定义 class 扩展了这个 BaseFragment,它将像:

class CustomFragment: BaseFragment() {
    companion object {
       fun newInstance(pageTitle: String): CustomFragment {
       val fragment = CustomFragment()
       val args = Bundle()
       args.putString("title", pageTitle)
       fragment.arguments = args
       return fragment
    }
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    val args = Bundle()
    pageTitle = args.getString("title")!!
    }
}

然后创建数组:

val fragmentList = listOf(
    CustomFragment.newInstance("title1"),
    CustomFragment.newInstance("title2"),
    CustomFragment.newInstance("title3"),
    CustomFragment.newInstance("title4"),
    CustomFragment.newInstance("title5")
)

最后,更改您的 PagerAdapter:

class PageAdapter(fragmentManager: FragmentManager, private val fragmentList: List<BaseFragment>) :
    FragmentPagerAdapter(fragmentManager, fragmentList.size) {

    override fun getCount() = fragmentList.size

    override fun getItem(position: Int): Fragment {
        return fragmentList.get(position)
    }

    override fun getPageTitle(position: Int): CharSequence? {
        return fragmentList.get(position).pageTitle
    }
}