我有横向模式的布局,左侧显示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; } }