Android的片段嵌套在另一个片段:getParentFragment()返回0 [英] Android fragment nested in another fragment: getParentFragment() returns 0

查看:2333
本文介绍了Android的片段嵌套在另一个片段:getParentFragment()返回0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上的情况是这样的:我有一个活动,它显示了三片段中的 FragmentPagerAdapter ,并在其中一个片段,我窝另一个片段 。我添加了片段是这样的:

  fragmentTransaction = getFragmentManager()调用BeginTransaction();
fragmentTransaction.add(rootView.getId(),新MainPageFragment(),LPT2);
fragmentTransaction.commit();

当我尝试调用 getParentFragment()里面的 MainPageFragment(),它返回
怎么可能?我查了这里计算器多个职位,以及其他博客,他们都得出结论,如果你窝另一个中的一个片段,称 getParentFragment 应返回它的父分段。返回表示,父是一个活动作为陈述的此处。这不是这种情况。请告诉我,我在做某种愚蠢的错误:\\

这是片段这是一个孩子的主要活动

 公共类扩展的MainPage片段{私人FragmentTransaction fragmentTransaction;
私人查看rootView;公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
           捆绑savedInstanceState){
        global_var =新的全球();
        rootView = inflater.inflate(R.layout.main_page,集装箱,FALSE);
        返回rootView;
}
@覆盖
公共无效onViewCreated(查看视图,捆绑savedInstanceState){
    fragmentTransaction = getFragmentManager()调用BeginTransaction()。
    fragmentTransaction.add(rootView.getId(),新MainPageFragment(),LPT2);
    fragmentTransaction.commit();
}
}

这是这是一个孩子的片段主页片段

 公共类MainPageFragment扩展片段{
私人查看rootView;
私人片段parentFragment;
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
    rootView = inflater.inflate(R.layout.main_page_list,集装箱,FALSE);
    如果(getParentFragment()== NULL)Log.i(该死的,它);
    返回rootView;
   }@覆盖
公共无效onViewCreated(查看视图,捆绑savedInstanceState){
    // TODO自动生成方法存根
    super.onViewCreated(查看,savedInstanceState);
}
}

请告诉我,如果我需要提供更多的code,我摘录的东西像的ListView 等等,因为我认为这将是无关紧要的。

编辑:我使用android.support.v4.app <​​/ P>

解决方案

检查是否有传递FragmentManager refrence从您FragmentPagerAdapter所有的碎片,然后用getParentFragment()。可能这会帮助你。

Basically the situation is like this: I have an Activity, which displays a three Fragments within a FragmentPagerAdapter, and within one of these Fragment, I nest another Fragment. I added the fragments like this:

fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(rootView.getId(), new MainPageFragment(), "lpt2");
fragmentTransaction.commit();

When I try to call getParentFragment() inside of MainPageFragment(), it returns null. How can that be? I checked multiple posts here on stackoverflow as well as on other blogs, and they all came to the conclusion that if you nest one fragment within another one, calling getParentFragment should return its parent fragment. Returning null means, that the parent is an activity as stated here. Which is not the case. Please tell me I'm making some kind of silly mistakes :\

This is the Fragment that is a child to the main Activity

public class MainPage extends Fragment {

private FragmentTransaction fragmentTransaction;
private View rootView;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
           Bundle savedInstanceState) {
        global_var = new Global();
        rootView = inflater.inflate(R.layout.main_page, container, false);
        return rootView;
}
@Override
public void onViewCreated (View view, Bundle savedInstanceState) {      
    fragmentTransaction = getFragmentManager().beginTransaction();
    fragmentTransaction.add(rootView.getId(), new MainPageFragment(), "lpt2");
    fragmentTransaction.commit();
}
}

This is the Fragment which is a child to the Main Page Fragment

public class MainPageFragment extends Fragment{
private View rootView;
private Fragment parentFragment;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.main_page_list, container, false);
    if (getParentFragment() == null) Log.i("damn", "it");
    return rootView;
   }

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onViewCreated(view, savedInstanceState);
}
}

Please tell me if I need to provide more code, I extracted things like ListView and so on because I thought it would be irrelevant.

Edit: I'm using android.support.v4.app

解决方案

check that you have pass FragmentManager refrence to all your fragments from your FragmentPagerAdapter and then use getParentFragment(). may be this will help you.

这篇关于Android的片段嵌套在另一个片段:getParentFragment()返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