FrameLayout占位与fragment页面联动

在这里插入代码片


<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">

    <FrameLayout
        android:id="@+id/fl"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

    </FrameLayout>

    <RadioGroup
        android:id="@+id/rg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:background="@drawable/select"
            android:button="@null"
            android:checked="true"
            android:gravity="center"
            android:text="首页" />

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:background="@drawable/select"
            android:button="@null"
            android:gravity="center"
            android:text="发现" />

        <RadioButton
            android:id="@+id/radio3"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:background="@drawable/select"
            android:button="@null"
            android:gravity="center"
            android:text="购物车" />

        <RadioButton
            android:id="@+id/radio4"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:background="@drawable/select"
            android:button="@null"
            android:gravity="center"
            android:text="我的" />

    </RadioGroup>

</LinearLayout>
package com.bawei.weeklx;

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

import com.bawei.weeklx.base.BaseActivity;
import com.bawei.weeklx.fragmen.Frag01;
import com.bawei.weeklx.fragmen.Frag02;
import com.bawei.weeklx.fragmen.Frag03;
import com.bawei.weeklx.fragmen.Frag04;

public class MainActivity extends BaseActivity {


    private RadioGroup rg;

    @Override
    protected int layoutID() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        rg = findViewById(R.id.rg);
    }

    @Override
    protected void initData() {
        //获取事务管理器
        final FragmentManager manager = getSupportFragmentManager();
        //开启事务
        FragmentTransaction transaction = manager.beginTransaction();
        //添加布局
        final Frag01 frag01 = new Frag01();
        final Frag02 frag02 = new Frag02();
        final Frag03 frag03 = new Frag03();
        final Frag04 frag04 = new Frag04();
        transaction.add(R.id.fl, frag01);
        transaction.add(R.id.fl, frag02);
        transaction.add(R.id.fl, frag03);
        transaction.add(R.id.fl, frag04);
        transaction.show(frag01).hide(frag02).hide(frag03).hide(frag04);
        transaction.commit();
        rg.check(R.id.radio1);
        //切换
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                FragmentTransaction qq = manager.beginTransaction();
                switch (checkedId) {
                    case R.id.radio1:
                        qq.show(frag01).hide(frag02).hide(frag03).hide(frag04);
                        break;
                    case R.id.radio2:
                        qq.show(frag02).hide(frag01).hide(frag03).hide(frag04);
                        break;
                    case R.id.radio3:
                        qq.show(frag03).hide(frag01).hide(frag02).hide(frag04);
                        break;
                    case R.id.radio4:
                        qq.show(frag04).hide(frag01).hide(frag02).hide(frag03);
                        break;
                }
                qq.commit();
            }
        });
    }
}

版权声明:本文为weixin_44210659原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。