Until recently, using android.support.v4.app.FragmentPagerAdapter
would prevent the usage of a PreferenceFragment
as one of the Fragments used in the FragmentPagerAdapter.
The latest versions of the support v7 library now include the PreferenceFragmentCompat
class, which will work with a ViewPager and the v4 version of FragmentPagerAdapter.
Example Fragment that extends PreferenceFragmentCompat
:
import android.os.Bundle;
import android.support.v7.preference.PreferenceFragmentCompat;
import android.view.View;
public class MySettingsPrefFragment extends PreferenceFragmentCompat {
public MySettingsPrefFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.fragment_settings_pref);
}
@Override
public void onCreatePreferences(Bundle bundle, String s) {
}
}
You can now use this Fragment in a android.support.v4.app.FragmentPagerAdapter
subclass:
private class PagerAdapterWithSettings extends FragmentPagerAdapter {
public PagerAdapterWithSettings(FragmentManager supportFragmentManager) {
super(supportFragmentManager);
}
@Override
public Fragment getItem(int position) {
switch(position) {
case 0:
return new FragmentOne();
case 1:
return new FragmentTwo();
case 2:
return new MySettingsPrefFragment();
default:
return null;
}
}
// .......
}