Fragment点击切换A、B、C、D 4个页面(隐藏、显示)

案例图片:


XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".View.MainActivity">
    <FrameLayout
        android:id="@+id/fff"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"></FrameLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RadioButton
            android:id="@+id/radio_1"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:button="@null"
            android:gravity="center"
            android:text="主页"
            android:textSize="25sp" />

        <RadioButton
            android:id="@+id/radio_2"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:button="@null"
            android:text="发现"
            android:gravity="center"
            android:textSize="25sp" />

        <RadioButton
            android:id="@+id/radio_3"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:button="@null"
            android:text="成长"
            android:gravity="center"
            android:textSize="25sp" />

        <RadioButton
            android:id="@+id/radio_4"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_height="wrap_content"
            android:button="@null"
            android:text="个人"
            android:textSize="25sp" />
    </LinearLayout>
</LinearLayout>


Java代码
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private RadioButton radio_1;
    private RadioButton radio_2;
    private RadioButton radio_3;
    private RadioButton radio_4;
    private AFragment aFragment;
    private BFragment bFragment;
    private CFragment cFragment;
    private DFragment dFragment;
    private FragmentManager supportFragmentManager;

    //调全局FrameLayout控件
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        //获取管理者
        supportFragmentManager = getSupportFragmentManager();
        //默认显示第一条
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        if(aFragment!=null){
            fragmentTransaction.hide(aFragment);
        }
        if (aFragment == null) {
            aFragment = new AFragment();
            fragmentTransaction.add(R.id.fff, aFragment);
        } else {
            fragmentTransaction.show(aFragment);
        }
        fragmentTransaction.commit();

    }


    private void initView() {
        radio_1 = (RadioButton) findViewById(R.id.radio_1);
        radio_2 = (RadioButton) findViewById(R.id.radio_2);
        radio_3 = (RadioButton) findViewById(R.id.radio_3);
        radio_4 = (RadioButton) findViewById(R.id.radio_4);
        radio_1.setOnClickListener(this);
        radio_2.setOnClickListener(this);
        radio_3.setOnClickListener(this);
        radio_4.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        //分发事件
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        //如果当前对象不等于null的话,那就隐藏他,不然会造成页面叠加现象
        if (aFragment != null) {
            fragmentTransaction.hide(aFragment);
        }
        if (bFragment != null) {
            fragmentTransaction.hide(bFragment);
        }
        if (cFragment != null) {
           fragmentTransaction.hide(cFragment);
        }
        if (dFragment != null) {
            fragmentTransaction.hide(dFragment);
        }
        switch (v.getId()) {
            case R.id.radio_1:
                //如果对象==null的情况下,那就初始化,显示对象
                if (aFragment == null) {
                    aFragment = new AFragment();
                    fragmentTransaction.add(R.id.fff, aFragment);
                } else {
                    fragmentTransaction.show(aFragment);
                }
                break;
            case R.id.radio_2:
                if (bFragment == null) {
                    bFragment = new BFragment();
                    fragmentTransaction.add(R.id.fff, bFragment);
                } else {
                    fragmentTransaction.show(bFragment);
                }
                break;
            case R.id.radio_3:
                if (cFragment == null) {
                    cFragment = new CFragment();
                    fragmentTransaction.add(R.id.fff, cFragment);
                } else {
                    fragmentTransaction.show(cFragment);
                }
                break;
            case R.id.radio_4:
                //管理者         执行者          //替换   //提交
                if (dFragment == null) {
                    dFragment = new DFragment();
                    fragmentTransaction.add(R.id.fff, dFragment);
                } else {
                    fragmentTransaction.show(dFragment);
                }
                break;
        }
        //提交事件
        fragmentTransaction.commit();

    }

}

猜你喜欢

转载自blog.csdn.net/weixin_41835113/article/details/80529943