无法在添加到ViewPager的片段中使用getChildFragmentManager()

时间:2013-09-15 01:00:28

标签: android actionbarsherlock android-nested-fragment

我有一个使用ActionbarSherlockSlidingMenuViewPager的应用。这个应用程序运行良好但我试图使用嵌套片段引入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

2 个答案:

答案 0 :(得分:0)

我正在使用getChildFragmentManager(),它对我来说很好。 尝试编写完整路径android.support.v4.app.Fragment.getChildFragmentManager()。 尝试我的支持v4 here

答案 1 :(得分:0)

我有一个类似的问题,我发现问题出在片段导入上。 尝试将 import android.app.Fragment 更改为 import android.support.v4.app.Fragment;