Fragment切换和隐藏,显示(show,hide)

 
  
代码演示
XML



    

    
        
        
        
        
    



Java
 
  
package com.bawei.zhangjinfeng20171105;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.widget.FrameLayout;
import android.widget.RadioGroup;

import com.bawei.zhangjinfeng20171105.frag.Four;
import com.bawei.zhangjinfeng20171105.frag.One;
import com.bawei.zhangjinfeng20171105.frag.Three;
import com.bawei.zhangjinfeng20171105.frag.Two;

public class MainActivity extends AppCompatActivity {

    private FragmentManager fragmentManager;
    private RadioGroup rg;
    private FrameLayout fl;
    private One one;
    private Two two;
    private Three three;
    private Four four;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取管理者
        fragmentManager = getSupportFragmentManager();
        //初始化组件
        fl = (FrameLayout) findViewById(R.id.fl);
        rg = (RadioGroup) findViewById(R.id.rg);
        //将所有Fragment添加到占位布局
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        //实例化第一个Frangment
        one = new One();
//        fragmentTransaction.add(R.id.fl, One).commit();
        fragmentTransaction.add(R.id.fl,one).commit();
        /* RadioGroupd点击监听 */
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {



            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                //隐藏所有Fragment
                hidefragment();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                switch (i){
                    case R.id.ra1:
                        //展示第一个
                        fragmentTransaction.show(one).commit();
                        break;
                    case R.id.ra2:
                        //展示第二个
                        if(two==null){
                        two = new Two();
                        fragmentTransaction.add(R.id.fl, two).commit();

                        }else{
                            fragmentTransaction.show(two).commit();
                        }
                        break;
                    case R.id.ra3:
                        //展示第三个
                        if(three==null){
                            three = new Three();
                            fragmentTransaction.add(R.id.fl, three).commit();
                        }else{
                            fragmentTransaction.show(three).commit();
                        }
                        break;
                    case R.id.ra4:
                        //展示第三个
                        if(four==null){
                            four = new Four();
                            fragmentTransaction.add(R.id.fl, four).commit();
                        }else{
                            fragmentTransaction.show(four).commit();
                        }
                        break;
                }
            }
        });
    }

    private void hidefragment() {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        //如果Fragment不为空并且已经添加,就隐藏
        if(one!=null&&one.isAdded()){
            fragmentTransaction.hide(one);
        }
        if(two!=null&&two.isAdded()){
            fragmentTransaction.hide(two);
        }
        if(three!=null&&three.isAdded()){
            fragmentTransaction.hide(three);
        }
        if(four!=null&&four.isAdded()){
            fragmentTransaction.hide(four);
        }
        //提交
        fragmentTransaction.commit();
    }
}


你可能感兴趣的:(基础,基础项目)