Android page switching is performed by a simple Fragment

The first is the activity of the layout

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

   

    <FrameLayout
        android:id="@+id/fragment"

        android:layout_width="395dp"
        android:layout_height="509dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.333"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </FrameLayout>

    <Button
        android:id="@+id/b1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="50dp"
        android:layout_marginBottom="12dp"
        android:text="1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/b2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="64dp"
        android:layout_marginBottom="17dp"
        android:text="2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

  Create two Fragment subclasses

Here, for example with a

package com.example.fragment;


import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

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

public class f1 extends Fragment {



@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.f1_fragment2, container, false);
}



}

  Its layout:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/f1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="f1">
    <TextView
        android:id="@+id/textView4"
        android:layout_width="182dp"
        android:layout_height="85dp"
        android:layout_marginTop="165dp"
        android: text = "I'm 1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

  Finally, and most importantly, Mainactivity content:

package com.example.fragment;

import androidx.appcompat.app.AppCompatActivity;

import androidx.fragment.app.FragmentManager;



import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


public class MainActivity extends AppCompatActivity {
    private Button b1=null;
    private Button b2=null;
   private  FragmentManager fm=null ;
    private  FragmentTransaction transaction =null ;

    private f1 f1;
    private f2 f2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView(R.layout.activity_main);
        b1=(Button)findViewById(R.id.b1);
        b2=(Button)findViewById(R.id.b2);
       fm = getSupportFragmentManager ();


    setDefaultFragment();
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            transaction = fm.beginTransaction();
            f1=new f1();
            transaction.replace(R.id.fragment,f1);
            transaction.commit();
        }
    });
    b2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            transaction = fm.beginTransaction();
            f2=new f2();
            transaction.replace(R.id.fragment,f2);
            transaction.commit();
        }
    });
    }

    private void setDefaultFragment()
    {
        transaction = fm.beginTransaction();
        f1=new f1();
        transaction.replace(R.id.fragment,f1);
        transaction.commit();
    }



}

  Note: Each FragmentTransaction only be submitted once, and therefore must be re-assign a new object for the transaction prior to each submission;

Moreover, on the "fm = getSupportFragmentManager ();" at the use of "getSupportFragmentManager ();", instead of using the "fm = getFragmentManager ();" The reason, please refer to this post https://blog.csdn.net/qq_28484355/ article / details / 67824228

effect:

 

 Click the "2":

 

Guess you like

Origin www.cnblogs.com/liuleliu/p/12304794.html