问题
I want to build a preference tab for my app but since I have other tabs extending from a regular fragment, I have an incompatible types error, as a PreferenceFragment cannot be converted to Fragment.
Here is my Tab Fragment Adapter :
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Tab1Discover tab1 = new Tab1Discover();
return tab1;
case 1:
Tab2Planning tab2 = new Tab2Planning();
return tab2;
case 2:
Tab3Favorites tab3 = new Tab3Favorites();
return tab3;
case 3:
Tab4Messages tab4 = new Tab4Messages();
return tab4;
case 4 :
/*Tab5Profile tab5 = new Tab5Profile();
return tab5;*/
return new PreferenceFragment() {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
};
default:
return null;
}
}
@Override
public int getCount() {
return mNumOfTabs;
}
}
回答1:
There are incompatible types, the problem is - your fragments inherited from support.v4.app.Fragment, but PreferenceFragment inherited from android.app.Fragment.
There are two ways you could deal with it:
- Inherit all your fragments from android.app.Fragment
- Reimplement PreferenceFragment by taken it source code from here for example and inherit it from support.v4.app.Fragment
来源:https://stackoverflow.com/questions/33328177/preference-fragment-tab-adapter