无法从FrameLayout中删除片段?

时间:2012-03-28 16:29:14

标签: android android-layout android-fragments

我有横向模式的布局,左侧显示ListView,右侧显示FrameLayout。从列表中选择一个项目时,另一个片段将添加到FrameLayout

MyFragment myFragment = (MyFragment) fragmentManager.findFragmentById(R.id.myFrameLayout);
FragmentTransaction ft = fragmentManager.beginTransaction();
if (myFragment == null) {
    myFragment = new MyFragment(uri);
    ft.replace(R.id.myFrameLayout, playerFragment);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.commitAllowingStateLoss();
}

稍后我在列表视图中按删除并删除列表中的最后一项,我尝试删除片段以便不显示任何内容,但它不起作用,我的片段仍然在屏幕上。删除代码是:

MyFragment myFragment = (MyFragment) fragmentManager.findFragmentById(R.id.myFrameLayout);
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.remove(myFragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
ft.commitAllowingStateLoss();

为什么没有从视图中移除它的任何想法?

6 个答案:

答案 0 :(得分:12)

@DaveJohnston我已经尝试过你的代码删除片段我刚刚模塑....谢谢:)

嘿,我不知道这是否是您问题的解决方案,但您尝试这种方式会起作用:

添加片段的方法:

YourFragment yourFrament;

//Add your Fragment
public void addYourFragment(){
yourFragment = new YourFragment();

FragmentTransaction transation = getSupportFragmentManager().beginTransaction();
transation.replace(R.id.yourFlowLayout, yourFragment);

        transation.commit();

}   

删除片段的方法:

 //Remove Fragment
    public void removeYourFragment(){
     FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            if (yourFragment != null) {           
                transaction.remove(yourFragment);
                transaction.commit();
                transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
                yourFragment = null;
            }
    }

答案 1 :(得分:3)

你在xml中声明片段吗?引用来自谷歌的Dianne Hackborn:

  

以XML格式定义片段   主要用于将要留在身边的东西。如果你是   要添加和删除,您应该始终如一地动态地执行此操作。

希望有所帮助

答案 2 :(得分:0)

当您通过Id找到您的片段时,您引用了片段容器而不是片段本身。 添加片段然后通过TAG查找时使用TAG:

MyFragment myFragment = (MyFragment) fragmentManager.findFragmentByTag("yourTag");

答案 3 :(得分:0)

我很难在未通过XML设置<fragment>时找到此问题的SO引用,但确实动态地设置每个Fragment FragmentTransaction<FrameLayout>

简化Gru的解决方案,为我解决的问题是简单地将.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)添加到我的替换事务中并且它有效!现在实际上隐藏了以前的片段。

我想其他过渡也会起作用。

答案 4 :(得分:0)

private static String TAG_FRAGMENT="MyFragment";
MyFragment myFragment = (MyFragment)   fragmentManager.findFragmentById(R.id.myFrameLayout);
 FragmentTransaction ft = fragmentManager.beginTransaction();
 if (myFragment == null) {
      myFragment = new MyFragment(uri);
     ft.replace(R.id.myFrameLayout, playerFragment,TAG_FRAGMENT);
     ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
     ft.addToBackStack(TAG_FRAGMENT);
     ft.commit();
 }

在更换片段后,在轻松删除片段后使用ft.addToBackStack(null)方法。

Fragment fragment = getSupportFragmentManager().findFragmentByTag(TAG_FRAGMENT);

if(fragment != null)
getSupportFragmentManager().beginTransaction().remove(fragment).commit();

答案 5 :(得分:0)

如果您更换Fragment中的Fragment时未删除FrameLayout?您可以通过在调用常规替换提交之前对其FragmentTransaction.TRANSIT_FRAGMENT_CLOSE进行单独的删除过渡来删除它。

Fragment removeThisFragment;//Keep a reference to the fragment you want to remove
...
 FragmentManager fragmentManager =  getSupportFragmentManager();
 FragmentTransaction transaction =  fragmentManager.beginTransaction();

 // This fragment "sometimes" do not get replaced so we remove it here if it's added.
        if(removeThisFragment!=null&&removeThisFragment.isAdded()) {
            transaction =  fragmentManager.beginTransaction();
            transaction.remove(removeThisFragment);
            transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
            transaction.commitAllowingStateLoss();
        }

        // update the main content by replacing fragments
        transaction =  fragmentManager.beginTransaction();
        transaction.replace(R.id.content_frame, fragment, TAG_MAIN_FRAGMENT).commit();

但是我建议首先尝试弄清楚为什么片段没有被替换,检查它是否做了一些繁重的工作或者让它不被替换?但如果您需要快速临时修复,请尝试以上代码。