欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android Studio FragmentManager

程序员文章站 2022-07-01 16:37:55
<?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"
    tools:context=".MainActivity"
    android:id="@+id/ll"
    android:orientation="vertical"
    >
  <FrameLayout
      android:layout_weight="1"
      android:layout_gravity="top"
      android:layout_width="match_parent"
      android:layout_height="match_parent"></FrameLayout>
  <RadioGroup
      android:id="@+id/rg"
      android:background="#88A29D9D"
      android:orientation="horizontal"
      android:layout_gravity="bottom"
      android:layout_width="match_parent"
      android:layout_height="50dp">
    <RadioButton
        android:id="@+id/rd1"
        android:drawableTop="@drawable/tp"
        android:layout_weight="1"
        android:text="联系人"
        android:textColor="@drawable/text"
        android:button="@null"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        ></RadioButton>
    <RadioButton
        android:id="@+id/rd2"
        android:textColor="@drawable/text"
        android:drawableTop="@drawable/tp1"
        android:layout_weight="1"
        android:text="信息"
        android:gravity="center"
        android:button="@null"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        ></RadioButton>
  </RadioGroup>


</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".BlankFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="11111111111" />

</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".BlankFragment2">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="2222222222" />

</FrameLayout>
package com.example.day4;

import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.RadioGroup;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import java.io.BufferedReader;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener{

    private LinearLayout ll;
    private RadioGroup rg;
    private FragmentManager manager;
    private BlankFragment2 blankFragment2;
    private   BlankFragment blankFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ll = findViewById(R.id.ll);
        rg = (RadioGroup) findViewById(R.id.rg);

        rg.setOnCheckedChangeListener(this);

         manager=getSupportFragmentManager();

        final  FragmentTransaction fragmentTransaction=manager.beginTransaction();

         blankFragment=new BlankFragment();
         fragmentTransaction.add(R.id.ll,blankFragment);

         blankFragment2=new BlankFragment2();
         fragmentTransaction.add(R.id.ll,blankFragment2);

         fragmentTransaction.hide(blankFragment2);
             fragmentTransaction.commit();

    }
    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i){
            case R.id.rd1:
          FragmentTransaction fragmentTransaction1=manager.beginTransaction();
          fragmentTransaction1.hide(blankFragment2);
          fragmentTransaction1.show(blankFragment);
          fragmentTransaction1.commit();
                break;
            case R.id.rd2:
                FragmentTransaction fragmentTransaction2=manager.beginTransaction();
                fragmentTransaction2.hide(blankFragment);
                fragmentTransaction2.show(blankFragment2);
                fragmentTransaction2.commit();
                break;
        }
    }


}

 FragmentManager两个都一样
package com.example.day4;


import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * A simple {@link Fragment} subclass.
 */
public class BlankFragment extends Fragment {


    public BlankFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_blank, container, false);
    }

}

本文地址:https://blog.csdn.net/mynameisluowei/article/details/108576317