gpt4 book ai didi

android - 无法从 FrameLayout 中删除 fragment ?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:15:02 25 4
gpt4 key购买 nike

我有一个横向模式的布局,它在左侧显示一个 ListView,在右侧显示一个 FrameLayout。当从列表中选择一个项目时,另一个 fragment 被添加到 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();
}

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

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

知道为什么它没有从 View 中删除吗?

最佳答案

@DaveJohnston 我试过你的代码来删除我刚刚塑造的 fragment ....谢谢 :)

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

添加 fragment 的方法:

YourFragment yourFrament;

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

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

transation.commit();

}

删除 fragment 的方法:

 //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;
}
}

关于android - 无法从 FrameLayout 中删除 fragment ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9911851/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com