android – 无法从FrameLayout中删除片段?

前端之家收集整理的这篇文章主要介绍了android – 无法从FrameLayout中删除片段?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有横向模式的布局,左侧显示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();

任何想法为什么它没有从视图中删除

解决方法

@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;
            }
    }
原文链接:https://www.f2er.com/android/316573.html

猜你在找的Android相关文章