Dynamic loading of Fragment in Android

In this article, the replace method is used to replace and display fragments

MainActivity.class

package com.example.y.potographu;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
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.view.MenuItem;
import com.example.y.potographu.fragment.FragmentAppointment;
import com.example.y.potographu.fragment.FragmentDiscovery;
import com.example.y.potographu.fragment.FragmentHome;
import com.example.y.potographu.fragment.FragmentMine;

public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
    private FragmentTransaction transaction;
    private FragmentManager manager;
    private FragmentHome fragmentHome;
    private FragmentDiscovery fragmentDiscovery;
    private FragmentAppointment fragmentAppointment;
    private FragmentMine fragmentMine;

    @SuppressLint("CommitTransaction")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView(R.layout.activity_main);

        BottomNavigationView navigation = findViewById(R.id.navigation); //bottomNavigation is used to select Fragment
        navigation.setOnNavigationItemSelectedListener (this);

        /*Dynamic loading requires FragmentManager and FragmentTransaction to control the display of Fragment
        * A transaction can only be committed once, so I put its assignment in the showFragment() method
        */
        manager = getSupportFragmentManager(); //Initialize the manager
        fragmentHome = new FragmentHome(); //The first page Fragment
        showFragment(fragmentHome); //Show method

    }

    private void showFragment(Fragment fragment) {
        transaction= manager.beginTransaction(); //Open transaction
        /*
        * Use the replacement method to display the Fragment that needs to be displayed
        * The first parameter is the layout of the displayed fragment
        * The second parameter is to display the fragment
        * */
        transaction.replace(R.id.fragment_container, fragment);
        transaction.commit();//Commit the transaction

    }


    private void hideFragment(FragmentTransaction transaction) {
        /* Determine the currently displayed fragment and hide it
        * There is this step in order not to cause conflicts
        * but I use the replace method to show no conflict
        * */
        if (!fragmentHome.isHidden())
            transaction.hide(fragmentHome);
        else if (!fragmentDiscovery.isHidden())
            transaction.hide(fragmentDiscovery);
        else if (!fragmentAppointment.isHidden())
            transaction.hide(fragmentAppointment);
        else if (!fragmentMine.isHidden())
            transaction.hide(fragmentMine);
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
       // hideFragment(transaction);
        /*
        * Use the showFragment() method to display
        * */
        switch (item.getItemId()) {
            case R.id.navigation_home:
                showFragment(fragmentHome);
                return true;
            case R.id.navigation_discovery:
                if (fragmentDiscovery == null)
                    fragmentDiscovery = new FragmentDiscovery();
                showFragment(fragmentDiscovery);
                return true;
            case R.id.navigation_appointment:
                if (fragmentAppointment == null)
                    fragmentAppointment = new FragmentAppointment();
                showFragment(fragmentAppointment);
                return true;
            case R.id.navigation_mine:
                if (fragmentMine == null)
                    fragmentMine = new FragmentMine();
                showFragment(fragmentMine);
                return true;
            default:
        }
        return false;
    }
}


FragmentHome.class

package com.example.y.potographu.fragment;

        import android.os.Bundle;
        import android.support.annotation.NonNull;
        import android.support.annotation.Nullable;
        import android.support.v4.app.Fragment;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.ViewGroup;

        import com.example.y.potographu.R;

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

The other three are similar to the code on this page, so they will not be posted here.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_above="@id/navigation"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>
    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_alignParentBottom="true"
        app:menu="@menu/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </android.support.design.widget.BottomNavigationView>

</RelativeLayout>
The layout of the fragment just sets the background to identify different pages

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325561563&siteId=291194637