Android tab location in fragment without fragmentation

I am currently creating an application that uses a navigation box and fragments. In one fragment, I want to create a tab layout that should be done in the fragment activity. My question is, are there alternative ways to implement a tab layout inside a fragment? Thank you in advance

0
source share
2 answers

Take a look at the following code:

import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTabHost; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class TabFragment extends Fragment { FragmentTabHost mTabHost; @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setRetainInstance(true); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mTabHost = new FragmentTabHost(getActivity()); mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.content); getActivity().getActionBar().setTitle("TabFragment"); mTabHost.addTab( mTabHost.newTabSpec("First").setIndicator( "First"), FirstFragment.class, null); mTabHost.addTab(mTabHost.newTabSpec("Second").setIndicator("Second"), SecondFragment.class, null); return mTabHost; } @Override public void onDestroyView() { // TODO Auto-generated method stub super.onDestroyView(); mTabHost = null; } } 

Here R.id.content is the FrameLayout where you show the fragments. (Like a layout called content_frame, which only has a FrameLayout with the content id.)

+2
source

You should be able to create a view with a tab layout, as in a normal activity. Then create your class for the tab layout and extend the FragmentActivity and contribute the TabListener. In this snippet, you simply inflate the tab into your container snippet, like all the snippets, and you must do whatever you are used to doing with TabLayout.

These links would probably be useful for you: Android tab plugin inside snippet

0
source

Source: https://habr.com/ru/post/1013675/


All Articles