为了账号安全,请及时绑定邮箱和手机立即绑定

用活动组内的另一个片段替换片段

用活动组内的另一个片段替换片段

人到中年有点甜 2019-07-23 19:44:37
用活动组内的另一个片段替换片段我在组活动中有一个片段,我想用另一个片段替换它:FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();SectionDescriptionFragment bdf = new SectionDescriptionFragment();ft.replace(R.id.book_description_fragment, bdf);ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);ft.addToBackStack(null);ft.commit();它在没有使用活动组的情况下作为单独的项目完成时工作正常,因为控件进入getview()内部,每个东西都可以在log cat中正常工作,但是没有视图可见,甚至没有出现任何异常,我希望书籍详细信息片段到由部分细节片段替换。书籍详细信息片段的Xml具有id book_description_fragment,而用于部分描述片段的xml具有id section_description_fragment。上面的代码是项目的onClick方法,我希望当用户点击水平滚动视图中的项目时,片段会发生变化。
查看完整描述

3 回答

?
吃鸡游戏

TA贡献1829条经验 获得超7个赞

以XML格式编码的片段无法替换。如果你需要用另一个片段替换片段,你应该首先动态添加它们。


注意:R.id.fragment_container是您将片段带到的活动中所选择的布局或容器。


// Create new fragment and transaction

Fragment newFragment = new ExampleFragment();

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();


// Replace whatever is in the fragment_container view with this fragment,

// and add the transaction to the back stack if needed

transaction.replace(R.id.fragment_container, newFragment);

transaction.addToBackStack(null);


// Commit the transaction

transaction.commit();


查看完整回答
反对 回复 2019-07-23
?
LEATH

TA贡献1936条经验 获得超6个赞

我已经用完美的方法来管理片段 替换生命周期

它只用一个新片段替换当前片段,如果它不相同并且它不在backstack中(在这种情况下它将弹出它)。

它包含几个选项,就好像您希望将片段保存在backstack中一样。

=> 见这里的要点

使用此活动和单个活动,您可能希望将其添加到您的活动中:

@Overridepublic void onBackPressed() {
    int fragments = getSupportFragmentManager().getBackStackEntryCount();
    if (fragments == 1) {
            finish();
            return;
    }

    super.onBackPressed();}


查看完整回答
反对 回复 2019-07-23
  • 3 回答
  • 0 关注
  • 554 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信