With API level 17, this is now possible:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
// This class is the 3rd fragment in my ViewPager,
// to which I wanted to add 2 tabs....
public class TabHostParentFragment extends Fragment {
private FragmentTabHost mTabHost;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mTabHost = new FragmentTabHost(getActivity());
mTabHost.setup(getActivity(), getChildFragmentManager(), R.layout.my_parent_fragment);
Bundle arg1 = new Bundle();
arg1.putInt("Arg for Frag1", 1);
mTabHost.addTab(mTabHost.newTabSpec("Tab1").setIndicator("Frag Tab1"),
MyNestedFragment1.class, arg1);
Bundle arg2 = new Bundle();
arg2.putInt("Arg for Frag2", 2);
mTabHost.addTab(mTabHost.newTabSpec("Tab2").setIndicator("Frag Tab2"),
MyNestedFragment2.class, arg2);
return mTabHost;
}
@Override
public void onDestroyView() {
super.onDestroyView();
mTabHost = null;
}
}
Make sure you update your android-support-v4.jar file, as it didnt auto update for me when I downloaded through the SDK manager. This prevents the getChildFragmentManger()
function from being defined.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…