无法在Fragment里面获得支持FragmentManager - 在Fragment里面的ViewPager

时间:2016-08-02 12:17:50

标签: android android-fragments android-viewpager fragment fragmentmanager

我正在尝试使用NavigationDrawer创建活动,并且在一个片段中,我想放置ViewPager。问题是,当我尝试创建新的SectionPagerAdapter(扩展FragmentPagerAdapter的类)时,我无法获得适当的FragmentManager作为构造函数。

我想放置ViewPager的navDrawer片段的onCreateView代码:

@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类:

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

和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

        }

    }

现在我在调用getChuldFragmentManager()时出错,它说android.app.FragmentManager不能应用于android.support.v4.app.FragmentManager

解决 我在我的课程中扩展了android.app.Fragment而不是android.support.v4.app.Fragment

2 个答案:

答案 0 :(得分:0)

删除导入import android.app.FragmentManager;并添加此

import android.support.v4.app.FragmentManager;

答案 1 :(得分:0)

根据这个

  

android.support.v4.FragmentManager无法应用于android.app.FragmentManager

您在android.app.FragmentManager课程中使用SectionsPagerAdapter。将其更改为android.support.v4.FragmentManager