So I have 3 Sherlock Fragments setup and working fine with ViewPageIndicator. What I’d like to do now is put a TabHost inside one of the Fragments so that it displays 2 separate tabs (at the bottom). I believe this means I need to have 2 more Fragments to put inside the 3rd Fragment controlled by the TabHost.
Considering the TabHost needs to be created inside a Fragment, I’m not sure how to properly create it.
Here is the code for the Fragment I’ve gotten working from this tutorial, which I’ve tried to update to add the TabHost unsuccessfully:
TestFragment3.java
public class TestFragment3 extends SherlockFragment {
private String mContent = "none";
public static TestFragment3 newInstance(String text) {
TestFragment3 fragment = new TestFragment3();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putString(KEY_TAB_NUM, text);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_main, null);
String text = getString(R.string.tab_page_num) + mContent;
((TextView)view.findViewById(R.id.text)).setText(text);
return view;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContent = getArguments() != null ? getArguments().getString(KEY_TAB_NUM) : "none";
}
}
MainActivity.java
public class MainActivity extends SherlockFragmentActivity {
private static final String[] TAB_TITLES = new String[] { "This", "Is", "A", "ViewPager" };
TestFragmentAdapter mAdapter;
ViewPager mPager;
PageIndicator mIndicator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_tabs);
mAdapter = new TestFragmentAdapter(getSupportFragmentManager());
mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mIndicator = (TabPageIndicator)findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
}
class TestFragmentAdapter extends FragmentPagerAdapter {
private int mCount = TAB_TITLES.length;
public TestFragmentAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return TestFragment.newInstance(String.valueOf(position));
}
@Override
public int getCount() {
return mCount;
}
@Override
public CharSequence getPageTitle(int position) {
return TAB_TITLES[position];
}
}
}
Do I need to create a new SherlockFragmentActivity and add the 2 additional SherlockFragments to that TabHost? If so, how do I setup the MainActivity with 3 Fragments to use another Activity, not a Fragment, for its 3rd Fragment?
I’m getting this error for trying to put a Fragment within a Fragment…
11-14 19:10:54.271: E/AndroidRuntime(2812): java.lang.IllegalStateException: Recursive entry to executePendingTransactions
11-14 19:10:54.271: E/AndroidRuntime(2812): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1388)
11-14 19:10:54.271: E/AndroidRuntime(2812): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:431)
11-14 19:10:54.271: E/AndroidRuntime(2812): at com.example.demolistview.EventInviteesFragment$TabManager.onTabChanged(EventInviteesFragment.java:152)
11-14 19:10:54.271: E/AndroidRuntime(2812): at android.widget.TabHost.invokeOnTabChangeListener(TabHost.java:391)
11-14 19:10:54.271: E/AndroidRuntime(2812): at android.widget.TabHost.setCurrentTab(TabHost.java:376)
11-14 19:10:54.271: E/AndroidRuntime(2812): at android.widget.TabHost.addTab(TabHost.java:236)
...
...
...
In regards to this question:
If you look at the latest support demos they have an example of what you are trying to implement. They can be downloaded through the SDK Manager.
This is how they are nesting a tab host within a fragment. The setup is using the Child FragmentManager.
I also created a small example which can be found at: https://github.com/marsucsb/nested-fragments