Get current fragment with ViewPager2

后端 未结 11 1993
野的像风
野的像风 2020-12-24 01:15

I\'m migrating my ViewPager to ViewPager2 since the latter is supposed to solve all the problems of the former. Unfortunately, when using it with a

11条回答
  •  情歌与酒
    2020-12-24 01:36

    The ViewPagerAdapter is intended to hide all these implementation details which is why there is no straight-forward way to do it.

    You could try setting and id or tag on the fragment when you instantiate it in getItem() then use fragmentManager.findFragmentById() or fragmentManager.findFragmentByTag() to retrieve.

    Doing it like this, however, is a bit of a code smell. It suggests to me that stuff is being done in the activity when it should be done in the fragment (or elsewhere).

    Perhaps there is another approach to achieve what you want but it's hard to give suggestions without knowing why you need to get the current fragment.

提交回复
热议问题