2014년 2월 21일 금요일

support v4 library 사용 중 PreferenceFragment를 FragmentPagerAdapter 추가 시 문제점

하위 기기 호환을 위해서 android support v4 library
(http://developer.android.com/tools/support-library/index.html)를 사용하여
fragment들을 사용, 관리하고 있는 중에
preference fragment를 fragmentPagerAdapter에 추가 시 문제가 발생함.

android.support.v4.app.FragmentPagerAdapter 에서는
android.support.v4.app.Fragment 를 처리하고 있어
android.app.Fragment를 상속받은 PreferenceFragment을
v4.appFragment로 처리 할 수 없음.

좀 찾아봤으나 어쩔 수 없이 v13 library를 사용하는 것으로 마무리..
더 찾기도 귀찮음.

http://stackoverflow.com/questions/15845632/adding-preferencefragment-to-fragmentpageradapter

This answer led me to the solution of using the v13 support library, which includes a FragmentPagerAdapter that uses bona-fide android.app.Fragments so it can support the PreferenceFragment.
Assuming you use Eclipse and run the new app wizard with the "Scrollable Tabs + Swipe" Navigation (which gives you the v4 pager boilerplate), here are the modifications you need to make to upgrade to v13:
  • Delete "android-support-v4.jar" file from your libs folder
  • Copy "android-support-v13.jar" from SDK_PATH\extras\android\support\v13; if it's not there, use the SDK manager to install or update "Extras/Android Support Library"
Then, in the Java file:
  • Change FragmentPagerAdapter import from v4 to v13
  • Change FragmentActivity to a plain Activity
  • Change calls to getSupportFragmentManager to getFragmentManager
  • Import all necessary classes from android.app instead of android.support.v4
  • (Except: you still need to use the v4 ViewPager, but it's compatible)
I've copied the modified source below, verified on latest Jellybean.

댓글 없음:

댓글 쓰기