我是第一次与Navigation UI Architecture Component
我正在尝试使用ViewPager。我想将参数传递给添加到viewpager
我没有找到任何与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
答案 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