Qiteq Qiteq - 6 months ago 81
Android Question

Cannot get support FragmentManager inside Fragment - ViewPager inside Fragment

I am trying to create activity with NavigationDrawer, and inside one Fragment, i want to place ViewPager. The problem is, when i am trying to create new SectionPagerAdapter (class extending FragmentPagerAdapter), i cant get proper FragmentManager for constructor.

There is code of onCreateView of navDrawer fragment where i want to place ViewPager:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.layout_anime, container, false);

final ActionBar actionBar = getActivity().getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//There is problem:
mSectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());
mViewPager = (ViewPager) getActivity().findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);

mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});

for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}

return rootView;
}


SectionPagerAdapter class:

public class SectionsPagerAdapter extends FragmentPagerAdapter {

public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public android.support.v4.app.Fragment getItem(int position) {
android.support.v4.app.Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}

@Override
public int getCount() {
return 3;
}

@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return "Section 1".toUpperCase();
case 1:
return "Section 2".toUpperCase();
case 2:
return "Section 3".toUpperCase();
}
return null;
}
}


and DummySectionFragment:

public static class DummySectionFragment extends android.support.v4.app.Fragment {
public static final String ARG_SECTION_NUMBER = "section+number";

public DummySectionFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.layout_anime, container, false);
ListView listView = (ListView)

//My code here - http request and listView creating

}

}


Now i have an error when calling getChuldFragmentManager(), which says that android.app.FragmentManager cannot be applied to android.support.v4.app.FragmentManager

SOLVED
I was extending android.app.Fragment instead of android.support.v4.app.Fragment in my class

Answer

According to this

android.support.v4.FragmentManager cannot be applied to android.app.FragmentManager

you are using android.app.FragmentManager in SectionsPagerAdapter class. Change it to android.support.v4.FragmentManager.