如何在ViewPager中添加PreferenceFragment?

时间:2018-03-11 22:52:44

标签: android android-fragments android-viewpager

我的所有片段都是android.support.v4.app.Fragment,而Viewpager扩展了FragmentStatePagerAdapter,它也使用了android.support.v4.app.Fragment。

我有一个Fragment类,它扩展了我想在我的Viewpager中添加其他两个片段的PreferenceFragment。

我怎样才能做到这一点?

这是我的ViewPager

public class SectionsPagerAdapter extends FragmentStatePagerAdapter {

private ArrayList<Fragment> mFragments = new ArrayList<>();
private ArrayList<String> mFragmentsTitle = new ArrayList<>();

public void addFragment(Fragment fragment, String title){
    mFragments.add(fragment);
    mFragmentsTitle.add(title);
}

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

@Override
public Fragment getItem(int position) {
    return mFragments.get(position);
}

@Override
public int getCount() {
    return mFragments.size();
}

@Override
public CharSequence getPageTitle(int position) {
    return mFragmentsTitle.get(position);
}


}

这是我在PreferenceFragment中的内容

public class GameNotesFragment extends PreferenceFragment {


public GameNotesFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_game_notes, container, false);
    return view;
}

}

当我尝试添加它时:

mSectionsPagerAdapter.addFragment(new GameNotesFragment(), "Note");

它给了我

Wrong firt argument com.example.fragments.GameNotesFragment', required: 'android.support.v4.app.Fragment' 

2 个答案:

答案 0 :(得分:1)

PreferenceFragment扩展了Fragment类的平台变体。你需要的是PreferenceFragmentCompat,它扩展了Fragment的支持库变体(注意不同的包)。

支持首选项需要您的gradle文件中的其他包:

implementation 'com.android.support:preference-v7:27.1.0'
implementation 'com.android.support:preference-v14:27.1.0'

此外,在您使用它的同时,请务必查看我为支持偏好设置创建的bug fix lib常见(并且非常烦人)错误。

答案 1 :(得分:0)

public class SectionsPagerAdapter extends FragmentStatePagerAdapter {

    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private ArrayList<String> mFragmentsTitle = new ArrayList<>();

    public void addFragment(Fragment fragment, String title){
        mFragments.add(fragment);
        mFragmentsTitle.add(title);
    }

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

    @Override
    public android.support.v4.app.Fragment getItem(int position) {
        android.support.v4.app.Fragment f = null;
        if (position == 0) {
            f = new GameNotesFragment();
        }

        if (position == 1) {
            f = new GameNotesFragment(); //replace your fragment 2
        }

        if (position == 2) {
            f = new GameNotesFragment();//replace your fragment 3
        }

        return f;
    }

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

    @Override
    public CharSequence getPageTitle(int position) {

        switch (position) {
            case 0:
                return getResources().getString(R.string.name1);
            case 1:
                return getResources().getString(R.string.name2);
            case 2:
                return getResources().getString(R.string.name3);
        return null;
    }
}

和活动:

SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(MainActivity.this.getSupportFragmentManager());
tabLayout = v1.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));