当前位置:网站首页>Fragment的show和hide

Fragment的show和hide

2022-08-10 12:38:00 锐湃

package kk.com.yuekao;
 
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioGroup;
 
import com.bawei.fragment.Fragment1;
import com.bawei.fragment.Fragment2;
import com.bawei.fragment.Fragment3;
 
 
public class MainActivity extends FragmentActivity {
    private RadioGroup group;
    private Fragment1 fragment1;
    private Fragment2 fragment2;
    private Fragment3 fragment3;
    private FragmentTransaction transactionransaction;
    private FragmentManager supportFragmentManager;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取资源ID
        group=findViewById(R.id.group);
      //加载Fragment的方法
        //得到管理类
        supportFragmentManager = getSupportFragmentManager();
        fragment1 = new Fragment1();
        fragment2 = new Fragment2();
        fragment3 = new Fragment3();
        //打开事物
        transactionransaction = supportFragmentManager.beginTransaction();
        //加载Fragment
        transactionransaction.add(R.id.fragm,fragment1);
        transactionransaction.add(R.id.fragm,fragment2);
        transactionransaction.add(R.id.fragm,fragment3);
//        显示和隐藏
        transactionransaction.show(fragment1).hide(fragment2).hide(fragment3);
        transactionransaction.commit();
        group.check(group.getChildAt(0).getId());
 
 
      //group的监听
        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
 
 
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                FragmentTransaction  fragmentTransaction1= supportFragmentManager.beginTransaction();
                switch (checkedId){
                    case R.id.but1:
                        fragmentTransaction1.show(fragment1).hide(fragment2).hide(fragment3);
                        break;
                    case R.id.but2:
                        fragmentTransaction1.show(fragment2).hide(fragment1).hide(fragment3);
                        break;
                    case R.id.but3:
                        fragmentTransaction1.show(fragment3).hide(fragment2).hide(fragment1);
                        break;
                }
                fragmentTransaction1.commit();
            }
        });
    }
}


 
————————————————
版权声明:本文为CSDN博主「Fahaxiki丿」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/wangshuo_/article/details/86229990

原网站

版权声明
本文为[锐湃]所创,转载请带上原文链接,感谢
https://blog.csdn.net/chuyouyinghe/article/details/126261924