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

jk9hmnmh  于 2023-01-07  发布在  Android
关注(0)|答案(2)|浏览(149)

我有一个应用程序正在使用ActionbarSherlockSlidingMenu,还有ViewPager。这个应用程序运行良好,但我试图将嵌套片段引入到ViewPager中。
目前,我有一个片段的ViewPager的每个页面,但我想有1至4个嵌套片段的页面片段,这取决于天气我显示在手机或平板电脑上。所以,如果我在手机上,我会说有14个页面的ViewPager,但在平板电脑上,我想合并这些页面利用嵌套片段。
下面是我尝试做的代码,但它不允许我使用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也是。

nnvyjq4y

nnvyjq4y1#

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

bqucvtff

bqucvtff2#

我也遇到过类似的问题,我发现问题出在片段导入上,尝试将import android. app. Fragment改为import android. support. v4. app. Fragment;

相关问题