gpt4 book ai didi

java - Android:切换按钮打开:显示 fragment 关闭:隐藏 fragment

转载 作者:太空宇宙 更新时间:2023-11-04 13:28:50 25 4
gpt4 key购买 nike

我想切换按钮以在单击它时显示我的 fragment ,以及当我再次单击它以删除或隐藏我的 fragment 时。基本上〜开:炫耀:隐藏

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Shower_fragment shower_fragment = new Shower_fragment();
android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.linear_shower, shower_fragment, "Shower");
if(isChecked){
fragmentTransaction.show(shower_fragment);
fragmentTransaction.commit();
}else{
fragmentTransaction.hide(shower_fragment);
fragmentTransaction.commit();

}
}

最佳答案

我认为你忘记调用 getFragmentManager();

Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
Shower_fragment shower_fragment = new Shower_fragment();
FragmentManager fm = getFragmentManager();

if(null == fm){return;}
if(isChecked)
{
fm.beginTransaction();
.add(R.id.linear_shower, shower_fragment, "Shower");
.show()
.commit();
}else
{

fm.beginTransaction();
.add(R.id.linear_shower, shower_fragment, "Shower");
.hide()
.commit();
}
}

关于java - Android:切换按钮打开:显示 fragment 关闭:隐藏 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32385468/

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