答案 0 :(得分:2)
是的,您需要同时实现两者才能使发电机正常工作。有一个示例here。关键部分是:
class MyViewPagerAdapter(val id: String) :
FragmentStatePagerAdapter(supportFragmentManager) {
val titles = arrayOf(getString(R.string.title1),
getString(R.string.title2))
override fun getItem(position: Int) = when (position) {
0 -> MyFragment1.newInstance(id)
1 -> MyFragment2.newInstance(id)
else -> Fragment()
}
override fun getCount() = 2
override fun getPageTitle(position: Int): CharSequence? {
return titles[position]
}
override fun setPrimaryItem(container: ViewGroup, position: Int, `object`: Any) {
super.setPrimaryItem(container, position, `object`)
val currentFragment = `object` as Fragment
if (currentFragment.view != null) { //The first time the view isn't created yet
for (i in 0 until count) {
(container.getChildAt(i) as NestedScrollView).isNestedScrollingEnabled = false
}
val currentNestedScrollView: NestedScrollView = currentFragment.view as NestedScrollView
currentNestedScrollView.isNestedScrollingEnabled = true
container.requestLayout()
}
}
}