[JAVA] Create more Tabs and Fragments in the Fragment of BottomNavigationView

I made a note when I made BottomNavigationView once and made Tabhost and its fragment in it.
I mainly referred to the reference at the bottom, but I am fixing it because some errors occurred. First, it is one of each fragment divided by BottomNavigationView.

Parent fragment.java


public class parent fragment extends Fragment{
    public static Record newInstance() {
        return new Record();
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.record, container, false);
        //Get Fragment Manager
        FragmentManager mFragmentManager = getChildFragmentManager();
        //Get FragmentTabHost from xml, id is android.R.id.Note that it is tabhost
        FragmentTabHost tabHost = (FragmentTabHost)v.findViewById(android.R.id.tabhost);
        Log.d("tabHost", String.valueOf(tabHost));
        //Set up by passing the Context, FragmentManager, and the id of the View that corresponds to the Fragment.
        tabHost.setup(getActivity().getApplicationContext(), mFragmentManager, R.id.content);
        //Pass any id as an argument of String type
        //This time, we will prepare two TabSpecs to switch between the two Fragments from the FragmentTabHost.
        TabHost.TabSpec mTabSpec1 = tabHost.newTabSpec("tab1");
        TabHost.TabSpec mTabSpec2 = tabHost.newTabSpec("tab2");
        //Pass the character to be displayed on Tab
        mTabSpec1.setIndicator("This is tab1");
        mTabSpec2.setIndicator("This is tab2");
        Bundle args = new Bundle();
        args.putString("string", "message");
        //Pass an argument to associate a class with each TabSpec
        //By having Bundle as the third argument, you can pass a value to Fragment. Pass null if not needed
        tabHost.addTab(mTabSpec1,Child fragment 1.class, args);
        tabHost.addTab(mTabSpec2,Child fragment 2.class, null);
        return v;

    }
}

Child fragment 1.java


public class child fragment 1 extends Fragment{
static child fragment 1 newInstance() {return new child fragment 1();}
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        //If you pass a Bundle during addTab, get the value from the Bundle
        //Do not pass(Passing null)In that case, it is not necessary to implement it. Otherwise getString("string")Sometimes an error occurs
        Bundle args = getArguments();
        String str = args.getString("string");
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        return inflater.inflate(R.layout.Child fragment layout 1, null);
    }
}

Child fragment 2.java


public class child fragment 2 extends Fragment{
static child fragment 2 newInstance() {return new child fragment 2();}

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        return inflater.inflate(R.layout.Child fragment layout 2, null);
    }
}

Parent fragment layout.xml


<?xml version="1.0" encoding="utf-8"?>
<!--The id of FragmentTabHost must be@android:id/make it tabhost-->
<android.support.v4.app.FragmentTabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!--Like FragmentTabHost, id is specified. id must be@android:id/to tabs-->
        <TabWidget
            android:id="@android:id/tabs"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"/>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0"/>

        <!--Fragment is added to content-->
        <FrameLayout
            android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>

    </LinearLayout>
</android.support.v4.app.FragmentTabHost>

Child fragment layout.xml(Child fragment layout1,Child fragment layout2)


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

    <TextView
        android:id="@+id/textView3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Child fragment" />
</LinearLayout>

Remarks:
There are "android.app.Fragment ~" and "android.support.v4.app.Fragment ~" in the Fragment system, but it seems better to unify them (naturally.)
[Android standard I am using v4 by referring to the article "Support.v4.app.Fragment should be used rather than Fragment" (http://qiita.com/LyricalMaestro0/items/a8bafa653fad23dead8e).

Reference:
FragmentTabHost Memorandum of Understanding

Recommended Posts

Create more Tabs and Fragments in the Fragment of BottomNavigationView
Create a private repository in Amazon ECR and push/pull the image
[Java] Get the dates of the past Monday and Sunday in order
Let's consider the meaning of "stream" and "collect" in Java's Stream API.
Spring validation was important in the order of Form and BindingResult
Install Rails in the development environment and create a new application
Let's create a TODO application in Java 5 Switch the display of TODO
This and that of the JDK
Order of processing in the program
[Rails] Difference in behavior between delegate and has_many-through in the case of one-to-one-to-many
The story of forgetting to close a file in Java and failing
Set the number of seconds for fast forward and rewind in ExoPlayer
Confirmation and refactoring of the flow from request to controller in [httpclient]
[Ruby basics] About the role of true and break in the while statement
(Determine in 1 minute) About the proper use of empty ?, blank? And present?
This and that of the implementation of date judgment within the period in Java
How to change the maximum and maximum number of POST data in Spark
Calculate the percentage of "good", "normal", and "bad" in the questionnaire using SQL
Find the maximum and minimum of the five numbers you entered in Java
Displaying the three-dimensional structure of DNA and proteins in Ruby-in the case of GR.rb
How to create your own annotation in Java and get the value
Get the result of POST in Java
The identity of params [: id] in rails
Folding and unfolding the contents of the Recyclerview
About the operation of next () and nextLine ()
Summary of hashes and symbols in Ruby
The story of AppClip support in Anyca
Implementation of tabs using TabLayout and ViewPager
The story of writing Java in Emacs
About the mechanism of the Web and HTTP
Write the movement of Rakefile in the runbook
Discrimination of Enums in Java 7 and above
Create your own shortcuts in Xcode to eliminate the hassle of pod install
Be absolutely careful when putting the result of and / or in a variable!