跳到主要內容

在 Fragment 裡使用 ViewPager 搭配 FragmentPagerAdapter

        Fragment 是個強大的東西,可以想成 Activity 的子頁面,可隨時替換頁面內容(但使用起來還滿麻煩的…)。這麼強大的東西,在 ViewPager 當然也會提供 Fragment 的版本。在官方文件就有一個 PagerAdapter 的實作:FragmentPagerAdapter,可以用 Fragment 來當成每個 ViewPager 的子頁。FragmentPagerAdapter 的官方文件中也有提供 sample code。

        但我自己參考 sample code 寫出來的效果卻怪怪的,ViewPager 裡子頁面的 lifecycle 竟然沒有跟著父 Fragment ,看起來比較像是跟著 Activity。仔細看 sample code 才發現,它是在 Activity 裡使用 ViewPager + FragmentPagerAdapter。那要如何使用在 Fragment 呢?

        其實只要改一行 code 就可以了。 sample code 裡的
mAdapter = new MyAdapter(getSupportFragmentManager()); 只要改成
mAdapter = new MyAdapter(Fragment.getChildFragmentManager()); 就可以囉 ~~

        由於 getChildFragmentManager()  api level 17+ 才有的東西,而 Fragment 是 11+ 才有,不想定太高的 api level 可以使用 support v4 library,怎麼使用就不多說啦。

參考資料:

support v4 Fragment:
http://developer.android.com/intl/zh-tw/reference/android/support/v4/app/Fragment.html

support v4  FragmentPagerAdapter:
http://developer.android.com/intl/zh-w/reference/android/support/v4/app/FragmentPagerAdapter.html


留言

這個網誌中的熱門文章

如何把 Status Bar 變透明

        Android 從 4.4 (KitKat, api level 19) 後才支援這個功能, 到了 5.0 (Lollipop, api level 21) 自訂性更高, 可以讓我們設定各種顏色, 當然也包含透明色。以下分別介紹如何使用這兩種版本的方法。         方法1: 利用 attribute " android:windowTranslucentStatus ", 在 style.xml 加上這個 attribute 就好。要注意的是 Android 版本要在 4.4 以上才可以用這個 attribute: <resources> <!-- Base application theme for API 19+. This theme completely replaces AppTheme from res/values/styles.xml on API 19+ devices. --> <style name="AppTheme" parent="@style/AppBaseTheme"> <!-- API 19 theme customizations can go here. --> <item name="android:windowTranslucentStatus">true</item> </style> </resources>         下面的圖分別為 4.4 跟 5.0 的手機使用這個 attribute 的結果:         因為設定了這個 attribute, 畫面會從 status bar 下方開始畫。要解決這個有兩種方法, 第一個是在 layout 畫面設定 attribute "android:fitsSystemWindows " <RelativeLayout xmlns:android="http:...

Android O 多了什麼新東西 ??

Background Execution Limits 1. 大部份在 AndroidMenifast 宣告的 Implicit BroadcastReceiver 不會被呼叫,但有些還是可以 work,如以下: BOOT_COMPL ETELOCALE_CHANGED USB_DEVICE_ATTACHED Explicit BroadcastReceiver 沒什麼改變,還是可正常發送。Runtime Implicit BroadcastReceiver 也沒什麼改變。 2. Service 也有些改變,如以下:   Run minimally after the app has been background (這要開發者怎麼決定哪些是一定要在背景執行的阿…) App 在背景時,不能使用 startService() 跟 PendingIntent Foreground service 不變,多了新 API 可以使用,NotificationManager#startServiceInForeground 3. When you app enter cache state with no active component, weak locks be automatically released. 4. Location Background Location Limited updates, few times per hour Passive Location Unchanged (這句在說啥?) 有其他 app 在前景使用 Location update,你的 background location 更新的頻率會跟前景一樣 (所以重點就是要有 app 在前景要 location 的資料!) Foreground Location 沒改變 Settings.Secure.ANDROID_ID 其實這個我看不懂他在說什麼,之前完全沒碰過… pre-O:每個 app 的 ID 都一樣 post-O:每個 app 都會有個獨立的 ID (影片說由 Google Play Service 管嗎?) Account access 不能用 GET_ACCOUNTS 這個 permis...

透明背景的 AlertDialog

        Android dialog 預設都會有一個背景底色,要怎麼把它去掉呢?其實直接改 AlertDialog 的 style 就可以了,但我改好久…以下的範例是使用了 Support v7 AppCompat 的 theme,其他 theme 我不知道可不可以,有興趣的人可以試試看。 <style name="TransparentDialog" parent="@style/Base.Theme.AppCompat.Dialog"> <item name="android:windowFrame">@null</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowIsFloating">true</item> <item name="android:windowTitleStyle">@null</item> <item name="android:background">@android:color/transparent</item> <item name="android:windowNoTitle">true</item> <item name="android:alertDialogStyle">@style/TransparentDialog.Color</item> </style> <style name="TransparentDialog.Color" parent="@style/Base.Theme.AppCompat.Dialog"> <item name="androi...