Retrieve a Fragment from a ViewPager

前端 未结 23 2565
逝去的感伤
逝去的感伤 2020-11-21 11:13

I\'m using a ViewPager together with a FragmentStatePagerAdapter to host three different fragments:

  • [Fragment1]
  • [Fragment2]<
23条回答
  •  梦毁少年i
    2020-11-21 11:54

    I implemented this easy with a bit different approach.

    My custom FragmentAdapter.getItem method returned not new MyFragment(), but the instance of MyFragment that was created in FragmentAdapter constructor.

    In my activity I then got the fragment from the adapter, check if it is instanceOf needed Fragment, then cast and use needed methods.

提交回复
热议问题