3

使用ChildFragmentManager时工作,我想作为一个孩子添加一个片段到另一个片段
我使用ChildFragmentManager

片段替换未在Android中

这是我ChildFragmentManagerActivity.java
ChildFragment.java
ParentFragment.java
其布局如下
activity_childfragmentmanager.xml
layout_parentfragment.xml
layout_childfragment.xml

我能够加入ParentFragment成功。请检查下面的图片
parent_fragment.png

但是当我尝试添加ChildFragment,则出现如下
child_fragment

我想通过替换被添加的子片段内容前面的布局
在前提前

+0

“我想通过替换以前的布局将子片段添加为内容。”你能更清楚地解释一下吗?什么是以前的布局? – koso

+0

嗨koso感谢您的评论。红色部分(** layout_parentfragment **)是** ParentFragment **的布局。我正在为它添加一个子片段。蓝色部分(** layout_childfragment **)是** ChildFragment **的布局。我希望蓝色被红色代替,完全类似于片段替换方法。但它只是添加ChildFragment而不是替换它。 –

+0

如果你在parent_layout的开头添加frameLayout,那么height = match_parent和visiblity =消失了。并且,当您将片段添加到frameLayout时,请使frameLayout可见,因此它将覆盖整个父级片段布局。我认为你想要做对吗? – koso

回答

4

事务不会删除将用于事务的容器中已存在的视图。要删除这些视图,您需要将ParentFragment的初始内容作为片段进行换行,并用子片段替换(使用replace事务而不是add事务)。我做了一些修改你的代码,看看下面:

ParentFragment:

public class ParentFragment extends Fragment { 

private static final int CONTAINER_ID = 0x2222; 
private static final String INITIAL_FRAG = "initial_fragment"; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    FrameLayout wrapper = new FrameLayout(getActivity()); 
    wrapper.setId(CONTAINER_ID); 
    // look for our two possible fragments, if we don't find the 
    // InitialContentFragment add it 
    if (getChildFragmentManager().findFragmentByTag(INITIAL_FRAG) == null) { 
     InitialContentFragment initContent = new InitialContentFragment(); 
     Bundle args = new Bundle(); 
     args.putString("text", 
       "I'm the initial content fragment in the parent fragment"); 
     initContent.setArguments(args); 
     getChildFragmentManager().beginTransaction() 
       .add(CONTAINER_ID, initContent, INITIAL_FRAG).commit(); 
    } 
    return wrapper; 
} 

public void requestFragmentTransaction() { 
    FragmentTransaction fragmentTransaction = getChildFragmentManager() 
      .beginTransaction(); 
    ChildFragment childFragment = new ChildFragment(); 
    Bundle args = new Bundle(); 
    args.putString("text", "Hi I am Child Fragment"); 
    childFragment.setArguments(args); 
    fragmentTransaction.replace(CONTAINER_ID, childFragment, "ChildFragment"); 
    fragmentTransaction.addToBackStack(null); 
    fragmentTransaction.commit(); 

} 

}

其中InitialContentFragment是:

public static class InitialContentFragment extends Fragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     // inflate the layout file that would normally be in the 
     // ParentFragment at start 
     View view = inflater.inflate(R.layout.layout_parentfragment, 
       container, false); 
     Bundle bundle = getArguments(); 
     final String text = bundle.getString("text"); 
     TextView textView = (TextView) view.findViewById(R.id.textView1); 
     textView.setText(text); 
     Button button = (Button) view.findViewById(R.id.button1); 
     button.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       ParentFragment parent = (ParentFragment) InitialContentFragment.this 
         .getParentFragment(); 
       parent.requestFragmentTransaction(); 
      } 
     }); 
     return view; 
    } 
} 

作为一个侧面说明,不要像你那样忽略try-catch块。

+0

我已经使用以下代码实现了Navigation Drawer的代码。http://stackoverflow.com/questions/23678513/swapable-tabs-in-slider-menu-fragment和它的工作绝对好,但当你点击导航抽屉列表片段时的问题。选项卡主机选项卡正在出现并且非常棒,但返回到其他活动并返回到选项卡主机活动时。这些选项卡正在出现,但选项卡主机的线性布局背景显示在tabhost下方的单击和单击两次的图像中。 i.stack.imgur.com/CzIsC.png i.stack.imgur.com/Dmxn3.png –

+0

@ B.rohitNare请发布一个提供您问题的所有详细信息的新问题。 – Luksprog

+0

http://stackoverflow.com/questions/26483069/navigation-drawer-with-tabhost-and-viewpager @Luksprog 嘿队友跟着链接 –