所以我有 3 个 Sherlock Fragments 设置并与 ViewPageIndicator 一起工作正常。我现在想做的是将 TabHost 放在其中一个 Fragment 中,以便它显示 2 个单独的选项卡(在底部)。我相信这意味着我需要再有 2 个片段放入 TabHost 控制的第 3 个片段中。
考虑到 TabHost 需要在 Fragment 中创建,我不确定如何正确创建它。
这是我从本教程中获得的 Fragment 的代码,我尝试对其进行更新以添加 TabHost 失败:
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];
}
}
}
我是否需要创建一个新的 SherlockFragmentActivity 并将另外 2 个 SherlockFragment 添加到该 TabHost?如果是这样,我如何设置带有 3 个片段的 MainActivity 以使用另一个 Activity,而不是片段,作为它的第 3 个片段?
我在尝试将 Fragment 放入 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)
...
...
...