我有一个使用
ActionbarSherlock
、SlidingMenu
和 ViewPager
的应用程序。这个应用程序运行良好,但我试图在 ViewPager
中引入使用嵌套片段。
目前,
ViewPager
的每一页都有一个片段,但我希望页面片段中有 1 到 4 个嵌套片段,具体取决于我在手机或平板电脑上显示的天气。因此,如果我在手机上,我会说 ViewPager
有 14 个页面,但在平板电脑上,我想利用嵌套片段组合这些页面。
这是我尝试执行的代码,但它不允许我使用
getChildFragmentManager()
。它指出 getChildFragmentManager()
未定义:
public class CharacterManualActivity extends BaseActivity {
private ViewPager mViewPager;
private TabsAdapter mTabsAdapter;
Boolean tabletLayout = false;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
// Check from the Saved Instance
characterUri = (savedInstanceState == null) ? null : (Uri) savedInstanceState.getParcelable(CharacterTable.CONTENT_ITEM_TYPE);
// Passed from the activity
if (extras != null) {
viewMode = extras.getInt("ViewMode");
characterUri = extras.getParcelable(CharacterTable.CONTENT_ITEM_TYPE);
}
mViewPager = new ViewPager(this);
mViewPager.setId(R.id.pager);
setContentView(mViewPager);
final ActionBar bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mTabsAdapter = new TabsAdapter(this, mViewPager);
if (tabletLayout) {
mTabsAdapter.addTab(bar.newTab().setText("Stats"), CSheetStatsFragment.class, null);
// mTabsAdapter.addTab(bar.newTab().setText("Abilities"), CSheet2Fragment.class, null);
// mTabsAdapter.addTab(bar.newTab().setText("Classes"), CSheet3Fragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Attacks/Defense"), CSheetAttacksFragment.class, null);
// mTabsAdapter.addTab(bar.newTab().setText("Weapons"), CSheet5Fragment.class, null);
// mTabsAdapter.addTab(bar.newTab().setText("Armor"), CSheet14Fragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Skills"), CSheetSkillsFragment.class, null);
// mTabsAdapter.addTab(bar.newTab().setText("Feats"), CSheet7Fragment.class, null);
// mTabsAdapter.addTab(bar.newTab().setText("Special Abilities/Race Features/Traits"), CSheet8Fragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Equipment"), CSheetEquipmentFragment.class, null);
// mTabsAdapter.addTab(bar.newTab().setText("Worn Equipment"), CSheet10Fragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Spellbook"), CSheetSpellbookFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Familiar/Companion"), CSheetFamiliarFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Notes"), CSheetNotesFragment.class, null);
} else {
mTabsAdapter.addTab(bar.newTab().setText("Stats"), CSheetStatsFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Abilities"), CSheetAbilitiesFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Classes"), CSheetClassesFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Attacks/Defense"), CSheetAttacksFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Weapons"), CSheetWeaponsFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Armor"), CSheetArmorFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Skills"), CSheetSkillsFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Feats"), CSheetFeatsFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Special Abilities/Race Features/Traits"), CSheetSpecialTraitsFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Equipment"), CSheetEquipmentFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Worn Equipment"), CSheetWornEquipmentFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Spellbook"), CSheetSpellbookFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Familiar/Companion"), CSheetFamiliarFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Notes"), CSheetNotesFragment.class, null);
}
...
}
...
}
public class TabsAdapter extends FragmentPagerAdapter implements ActionBar.TabListener , ViewPager.OnPageChangeListener{
private final Context mContext;
private final ActionBar mActionBar;
private final ViewPager mViewPager;
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
private final String TAG = "21st Polling:";
static final class TabInfo{
private final Class<?> clss;
private final Bundle args;
TabInfo(Class<?> _class, Bundle _args){
clss = _class;
args = _args;
}
}
public TabsAdapter(SherlockFragmentActivity fa, ViewPager pager) {
super(fa.getSupportFragmentManager());
mContext = fa;
mActionBar = fa.getSupportActionBar();
mViewPager = pager;
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
}
public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args){
TabInfo info = new TabInfo(clss, args);
tab.setTag(info);
tab.setTabListener(this);
mTabs.add(info);
mActionBar.addTab(tab);
notifyDataSetChanged();
}
@Override
public void onPageScrollStateChanged(int state) {
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
mActionBar.setSelectedNavigationItem(position);
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
mViewPager.setCurrentItem(tab.getPosition());
Log.v(TAG, "clicked");
Object tag = tab.getTag();
for (int i = 0; i<mTabs.size(); i++){
if (mTabs.get(i) == tag){
mViewPager.setCurrentItem(i);
}
}
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// Toast.makeText(mContext, "You've deselected a tab", Toast.LENGTH_SHORT).show();
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override
public Fragment getItem(int position) {
TabInfo info = mTabs.get(position);
return Fragment.instantiate(mContext, info.clss.getName(), info.args);
}
@Override
public int getCount() {
return mTabs.size();
}
}
public class CSheet1Fragment extends Fragment {
Boolean tabletLayout = false;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.csheet1, null);
Fragment tempFragment = new CSheetStatsFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.flStats, tempFragment);
transaction.commit();
return v;
}
}
有人知道我做错了什么吗?我还确保我使用的是最新的支持库,
ActionbarSherlock
。
我正在使用
getChildFragmentManager()
,它对我来说效果很好。
尝试写完整路径android.support.v4.app.Fragment.getChildFragmentManager()
。
也请尝试我的支持 v4 这里。
我也遇到了类似的问题,我发现问题出在片段导入上。 尝试将 import android.app.Fragment 更改为 import android.support.v4.app.Fragment;