简体   繁体   中英

Android: tablayout/viewpager fragments within fragment or activity

I have a project I am working on in Android. I have one big fragment that covers the whole of the screen in my main activity. Normally I for each ui screen the user uses I would just do a replace command and swap the fragments for each screen.

In my current one I want to put a tablayout/viewpager combo with three different fragments and depending on what tab is pushed I will show a new fragment. Problem with this is that is a tablayout showing three different fragments inside another fragment. Is this even possible or recommended? and if I have to change it to a new activity. Would the navigation drawer act normally and just shut as I loaded the activity or is there going to be a problem there as well.

Thanks for your help

You can check below official documentation for the same or can use my demo snippet: https://developer.android.com/reference/android/support/v4/app/FragmentTabHost.html

Try to do this to handle the Tabs in main fragment:

public class MainFragment extends Fragment {
private FragmentTabHost mTabHost;

//Mandatory Constructor
public MainFragment() {
}

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_tabs,container, false);


    mTabHost = (FragmentTabHost)rootView.findViewById(android.R.id.tabhost);
    mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);

    mTabHost.addTab(mTabHost.newTabSpec("fragmentb").setIndicator("Fragment B"),
            FragmentB.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("fragmentc").setIndicator("Fragment C"),
            FragmentC.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("fragmentd").setIndicator("Fragment D"),
            FragmentD.class, null);


    return rootView;
}

}

With this tabs layout:

<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">

    <FrameLayout
        android:id="@+id/realtabcontent"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

</LinearLayout>

Yes, TabLayout with viewPager will maintain it's different fragments in the memory.

Since, there are going to be only 3 tabs in your case it won't affect your memory performance, you can go with pagerAdapter.

You can reuse a single fragment for other tabs, but as i said since there are only 3 tabs, it won't affect your performance.

And Yes you can have tabLayout inside your main fragment, you main fragment is a part of your activity right, it will follow the same life cycle as the hosting activity except few other callbacks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM