不是更换所有片段

不是更换所有片段

本文介绍了片段取代()不是更换所有片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我调用add()的片段A和B具有相同viewId,然后尝试调用替换()与C片段的viewId,只有一个片段是越来越拆下,用碎片B和C.根据碰得该文档,A和B都应该用C来代替......还是我阅读文档错了?

下面是一个组合,做这样的:

 公共类FragmentActivity扩展SherlockFragmentActivity {
    @覆盖
    公共无效的onCreate(最终捆绑savedInstanceState){
        super.onCreate(savedInstanceState);        的setContentView(R.layout.main);        。。getSupportFragmentManager()调用BeginTransaction()加(R.id.fragment,新碎裂())加(R.id.fragment,新FragmentB())提交();        ((按钮)findViewById(R.id.swap))setOnClickListener。(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(最终查看视图){
                。getSupportFragmentManager()调用BeginTransaction()取代(R.id.fragment,新FragmentC())提交();
            }
        });
    }
}


解决方案

综观文档,.replace调用需要一个片段作为一个参数的方法。所以我猜想,这只是为了更换一个片段。我真的不明白,你为什么会在第一时间添加两个片段相同的ID。

If I call add() for fragments A and B with the same viewId and then try to call replace() on that viewId with fragment C, only fragment A is getting removed, ending up with fragments B and C. According to the docs, BOTH A and B should be replaced by C...or am I reading the docs wrong?

Here's one combination that does this:

public class FragmentActivity extends SherlockFragmentActivity {
    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        getSupportFragmentManager().beginTransaction().add(R.id.fragment, new FragmentA()).add(R.id.fragment, new FragmentB()).commit();

        ((Button) findViewById(R.id.swap)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View view) {
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment, new FragmentC()).commit();
            }
        });
    }
}
解决方案

Looking at the docs, .replace calls a method that takes a fragment as a parameter. So I would guess that it is only meant to replace one fragment. I don't really understand why you would add two fragments to the same id in the first place.

这篇关于片段取代()不是更换所有片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 12:02