我正在尝试使用包含片段的ViewPager。 当我使用FragmentPagerAdapter添加片段时,它不起作用,并且我注意到未调用'onCreateView'函数。 我尝试遵循一些教程,但似乎找不到问题。
代码如下:
class GuestListFragment : Fragment()
{
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
println("onCreateView")
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_guest_list, container, false)
}
}
class EventPagerAdapter(fragmentManager: FragmentManager): FragmentPagerAdapter(fragmentManager)
{
override fun getCount(): Int
{
return 0
}
override fun getItem(position: Int): Fragment
{
println("getItem")
return GuestListFragment()
}
}
class EventActivity : AppCompatActivity()
{
private lateinit var viewPager: ViewPager
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_event)
viewPager = findViewById(R.id.guests_view_pager)
val pagerAdapter = EventPagerAdapter(supportFragmentManager)
pagerAdapter.getItem(0)
viewPager.adapter = pagerAdapter
})
}
}
答案 0 :(得分:1)
Change return count to 1 in getCount()
override fun getCount(): Int
{
return 1
}