zoukankan      html  css  js  c++  java
  • 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;
        }
    }



  • 相关阅读:
    [Leetcode]@python 76. Minimum Window Substring
    [Leetcode]@python 75. Sort Colors
    HTNL表单
    第二天
    开学心德
    HTML表单
    网页制作
    2nd day
    开课心得
    CF10D/POJ2127 LCIS 题解
  • 原文地址:https://www.cnblogs.com/ganchuanpu/p/10478160.html
Copyright © 2011-2022 走看看