Problem putting a fragment into another fragment

女生的网名这么多〃 提交于 2020-01-16 19:11:29

问题


I Tried putting a Fragment into a FrameLayout inside another Fragment via FragmentManager and FragmentTransaction (from android.support.v4.app). The container fragment has a button and a TextView on top and a FrameLayout at the bottom (I create the layout programmatically and i don't want to hurt your eyes with all of that). The CreateView() works just fine and i cann access the FrameLayout at the bottom of the container and add or remove View dynamically as I please via

@Override
public void onClick(View button) {      
    FrameLayout frame = (FrameLayout)findViewById(DETAIL_CONTENT_FRAME);
    ImageView im = new ImageView(this);
    im.setImageResource(R.drawable.test);
    frame.addView(im);
}

but when I try to add a fragment instead of an ImageView to the frameLayout the code compiles perfectly but the desired fragment doesn't appear after the onClickListener() method is called. I checked the onCreateView() method of the fragment and it returns a proper view...

@Override
public void onClick(View button) {
    ServerDialogFragment serverDialog = new ServerDialogFragment();
    FragmentTransaction addDialog = getSupportFragmentManager().beginTransaction();
    addDialog.add(DETAIL_CONTENT_FRAME, serverDialog);
    addDialog.commit();
}

Do you have an answer to this ?

PS: I once tried adding fragments into other fragments and it worked, but they were simple fragments only holding ImageViews.


回答1:


Fragments inside of other fragments is not supported at this time. See:

  • Fragment Inside Fragment
  • Fragments within Fragments
  • Android: Can you nest Fragments?


来源:https://stackoverflow.com/questions/7321831/problem-putting-a-fragment-into-another-fragment

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!