2

I have an app that is using ActionbarSherlock, SlidingMenu, and also ViewPager. This app works well but I am trying to introduce using Nested Fragment into the ViewPager.

Currently I have one fragment for each page of the ViewPager but I want to have 1 to 4 Nested Fragments in page fragment, depending on weather I am displaying on a phone or tablets. So if I am on a phone I will say have 14 pages in the ViewPager but on a tablet I want to combine those pages utilizing nested fragments.

Here is the code that I trying to do but It will not allow me to use getChildFragmentManager(). It states that getChildFragmentManager() is undefined :

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;
}

}

Does anyone know what I am doing wrong? I have also made sure that I am using the latest support library and so is ActionbarSherlock.

Blo
  • 11,903
  • 5
  • 45
  • 99
user1713455
  • 31
  • 1
  • 2
  • also can you post the exact error you are getting? – rachit Sep 15 '13 at 04:00
  • You can also avoid to use `getChildFragmentManager` method by overriding the `getPageWidth` inside your ViewPager and display to fragments inside one page in tablet instead of one by one in phone. [See my answer](http://stackoverflow.com/a/23047128/2668136) for more information. – Blo Apr 17 '14 at 21:18

2 Answers2

0

I am using getChildFragmentManager(), and it is working fine for me. try writing full path android.support.v4.app.Fragment.getChildFragmentManager(). try my support v4 also here.

Milad Faridnia
  • 9,113
  • 13
  • 65
  • 78
rachit
  • 1,981
  • 15
  • 23
  • I have some other sample apps that I am using it in and they work but are a bitt different technique. I cant seem to figure out why in this app it is not. – user1713455 Sep 15 '13 at 04:17
  • I figured it out. The support library was current for my copy of SlidingMenu. Once this was fixed then getChildFragmentManager() worked. – user1713455 Sep 15 '13 at 05:24
0

I had a similar problem and I found that the problem was with the Fragment Import. Try to change import android.app.Fragment TO import android.support.v4.app.Fragment;

Shachar87
  • 149
  • 1
  • 1
  • 11