2

我有一个应用程序正在使用ActionbarSherlock,SlidingMenuViewPager. 这个应用程序运行良好,但我正在尝试将使用嵌套片段引入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.

4

2 回答 2

0

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

于 2013-09-15T03:59:16.380 回答
0

我遇到了类似的问题,我发现问题出在 Fragment Import 上。尝试将import android.app.Fragment更改为 import android.support.v4.app.Fragment;

于 2018-10-21T14:16:06.723 回答