如何实现Fragment之间的替换

1.主页面

package com.example.logindemo;

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

import com.example.fragment.Fragment1;
import com.example.fragment.Fragment2;
import com.example.fragment.Fragment3;
import com.example.fragment.Fragment4;
import com.example.fragment.Fragment5;

import static com.example.logindemo.R.id.fragment;

public class GoodsActivity extends AppCompatActivity {

    private FragmentManager manager;
    private RadioGroup btn_group;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods);
        btn_group = findViewById(R.id.btn_group);
        manager = getSupportFragmentManager();
        final FragmentTransaction transaction = manager.beginTransaction();
        final Fragment1 fragment1 = new Fragment1();
        final Fragment2 fragment2 = new Fragment2();
        final Fragment3 fragment3 = new Fragment3();
        final Fragment4 fragment4 = new Fragment4();
        final Fragment5 fragment5 = new Fragment5();
        transaction.add(R.id.fragment, fragment1).show(fragment1);
        transaction.add(R.id.fragment, fragment2).hide(fragment2);
        transaction.add(R.id.fragment, fragment3).hide(fragment3);
        transaction.add(R.id.fragment, fragment4).hide(fragment4);
        transaction.add(R.id.fragment, fragment5).hide(fragment5);
        transaction.commit();
        btn_group.check(btn_group.getChildAt(0).getId());
        btn_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                FragmentManager manager1 = getSupportFragmentManager();
                FragmentTransaction transaction1 = manager1.beginTransaction();
                switch (i){
                    case R.id.rb1:
                        transaction1.show(fragment1).hide(fragment2).hide(fragment3).hide(fragment4).hide(fragment5);
                        break;
                    case R.id.rb2:
                        transaction1.show(fragment2).hide(fragment1).hide(fragment3).hide(fragment4).hide(fragment5);
                        break;
                    case R.id.rb3:
                        transaction1.show(fragment3).hide(fragment2).hide(fragment1).hide(fragment4).hide(fragment5);
                        break;
                    case R.id.rb4:
                        transaction1.show(fragment4).hide(fragment2).hide(fragment3).hide(fragment1).hide(fragment5);
                        break;
                    case R.id.rb5:
                        transaction1.show(fragment5).hide(fragment2).hide(fragment3).hide(fragment4).hide(fragment1);
                        break;
                }
                transaction1.commit();
            }
        });
    }

}

2.视图文件

<?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=".GoodsActivity">
    <FrameLayout
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/fragment"
        ></FrameLayout>

    <RadioGroup
        android:id="@+id/btn_group"
        android:layout_alignParentBottom="true"
        android:background="@drawable/rectangle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/rb1"
            android:button="@null"
            android:layout_marginTop="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:drawableTop="@drawable/one_selector"
            />
        <RadioButton
            android:id="@+id/rb2"
            android:button="@null"
            android:layout_marginTop="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:drawableTop="@drawable/two_selector"/>
        <RadioButton
            android:id="@+id/rb3"
            android:button="@null"
            android:layout_marginTop="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:drawableTop="@drawable/sh"/>
        <RadioButton
            android:id="@+id/rb4"
            android:button="@null"
            android:layout_marginTop="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:drawableTop="@drawable/three_selector"/>
        <RadioButton
            android:id="@+id/rb5"
            android:layout_marginTop="20dp"
            android:layout_gravity="center"
            android:button="@null"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableTop="@drawable/four_selector"/>
    </RadioGroup>

</LinearLayout>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值