Android Jetpack navigationUI,如何传递数据以查看寻呼机片段

时间:2019-02-24 14:42:52

标签: android navigation android-viewpager android-jetpack

我是第一次与Navigation UI Architecture Component

合作

我正在尝试使用ViewPager。我想将参数传递给添加到viewpager

的Fragments

我没有找到任何与viewpager相关的解决方案

我的代码

class StoriesPagerAdapter(fragmentManager: FragmentManager?)
    :FragmentPagerAdapter(fragmentManager){

    private val sections= arrayListOf("science","technology","business","world","movies","travel")

    override fun getItem(position: Int): Fragment {

        val bundle=Bundle()
        bundle.putString("section",sections[position])
        Navigation.createNavigateOnClickListener(R.id.action_masterFragment_to_timesListFragment,bundle)
        return TimesListFragment()
    }

    override fun getCount(): Int {

        return sections.size

    }


    override fun getPageTitle(position: Int): CharSequence? {
        return sections[position]
    }
}

我的导航图

  • MasterFragment->包含TabLayout,ViewPager

  • 通过section

  • FragmentPagerAdapter密钥传递给ListFragment
  • ListFragment->显示基于section键的api响应

  • DetailsFragmenst

1 个答案:

答案 0 :(得分:0)

您可以在片段实例中传递数据:

class StoriesPagerAdapter(fragmentManager: FragmentManager?)
:FragmentPagerAdapter(fragmentManager){

private val sections= arrayListOf("science","technology","business","world","movies","travel")

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

override fun getCount(): Int {

    return sections.size

}


override fun getPageTitle(position: Int): CharSequence? {
    return sections[position]
 }
}

您必须根据此answer.

使用导航架构来管理类似于旧方法的viewPager