現(xiàn)在Fragment的應(yīng)用真的是越來越廣泛了,之前Android在3.0版本加入Fragment的時候,主要是為了解決Android Pad屏幕比較大,空間不能充分利用的問題,但現(xiàn)在即使只是在手機上,也有很多的場景可以運用到Fragment了,今天我們就來學(xué)習(xí)其中一個特別棒的應(yīng)用技巧。
很多手機應(yīng)用都會有一個非常類似的功能,即屏幕的下方顯示一行Tab標簽選項,點擊不同的標簽就可以切換到不同的界面,如以下幾個應(yīng)用所示:
? ? ? ? ? ? ?? ??? ??
上面三個應(yīng)用從左到右分別是QQ、新浪微博和支付寶錢包,可見,這種底部標簽式的布局策略真的非常常見。
那么話說回來,這種效果到底是如何的呢?熟悉Android的朋友一定都會知道,很簡單嘛,使用TabHost就OK了!但是殊不知,TabHost并非是那么的簡單,它的可擴展性非常的差,不能隨意地定制Tab項顯示的內(nèi)容,而且運行還要依賴于ActivityGroup。ActivityGroup原本主要是用于為每一個TabHost的子項管理一個單獨的Activity,但目前已經(jīng)被廢棄了。為什么呢?當然就是因為Fragment的出現(xiàn)了!查看Android官方文檔中ActivityGroup的描述,如下所示:
? ? ? ??
可以看到,在API 13的時候Android就已經(jīng)將ActivityGroup廢棄掉了,并且官方推薦的替代方式就是使用Fragment,因為它使用起來更加的靈活。那么剩下的問題就是如何借助Fragment來完成類似于TabHost一般的效果了,因此我們自然要動起手來了。
在開始之前,首先你必須已經(jīng)了解Fragment的用法了,如果你對Fragment還比較陌生的話,建議先去閱讀我前面的一篇文章?Android Fragment完全解析,關(guān)于碎片你所需知道的一切?。
另外,我們還應(yīng)該準備好程序所需要的資源,比如說每一個Tab項中所用到的圖片。我已經(jīng)事先從QQ里截好了幾張圖作為這個項目的資源,稍后會連同源碼一起給出。
新建一個項目,起名就叫FragmentDemo,這里我使用的是4.0的API。
下面開始編程工作,這里我們首先需要去編寫一個類似于QQ的主界面,當然只會去編寫界面最下方的TabHost部分,而不會編寫上面的內(nèi)容界面部分,因為內(nèi)容界面是應(yīng)該寫在Fragment的布局里的。打開或新建activity_main.xml作為程序的主布局文件,在里面加入如下代碼:
[html]?view plaincopy
<LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??????android:layout_width="match_parent"??????android:layout_height="match_parent"??????android:orientation="vertical"?>????????<FrameLayout??????????android:id="@+id/content"??????????android:layout_width="match_parent"??????????android:layout_height="0dp"??????????android:layout_weight="1"?>??????</FrameLayout>????????<LinearLayout??????????android:layout_width="match_parent"??????????android:layout_height="60dp"??????????android:background="@drawable/tab_bg"?>????????????<RelativeLayout??????????????android:id="@+id/message_layout"??????????????android:layout_width="0dp"??????????????android:layout_height="match_parent"??????????????android:layout_weight="1"?>????????????????<LinearLayout??????????????????android:layout_width="match_parent"??????????????????android:layout_height="wrap_content"??????????????????android:layout_centerVertical="true"??????????????????android:orientation="vertical"?>????????????????????<ImageView??????????????????????android:id="@+id/message_image"??????????????????????android:layout_width="wrap_content"??????????????????????android:layout_height="wrap_content"??????????????????????android:layout_gravity="center_horizontal"??????????????????????android:src="@drawable/message_unselected"?/>????????????????????<TextView??????????????????????android:id="@+id/message_text"??????????????????????android:layout_width="wrap_content"??????????????????????android:layout_height="wrap_content"??????????????????????android:layout_gravity="center_horizontal"??????????????????????android:text="消息"??????????????????????android:textColor="#82858b"?/>??????????????</LinearLayout>??????????</RelativeLayout>????????????<RelativeLayout??????????????android:id="@+id/contacts_layout"??????????????android:layout_width="0dp"??????????????android:layout_height="match_parent"??????????????android:layout_weight="1"?>????????????????<LinearLayout??????????????????android:layout_width="match_parent"??????????????????android:layout_height="wrap_content"??????????????????android:layout_centerVertical="true"??????????????????android:orientation="vertical"?>????????????????????<ImageView??????????????????????android:id="@+id/contacts_image"??????????????????????android:layout_width="wrap_content"??????????????????????android:layout_height="wrap_content"??????????????????????android:layout_gravity="center_horizontal"??????????????????????android:src="@drawable/contacts_unselected"?/>????????????????????<TextView??????????????????????android:id="@+id/contacts_text"??????????????????????android:layout_width="wrap_content"??????????????????????android:layout_height="wrap_content"??????????????????????android:layout_gravity="center_horizontal"??????????????????????android:text="聯(lián)系人"??????????????????????android:textColor="#82858b"?/>??????????????</LinearLayout>??????????</RelativeLayout>????????????<RelativeLayout??????????????android:id="@+id/news_layout"??????????????android:layout_width="0dp"??????????????android:layout_height="match_parent"??????????????android:layout_weight="1"?>????????????????<LinearLayout??????????????????android:layout_width="match_parent"??????????????????android:layout_height="wrap_content"??????????????????android:layout_centerVertical="true"??????????????????android:orientation="vertical"?>????????????????????<ImageView??????????????????????android:id="@+id/news_image"??????????????????????android:layout_width="wrap_content"??????????????????????android:layout_height="wrap_content"??????????????????????android:layout_gravity="center_horizontal"??????????????????????android:src="@drawable/news_unselected"?/>????????????????????<TextView??????????????????????android:id="@+id/news_text"??????????????????????android:layout_width="wrap_content"??????????????????????android:layout_height="wrap_content"??????????????????????android:layout_gravity="center_horizontal"??????????????????????android:text="動態(tài)"??????????????????????android:textColor="#82858b"?/>??????????????</LinearLayout>??????????</RelativeLayout>????????????<RelativeLayout??????????????android:id="@+id/setting_layout"??????????????android:layout_width="0dp"??????????????android:layout_height="match_parent"??????????????android:layout_weight="1"?>????????????????<LinearLayout??????????????????android:layout_width="match_parent"??????????????????android:layout_height="wrap_content"??????????????????android:layout_centerVertical="true"??????????????????android:orientation="vertical"?>????????????????????<ImageView??????????????????????android:id="@+id/setting_image"??????????????????????android:layout_width="wrap_content"??????????????????????android:layout_height="wrap_content"??????????????????????android:layout_gravity="center_horizontal"??????????????????????android:src="@drawable/setting_unselected"?/>????????????????????<TextView??????????????????????android:id="@+id/setting_text"??????????????????????android:layout_width="wrap_content"??????????????????????android:layout_height="wrap_content"??????????????????????android:layout_gravity="center_horizontal"??????????????????????android:text="設(shè)置"??????????????????????android:textColor="#82858b"?/>??????????????</LinearLayout>??????????</RelativeLayout>??????</LinearLayout>????</LinearLayout>?? 這段布局代碼雖然有點長,但其實主要就分為兩部分。第一個部分就是FrameLayout,這里只是給FrameLayout的id設(shè)置成content,并沒有在里面添加任何具體的內(nèi)容,因為具體的內(nèi)容是要在后面動態(tài)進行添加的。第二個部分就是FrameLayout下面的LinearLayout,這個LinearLayout中包含的就是整個類似于TabHost的布局。可以看到,我們將這個LinearLayout又等分成了四份,每一份中都會顯示一個ImageView和一個TextView。ImageView用于顯示當前Tab的圖標,TextView用于顯示當前Tab的標題,這個效果就會和QQ非常得類似。
?
既然是等分成了四分,那接下來我們自然要去分別實現(xiàn)四個Fragment和它們的布局了。新建一個message_layout.xml作為消息界面的布局,代碼如下所示:
[html]?view plaincopy
<?xml?version="1.0"?encoding="utf-8"?>??<RelativeLayout?xmlns:android="http://schemas.android.com/apk/res/android"??????android:layout_width="match_parent"??????android:layout_height="match_parent"?>????????<LinearLayout??????????android:layout_width="wrap_content"??????????android:layout_height="wrap_content"??????????android:layout_centerInParent="true"??????????android:orientation="vertical"?>????????????<ImageView??????????????android:layout_width="wrap_content"??????????????android:layout_height="wrap_content"??????????????android:layout_gravity="center_horizontal"??????????????android:src="@drawable/message_selected"?/>????????????<TextView??????????????android:layout_width="wrap_content"??????????????android:layout_height="wrap_content"??????????????android:layout_gravity="center_horizontal"??????????????android:padding="10dp"??????????????android:text="這是消息界面"??????????????android:textSize="20sp"?/>??????</LinearLayout>????</RelativeLayout>?? 這個布局就相對簡單多了,只是在屏幕的正中央顯示一個消息圖標,以及一段文字。
?
然后要去創(chuàng)建對應(yīng)這個布局的Fragment。新建MessageFragment繼承自Fragment,代碼如下所示:
[java]?view plaincopy
public?class?MessageFragment?extends?Fragment?{????????public?View?onCreateView(LayoutInflater?inflater,?ViewGroup?container,??????????????Bundle?savedInstanceState)?{??????????View?messageLayout?=?inflater.inflate(R.layout.message_layout,?container,?false);??????????return?messageLayout;??????}????}?? 后面就是依葫蘆畫瓢,把其它幾個Fragment以及對應(yīng)的布局創(chuàng)建出來。新建contacts_layout.xml作為聯(lián)系人界面的布局,代碼如下所示:
[html]?view plaincopy
<?xml?version="1.0"?encoding="utf-8"?>??<RelativeLayout?xmlns:android="http://schemas.android.com/apk/res/android"??????android:layout_width="match_parent"??????android:layout_height="match_parent"?>????????<LinearLayout??????????android:layout_width="wrap_content"??????????android:layout_height="wrap_content"??????????android:layout_centerInParent="true"??????????android:orientation="vertical"?>????????????<ImageView??????????????android:layout_width="wrap_content"??????????????android:layout_height="wrap_content"??????????????android:layout_gravity="center_horizontal"??????????????android:src="@drawable/contacts_selected"?/>????????????<TextView??????????????android:layout_width="wrap_content"??????????????android:layout_height="wrap_content"??????????????android:layout_gravity="center_horizontal"??????????????android:padding="10dp"??????????????android:text="這是聯(lián)系人界面"??????????????android:textSize="20sp"?/>??????</LinearLayout>????</RelativeLayout>?? 再新建ContactsFragment繼承自Fragment,代碼如下所示:
[java]?view plaincopy
public?class?ContactsFragment?extends?Fragment?{????????@Override??????public?View?onCreateView(LayoutInflater?inflater,?ViewGroup?container,??????????????Bundle?savedInstanceState)?{??????????View?contactsLayout?=?inflater.inflate(R.layout.contacts_layout,??????????????????container,?false);??????????return?contactsLayout;??????}????}?? 然后新建news_layout.xml作為動態(tài)界面的布局,代碼如下所示:
[html]?view plaincopy
<?xml?version="1.0"?encoding="utf-8"?>??<RelativeLayout?xmlns:android="http://schemas.android.com/apk/res/android"??????android:layout_width="match_parent"??????android:layout_height="match_parent"?>????????<LinearLayout??????????android:layout_width="wrap_content"??????????android:layout_height="wrap_content"??????????android:layout_centerInParent="true"??????????android:orientation="vertical"?>????????????<ImageView??????????????android:layout_width="wrap_content"??????????????android:layout_height="wrap_content"??????????????android:layout_gravity="center_horizontal"??????????????android:src="@drawable/news_selected"?/>????????????<TextView??????????????android:layout_width="wrap_content"??????????????android:layout_height="wrap_content"??????????????android:layout_gravity="center_horizontal"??????????????android:padding="10dp"??????????????android:text="這是動態(tài)界面"??????????????android:textSize="20sp"?/>??????</LinearLayout>????</RelativeLayout>?? 再新建NewsFragment繼承自Fragment,代碼如下所示:
[java]?view plaincopy
public?class?NewsFragment?extends?Fragment?{????????@Override??????public?View?onCreateView(LayoutInflater?inflater,?ViewGroup?container,??????????????Bundle?savedInstanceState)?{??????????View?newsLayout?=?inflater.inflate(R.layout.news_layout,?container,??????????????????false);??????????return?newsLayout;??????}????}?? 最后新建setting_layout.xml作為設(shè)置界面的布局,代碼如下所示:
[html]?view plaincopy
<?xml?version="1.0"?encoding="utf-8"?>??<RelativeLayout?xmlns:android="http://schemas.android.com/apk/res/android"??????android:layout_width="match_parent"??????android:layout_height="match_parent"?>????????<LinearLayout??????????android:layout_width="wrap_content"??????????android:layout_height="wrap_content"??????????android:layout_centerInParent="true"??????????android:orientation="vertical"?>????????????<ImageView??????????????android:layout_width="wrap_content"??????????????android:layout_height="wrap_content"??????????????android:layout_gravity="center_horizontal"??????????????android:src="@drawable/setting_selected"?/>????????????<TextView??????????????android:layout_width="wrap_content"??????????????android:layout_height="wrap_content"??????????????android:layout_gravity="center_horizontal"??????????????android:padding="10dp"??????????????android:text="這是設(shè)置界面"??????????????android:textSize="20sp"?/>??????</LinearLayout>????</RelativeLayout>?? 再新建SettingFragment繼承自Fragment,代碼如下所示:
[java]?view plaincopy
public?class?SettingFragment?extends?Fragment?{????????@Override??????public?View?onCreateView(LayoutInflater?inflater,?ViewGroup?container,??????????????Bundle?savedInstanceState)?{??????????View?settingLayout?=?inflater.inflate(R.layout.setting_layout,??????????????????container,?false);??????????return?settingLayout;??????}????}?? 這樣我們就把每一個Fragment,以及它們所對應(yīng)的布局文件都創(chuàng)建好了。接下來也就是最關(guān)鍵的步驟了,打開或新建MainActivity作為主Activity,代碼如下所示:
[java]?view plaincopy
??????public?class?MainActivity?extends?Activity?implements?OnClickListener?{????????????????private?MessageFragment?messageFragment;????????????????private?ContactsFragment?contactsFragment;????????????????private?NewsFragment?newsFragment;????????????????private?SettingFragment?settingFragment;????????????????private?View?messageLayout;????????????????private?View?contactsLayout;????????????????private?View?newsLayout;????????????????private?View?settingLayout;????????????????private?ImageView?messageImage;????????????????private?ImageView?contactsImage;????????????????private?ImageView?newsImage;????????????????private?ImageView?settingImage;????????????????private?TextView?messageText;????????????????private?TextView?contactsText;????????????????private?TextView?newsText;????????????????private?TextView?settingText;????????????????private?FragmentManager?fragmentManager;????????@Override??????protected?void?onCreate(Bundle?savedInstanceState)?{??????????super.onCreate(savedInstanceState);??????????requestWindowFeature(Window.FEATURE_NO_TITLE);??????????setContentView(R.layout.activity_main);????????????????????initViews();??????????fragmentManager?=?getFragmentManager();????????????????????setTabSelection(0);??????}????????????????private?void?initViews()?{??????????messageLayout?=?findViewById(R.id.message_layout);??????????contactsLayout?=?findViewById(R.id.contacts_layout);??????????newsLayout?=?findViewById(R.id.news_layout);??????????settingLayout?=?findViewById(R.id.setting_layout);??????????messageImage?=?(ImageView)?findViewById(R.id.message_image);??????????contactsImage?=?(ImageView)?findViewById(R.id.contacts_image);??????????newsImage?=?(ImageView)?findViewById(R.id.news_image);??????????settingImage?=?(ImageView)?findViewById(R.id.setting_image);??????????messageText?=?(TextView)?findViewById(R.id.message_text);??????????contactsText?=?(TextView)?findViewById(R.id.contacts_text);??????????newsText?=?(TextView)?findViewById(R.id.news_text);??????????settingText?=?(TextView)?findViewById(R.id.setting_text);??????????messageLayout.setOnClickListener(this);??????????contactsLayout.setOnClickListener(this);??????????newsLayout.setOnClickListener(this);??????????settingLayout.setOnClickListener(this);??????}????????@Override??????public?void?onClick(View?v)?{??????????switch?(v.getId())?{??????????case?R.id.message_layout:????????????????????????????setTabSelection(0);??????????????break;??????????case?R.id.contacts_layout:????????????????????????????setTabSelection(1);??????????????break;??????????case?R.id.news_layout:????????????????????????????setTabSelection(2);??????????????break;??????????case?R.id.setting_layout:????????????????????????????setTabSelection(3);??????????????break;??????????default:??????????????break;??????????}??????}???????????????????private?void?setTabSelection(int?index)?{????????????????????clearSelection();????????????????????FragmentTransaction?transaction?=?fragmentManager.beginTransaction();????????????????????hideFragments(transaction);??????????switch?(index)?{??????????case?0:????????????????????????????messageImage.setImageResource(R.drawable.message_selected);??????????????messageText.setTextColor(Color.WHITE);??????????????if?(messageFragment?==?null)?{????????????????????????????????????messageFragment?=?new?MessageFragment();??????????????????transaction.add(R.id.content,?messageFragment);??????????????}?else?{????????????????????????????????????transaction.show(messageFragment);??????????????}??????????????break;??????????case?1:????????????????????????????contactsImage.setImageResource(R.drawable.contacts_selected);??????????????contactsText.setTextColor(Color.WHITE);??????????????if?(contactsFragment?==?null)?{????????????????????????????????????contactsFragment?=?new?ContactsFragment();??????????????????transaction.add(R.id.content,?contactsFragment);??????????????}?else?{????????????????????????????????????transaction.show(contactsFragment);??????????????}??????????????break;??????????case?2:????????????????????????????newsImage.setImageResource(R.drawable.news_selected);??????????????newsText.setTextColor(Color.WHITE);??????????????if?(newsFragment?==?null)?{????????????????????????????????????newsFragment?=?new?NewsFragment();??????????????????transaction.add(R.id.content,?newsFragment);??????????????}?else?{????????????????????????????????????transaction.show(newsFragment);??????????????}??????????????break;??????????case?3:??????????default:????????????????????????????settingImage.setImageResource(R.drawable.setting_selected);??????????????settingText.setTextColor(Color.WHITE);??????????????if?(settingFragment?==?null)?{????????????????????????????????????settingFragment?=?new?SettingFragment();??????????????????transaction.add(R.id.content,?settingFragment);??????????????}?else?{????????????????????????????????????transaction.show(settingFragment);??????????????}??????????????break;??????????}??????????transaction.commit();??????}????????????????private?void?clearSelection()?{??????????messageImage.setImageResource(R.drawable.message_unselected);??????????messageText.setTextColor(Color.parseColor("#82858b"));??????????contactsImage.setImageResource(R.drawable.contacts_unselected);??????????contactsText.setTextColor(Color.parseColor("#82858b"));??????????newsImage.setImageResource(R.drawable.news_unselected);??????????newsText.setTextColor(Color.parseColor("#82858b"));??????????settingImage.setImageResource(R.drawable.setting_unselected);??????????settingText.setTextColor(Color.parseColor("#82858b"));??????}???????????????????private?void?hideFragments(FragmentTransaction?transaction)?{??????????if?(messageFragment?!=?null)?{??????????????transaction.hide(messageFragment);??????????}??????????if?(contactsFragment?!=?null)?{??????????????transaction.hide(contactsFragment);??????????}??????????if?(newsFragment?!=?null)?{??????????????transaction.hide(newsFragment);??????????}??????????if?(settingFragment?!=?null)?{??????????????transaction.hide(settingFragment);??????????}??????}??}?? 這個類中的注釋已經(jīng)寫得非常詳細了,下面我再帶大家簡單梳理一遍。在onCreate()方法中先是調(diào)用了initViews()來獲取每個控件的實例,并給相應(yīng)的控件設(shè)置好點擊事件,然后調(diào)用setTabSelection()方法設(shè)置默認的選中項,這里傳入的0說明默認選中第1個Tab項。
?
那么setTabSelection()方法中又是如何處理的呢?可以看到,首先第一步是調(diào)用clearSelection()方法來清理掉之前的選中狀態(tài),然后開啟一個Fragment事務(wù),并隱藏掉所有的Fragment,以防止有多個Fragment顯示在界面上。接下來根據(jù)傳入的index參數(shù)判斷出選中的是哪一個Tab項,并改變該Tab項的圖標和文字顏色,然后將相應(yīng)的Fragment添加到界面上。這里注意一個細節(jié),我們添加Fragment的時候并沒有使用replace()方法,而是會先判斷一下該Fragment是否為空,如果是空的則調(diào)用add()方法添加一個進來,如果不是空的則直接調(diào)用show()方法顯示出來即可。那么為什么沒有使用replace()方法呢?這是因為replace()方法會將被替換掉的那個Fragment徹底地移除掉,該Fragment的生命周期就結(jié)束了。當再次點擊剛才那個Tab項的時候,就會讓該Fragment的生命周期重新開始,onCreate()、onCreateView()等方法都會重新執(zhí)行一遍。這顯然不是我們想要的,也和ActivityGroup的工作原理不符,因此最好的解決方案就是使用hide()和show()方法來隱藏和顯示Fragment,這就不會讓Fragment的生命周期重走一遍了。
設(shè)置完默認選中項后,我們當然還可以通過點擊Tab項來自由地切換界面,這就會進入到onClick()方法中。onClick()方法中的邏輯判斷非常簡單,當點擊了消息標簽時就會選中第1個tab項,點擊聯(lián)系人標簽時就會選中第2個tab項,點擊動態(tài)標簽時就會選中第3個tab項,點擊設(shè)置標簽時就會選中第4個tab項。都是通過調(diào)用setTabSelection()方法來完成的,只是傳入了不同的參數(shù)。
好了,這樣我們就將全部的代碼都編寫完成了,下面就來運行一下吧。整個Tab的界面有點類似于QQ的感覺,并且可以通過點擊不同的Tab來切換界面,如下圖所示:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
另外,這個Tab界面即使在橫屏的情況下也有不錯的適用性哦,如下圖所示:
? ? ? ? ? ? ? ? ? ? ? ? ? ??
這樣,我們就成功使用Fragment編寫出了和TabHost一樣的效果。每個界面的具體邏輯就可以寫在相應(yīng)的Fragment里,效果和之前寫在Activity里是差不多的。如此一來,我們終于可以和那個被廢棄的ActivityGroup說再見了!
好了,今天的講解到此結(jié)束,有疑問的朋友請在下面留言。
源碼下載,請點擊這里
原文:http://blog.csdn.net/guolin_blog/article/details/13171191
總結(jié)
以上是生活随笔為你收集整理的Android Fragment应用实战的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。