Android -- Switching between Fragment s

Implementing Super Simple 4 Bottom Bar Switching Fragment

Design sketch:

                                                       

Implementation steps:

Step 1: Dependent packages

implementation 'com.android.support:design:28.0.0'

Step 2: Create menu directory under res directory and navigation.xml file

navigation.xml file code:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/main_home"
        android:icon="@drawable/ic_main_home_s"
        android:title="home page"
        />

    <item
        android:id="@+id/main_information"
        android:icon="@drawable/ic_main_information_u"
        android:title="information"
        />

    <item
        android:id="@+id/main_stock_operation"
        android:icon="@drawable/ic_main_stock_operation_u"
        android:title="operate"
        />

    <item
        android:id="@+id/main_my"
        android:icon="@drawable/ic_main_my_u"
        android:title="My"
        />

</menu>

Step 3: Define the activity_main.xml, selector_tab_color.xml files

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
    android:background="#FFFFFF"
    >

    <FrameLayout
        android:id="@+id/main_frameLayout"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/main_navigation"
        />

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/main_navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="?android:attr/windowBackground"
        app:itemIconTint="@drawable/selector_tab_color"
        app:itemTextColor="@drawable/selector_tab_color"
        app:labelVisibilityMode="labeled"
        app:layout_constraintTop_toBottomOf="@+id/main_frameLayout"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/navigation"
        />

</android.support.constraint.ConstraintLayout>

Among them, app: label VisibilityMode= "labeled" is the solution to add the fourth item without displaying the code.

Where app:menu="@menu/navigation" is the number of itme specified below (that is, the number of item s in the menu defined in the second step above)

selector_tab_color.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_checked="true" android:color="#FF60B6FF"/>
    <item android:state_checked="false" android:color="#FF999999"/>

</selector>

Step 4: Create MyFragment 1, MyFragment 2, MyFragment 3, MyFragment 4, and

MyFragmentMax.

I just posted the rest of MyFragment 1, just change the number, and all of them are not posted.

MyFragment1.xml

package com.qyt.hp.mybottomnavigationview.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.qyt.hp.mybottomnavigationview.R;
import com.qyt.hp.mybottomnavigationview.util.GT;


public class MyFragment1 extends Fragment {

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

    @Override
    public void onStart() {
        super.onStart();
        GT.log_v("onStart:" + this);
    }

    @Override
    public void onStop() {
        super.onStop();
        GT.log_v("onStop:" + this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        GT.log_v("onDestroy:" + this + "\n\n\n\n");
    }
}

MyFragmentMax.java

package com.qyt.hp.mybottomnavigationview.fragment;

import android.content.Context;
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 android.widget.Button;
import android.widget.TextView;

import com.qyt.hp.mybottomnavigationview.R;
import com.qyt.hp.mybottomnavigationview.util.GT;


public class MyFragmentMax extends Fragment {

    private static final String TAG = "MyFragmentMax";
    private int index;
    private static GT.GT_Fragment gf;

    public static MyFragmentMax newInstance(int index,GT.GT_Fragment gt_fragment) {
        Bundle args = new Bundle();
        args.putInt(TAG,index);
        gf = gt_fragment;
        MyFragmentMax fragment = new MyFragmentMax();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        GT.getGT().setCONTEXT(context);         //Initialization context
        index = getArguments().getInt(TAG);
    }

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

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Button btn = view.findViewById(R.id.Fragment_btn_max);
        TextView tv = view.findViewById(R.id.Fragment_tv_max);
        tv.setText("The first " + index + " individual Fragment");
        GT.toast_s("Successfully add the " + index + " individual Fragment");       //If toast does not specify a context, it needs to initialize the context instance first: GT.getGT().setCONTEXT(context); // initialization context
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gf.finish();    //Quitting the current top-level Fragment is similar to the use of Activity to exit the current page.
                GT.toast_s("Successful exit " + index + " individual Fragment");
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        GT.log_v("onStart:" + MyFragmentMax.TAG + ": " + index);
    }

