Add PreferenceFragment on FragmentPagerAdapter

99封情书 提交于 2020-01-03 19:31:32

问题


I use the last SDK to create an app with navigation type Tabs + Swipe and I want to put a PreferenceFragment on one of the tabs.

The generated code is like :

mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);

Meanwhile, I can't add PreferenceFragment on the SectionsPagerAdapter because its method getItem(int position) returns a Fragment, not a FragmentActivity.

I may need some help right here.


回答1:


The issue is that ViewPager's getItem(..) method returns a support Fragment (i.e. android.support.v4.app.Fragment) rather than android.app.Fragment, which is what PreferenceFragments extend. Basically, a PreferenceFragment uses the non-support version of the APIs and so is incompatible with ViewPagers, and there is no support version of PreferenceFragment available.




回答2:


You can upgrade to a v13 pager which supports the android.app.Fragment. I gave a detailed answer on a similar question here.



来源:https://stackoverflow.com/questions/11786229/add-preferencefragment-on-fragmentpageradapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!