2011-03-14 46 views
44

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

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)和还在,它只是增加了新的片段,含内查看原始片段中。

+0

什么是insert_buttons.xml布局文件? – Peter

回答

33

我在我的布局使用占位符,然后在运行时我的片段连接到它解决了这一点。像你一样,如果我在我的xml布局中实例化我的Fragment,那么在运行时用另一个Fragment替换内容后,内容将保持可见状态。

+4

代替home_buttons片段这就是我最终要做的事情。有点迟疑不是吗? – Shaun

+2

哈哈,是的!错误或*功能*我想知道。 – Damian

+1

准确。看来替代方法是无用的。 – Tastybrownies

2

我有同样的问题。看看这个链接: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; 
} 
+0

我看了一下,但我不认为这真的适用于我的情况。我没有编程我的碎片。出于某种原因,我的代码没有找到现有的片段,所以它不能代替它。它甚至不会产生任何错误或警告。它真的很混乱。 – Shaun

+0

对不起,没有帮助。我编辑了我的答案:inflater,但如果这没有帮助,我必须去解决它与w/IDE。 – Peter

+0

我试过这个返回inflater.inflate(R.layout.home_buttons,null,false);但没有工作要么 – Shaun

18

问题是这样的线:

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

replace有两个重载形式。在他们中,第一个参数是要被替换的碎片的容器,而不是碎片本身。所以,你的情况,你需要调用

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

编辑:我看到你的问题,你曾经尝试都。我已经测试并验证了该行为。这似乎是FragmentTransaction API中的一个错误。

EDIT2:不是一个错误毕竟。你根本无法替代的布局文件静态加入片段。你只能取代你以编程方式添加的那些(Android: can't replace one fragment with another

0

google developer guide是相当混乱的,因为它首先显示在你的xml中实现硬编码片段。但是,如果你真的想用新的替换现有的片段,那么你应该在运行时添加它(第一批)。

什么official site状态是

FragmentTransaction替换(INT containerViewId,片段的片段,字符串标签)

替换被添加到容器中的现有片段。这是 基本上与为所有当前所有 调用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(); 
}