【问题标题】:Fragment duplication on Fragment Transaction分片交易的分片重复
【发布时间】:2011-07-14 17:04:03
【问题描述】:

好的,每当我尝试替换应用程序中的片段时,它只会将片段添加到另一个片段所在的容器内,并留下当前片段。我尝试调用替换并引用包含片段的视图,并通过引用片段本身。这些都不起作用。我可以使用片段事务管理器将片段添加到视图中,但即使我在添加后尝试将其删除,它也不起作用。任何帮助,将不胜感激。这是我的文件。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //Setup the actionabar. Use setDrawableBackground to set a background image for the actionbar.
    final ActionBar actionbar = getActionBar();
    actionbar.setDisplayShowTitleEnabled(false);
    actionbar.setDisplayUseLogoEnabled(true);
    actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionbar.addTab(actionbar.newTab().setText(R.string.home_tab_text).setTabListener(this),true);
    actionbar.addTab(actionbar.newTab().setText(R.string.insert_tab_text).setTabListener(this));

    Fragment fragment = new insert_button_frag();
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(R.id.button_fragment, fragment);
    transaction.addToBackStack(null);

    transaction.commit();
}

这是布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:orientation="vertical"
        android:id="@+id/button_fragment_container"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <fragment
            android:name="com.bv.silveredittab.home_button_frag"
            android:id="@+id/button_fragment"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <fragment
            android:name="com.bv.silveredittab.quick_insert_frag"
            android:id="@+id/quick_insert_frag"
            android:layout_width="350dip"
            android:layout_height="fill_parent" />

        <fragment
            android:name="com.bv.silveredittab.editor_frag"
            android:id="@+id/editor_frag"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />

    </LinearLayout>

</LinearLayout>

这是片段代码

public class insert_button_frag extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        return inflater.inflate(R.layout.insert_buttons,container, false);
    }
}

就像我说的那样。我已经尝试引用片段父视图来替换它,片段本身(通过 id),它仍然只添加新片段,在原始片段所在的包含视图中。

【问题讨论】:

  • insert_buttons.xml 布局文件中有什么内容?

标签: android transactions android-fragments


【解决方案1】:

我通过在布局中使用占位符然后在运行时将我的片段附加到它来解决这个问题。

像你一样,如果我在我的 xml 布局中实例化我的 Fragment,那么在运行时将其替换为另一个 Fragment 后内容将保持可见。

【讨论】:

  • 没错。看来replace方法没用了。
【解决方案2】:

问题出在这一行:

transaction.replace(R.id.button_fragment, fragment);

replace 有两种重载形式。在它们中,第一个参数是要替换的 Fragment 的 container,而不是 Fragment 本身。所以,在你的情况下,你需要打电话

transaction.replace(R.id.button_fragment_container, fragment);

编辑:我在您的问题中看到您已经尝试了这两种方法。我已经测试并验证了这种行为。这似乎是 FragmentTransaction API 中的一个错误。

Edit2:毕竟不是错误。您根本无法替换在布局文件中静态添加的片段。您只能替换您以编程方式添加的那些 (Android: can't replace one fragment with another)

【讨论】:

    【解决方案3】:

    我遇到了同样的问题。看看这个链接:Android Fragment Duplication

    我想知道您将容器传递给 inflater.inflate() 方法这一事实是否会导致它在旧片段中创建新片段而不是批量替换。在工作版本中,我一直在为我的充气机提供“null”。

    这是我正在使用的版本中的要点...

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
       View newFrag = new View(getActivity().getApplicationContext());
       newFrag = inflater.inflate(R.id.frag, null);
       return newFrag;
    }
    

    【讨论】:

    • 我看了看,但我认为它并不真正适用于我的情况。我没有以编程方式填充我的片段。由于某种原因,我的代码没有找到现有的片段,所以它没有替换它。它甚至不会产生任何错误或警告。真的很混乱。
    • 抱歉没有帮助。我编辑了我的答案:充气机,但如果这没有帮助,我必须使用 IDE 对其进行故障排除。
    • 我试过这个 return inflater.inflate(R.layout.home_buttons,null, false);但这也没有用
    • 我使用 isAdded() 和 isVisible() 函数对其运行了一些 if 语句。它说它已添加到活动中,但不可见,当它清晰可见时,我可以看到它。我想知道这是什么原因造成的......
    • 删除功能不适用于我的任何片段...wth?我认为使用片段事务管理器会很容易......
    【解决方案4】:

    google developer guide 非常令人困惑,因为它首先显示在您的 xml 中实现硬编码片段。但是如果你真的想用新的片段替换现有的片段,那么你应该在运行时添加它(第一个)。

    official site 的状态是什么

    FragmentTransaction 替换(int containerViewId、Fragment 片段、String 标签)

    替换已添加到容器中的现有片段。这是 本质上与为所有当前调用 remove(Fragment) 相同 添加了使用相同 containerViewId 添加的片段,然后 add(int, Fragment, String) 与此处给出的参数相同。

    你应该注意到它说替换一个现有的片段被添加到容器

    所以,你的 xml 应该看起来像 someActivity.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        ...
        tools:context="someActivity">
    
        <ImageView
            .../>
    
        <LinearLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:gravity="bottom"/>
    
    </RelativeLayout>
    

    比在你的活动中做 OnCreate()

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            [...]
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment_container, new FirstFragment).commit();
    
        }
    

    您可以轻松地用动画替换片段并将其添加到后台堆栈以保存其状态。

    private SecondFrag getSecondFrag(){
        if(secondFrag == null)
            secondFrag = new SecondFrag()
        return secondFrag;
    }
    
    private void openRechargeFragment(){
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.setCustomAnimations(R.anim.show_frag, R.anim.hide_frag,
                    R.anim.show_frag, R.anim.hide_frag);
            ft.replace(R.id.fragment_container, getSecondFrag(), "myTAG");
            ft.addToBackStack(null);
            ft.commit();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-06
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多