    @Override
    public void onStop() {
        super.onStop();
        GT.log_v("onStop:" + MyFragmentMax.TAG + ": " + index);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        GT.log_v("onDestroy:" + MyFragmentMax.TAG + ": " + index);
    }

}

Create the corresponding xml file

fragment1_my.xml,fragment2_my.xml,fragment3_my.xml,fragment4_my.xml,fragment_max_my.xml

I just posted fragment1_my.xml and the rest are almost the same. Just change the number, and all will not be posted.

fragment1_my.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
    android:background="#9C27B0"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fragment 1"
        android:textColor="#FFFFFF"
        android:textSize="38sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />



</android.support.constraint.ConstraintLayout>

fragment_max_my.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F44336">

    <TextView
        android:id="@+id/Fragment_tv_max"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fragment Max"
        android:textColor="#FFFFFF"
        android:textSize="38sp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        />

    <Button
        android:id="@+id/Fragment_btn_max"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Exit"
        android:textAllCaps="false"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_margin="10dp"
        />

</android.support.constraint.ConstraintLayout>

Step 5: Code in MainActivity.java: GT packages are used

package com.qyt.hp.mybottomnavigationview;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;

import com.qyt.hp.mybottomnavigationview.fragment.MyFragment1;
import com.qyt.hp.mybottomnavigationview.fragment.MyFragment2;
import com.qyt.hp.mybottomnavigationview.fragment.MyFragment3;
import com.qyt.hp.mybottomnavigationview.fragment.MyFragment4;
import com.qyt.hp.mybottomnavigationview.util.GT;

import java.util.HashMap;
import java.util.Map;


public class MainActivity extends AppCompatActivity {

    private GT.GT_Fragment gf;
    private BottomNavigationView mainNavigation;
    public int index = 0;

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

        mainNavigation = findViewById(R.id.main_navigation);
        GT.getGT().setGtLogTf(true);//Log logs that open internal GT errors are turned off by default

        //Add Fragment s to be managed
        Map<Object, Fragment> map = new HashMap<>();
        map.put(MyFragment1.class,new MyFragment1());
        map.put(MyFragment2.class,new MyFragment2());
        map.put(MyFragment3.class,new MyFragment3());
        map.put("My",new MyFragment4());//Specify a jump key, a jump Fragment

        gf = new GT.GT_Fragment(savedInstanceState,this,getSupportFragmentManager());//Initialize gt_fragment object

        /**
         *  initFragment Method parameter analysis:
         *  First: Id of Fragment container
         *  Second: Put in those fragments and add them with map or list
         *  Third: Specify the first Fragment page to display
         */
        gf.initFragment(R.id.main_frameLayout,map,MyFragment1.class);//Initialize Fragment Manager

        //Click Bottom Navigation View to listen
        mainNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

                switch (menuItem.getItemId()){
                    case R.id.main_home:
                        gf.startFragment(MyFragment1.class);//Jumping to MyFragment1 is similar to the use of jumps between activities
                        return true;

                    case R.id.main_information:
                        gf.startFragment(MyFragment2.class);//Jump to MyFragment 2
                        return true;

                    case R.id.main_stock_operation:
                        gf.startFragment(MyFragment3.class);//Jump to MyFragment 3
                        return true;

                    case R.id.main_my:
                        gf.startFragment("My");//Jump to MyFragment 4
//                      Gf. start Fragment (MyFragment Max. new Instance (index ++, gf); //Open a new and "short-term" Fragment
                        return true;
                }

                return false;
            }
        });

    }
}

Conclusion:

The GT toolkit is used in this chapter. If not, you can download it from the following website:

GT Package: Tool Set Package

The GT toolkit uses: https://blog.csdn.net/qq_39799899/article/details/90600383

The source code for the current project: https://github.com/1079374315/MyBottomNavigationView

Tags: Android Fragment xml encoding

Posted on Wed, 09 Oct 2019 04:29:47 -0700 by ayj