Fragment嵌套Fragment时候。子类fragment调用父容器Fragment方法

业务场景:有的时候我们的页面可能是Activity 嵌套多个Fragment ..其中某个Fragment 又嵌套多个Fragment。

其中某个子Fragment  定义为  NewsFragment  。父容器 Fragment 定义为 ShouYeMainFragment

ShouYeMainFragment  嵌套   NewsFragment  。此时如果NewsFragment  想调用 ShouYeMainFragment 里面的方法怎么做呢?

解决办法:

通过NewsFragment  获取  父类Fragment 的容器管理器的所有Fragment 集合。然后 遍历。找到父容器Fragment.执行它的方法。

代码如下:

List<Fragment>list=(List<Fragment>)NewsFragment.this.getFragmentManager().getFragments();
for(Fragment f:list){
    if(f!=null&&f instanceof ShouYeMainFragment){
        ((ShouYeMainFragment) f).changView();
        break;
    }
}



猜你喜欢

转载自www.cnblogs.com/ganchuanpu/p/10478160.html
今日推荐