![](https://img.haomeiwen.com/i25021128/fcc7c4be08d9a528.png)
最近在项目开发当中使用到ViewPage2
控件,有一个需求需要获取当前的Fragment
来实现的,这里分享一下,我的获取方法。
在Actvity
获取Fragment:
supportFragmentManager.findFragmentByTag("f$adapter.getItemId(index)")
在Fragment
获取Fragment
:
childFragmentManager.findFragmentByTag("f$adapter.getItemId(index)")
我看到有的文章是使用下面的代码获取的:
findFragmnetByTag("f$index")
其实是不太建议这样使用的,因为在开发当中,我们可以重写Adapter
的getItemId()
方法,可能会出现有问题。
网友评论