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