Note that there are some explanatory texts on larger screens.

plurals
  1. POTabhost inside a SherlockFragment using ViewPageIndicator
    text
    copied!<p>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.</p> <p>Considering the TabHost needs to be created inside a Fragment, I'm not sure how to properly create it.</p> <p>Here is the code for the Fragment I've gotten working from <a href="http://droidista.blogspot.com/2012/08/making-actionbarsherlock-and.html" rel="nofollow">this tutorial</a>, which I've tried to update to add the TabHost unsuccessfully:</p> <p>TestFragment3.java</p> <pre><code>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"; } } </code></pre> <p>MainActivity.java</p> <pre><code>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]; } } } </code></pre> <p>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?</p> <p>I'm getting this error for trying to put a Fragment within a Fragment...</p> <pre><code>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) ... ... ... </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload