我已将FragmentStatePageAdapter设置为在相同布局和不同数据(用户配置文件)的片段之间滑动。 当我给它列出2个项目时,一切都很好。 如果我给它3个或更多项目,它将创建片段视图,但是在第三次滑动内容上,无论我向哪个方向滑动(向前,向前,向前,向后),都变成空白。如果在仍然按住屏幕的情况下准确地滑动到第三项,我可以看到它的有效内容,但是当我完成滑动释放时,所有屏幕都消失了。
页面适配器的设置如下。 (swipeList是用户ID的列表,swipeEntityType == 0,每个用户内容由通过捆绑传递的“ uid”加载)
class SwipePagerFragment : Fragment() {
private lateinit var parent: MainActivity
private lateinit var state: State
private lateinit var ctx: Context
private lateinit var swipeList: MutableList<Int>
private lateinit var pager: ViewPager
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_swipe_pager, container, false)
parent = activity as MainActivity
state = parent.state
ctx = state.ctx
swipeList = state.swipeList
pager = view.findViewById(R.id.fragmentSwipePager_pager)
val pagerAdapter = ScreenSlidePagerAdapter(childFragmentManager)
pager.adapter = pagerAdapter
return view
}
private inner class ScreenSlidePagerAdapter(fm: FragmentManager) : FragmentStatePagerAdapter(fm) {
override fun getCount(): Int = swipeList.size
override fun getItem(position: Int): Fragment = when (state.swipeEntityType) {
0 -> {
makeUserFragment(swipeList[position])
}
else -> {
makeEventFragment(swipeList[position])
}
}
}
private fun makeUserFragment(uid: Int): UserFragment {
val bundle = Bundle()
bundle.putInt("uid", uid)
val frag = UserFragment()
frag.arguments = bundle
return frag
}
private fun makeEventFragment(eid: Int): EventFragment {
val bundle = Bundle()
bundle.putInt("eid", eid)
val frag = EventFragment()
frag.arguments = bundle
return frag
}
}
答案 0 :(得分:0)
设置
pager.offscreenPageLimit = swipeList.size
解决了我的问题