轉載出處:http://blog.csdn.net/yangyu20121224/article/details/8995025
轉載,請自覺註明!
由於TabActivity在Android4.0以後已經被完全棄用,那麼我就不再浪費口水繼續講解它了,取而代之的是Fragment。Fragment是Android3.0新增的概念,Fragment翻譯成中文是碎片的意思,不過卻和Activity十分的相似,這一篇我花大量的篇幅來詳細的講解Fragment的介紹和使用方法。
一、Fragment的基礎知識介紹
1.1概述
1.1.1 特性
Fragment是activity的界面中的一部分或一種行爲。可以把多個Fragment組合到一個activity中來創建一個多界面
並且可以在多個activity中重用一個Fragment。可以把Fragment任務模塊化的一段activity,它具有自己的生命週期,
接收它自己的事件,並可以在activity運行時被添加或刪除。
Fragment不能獨立存在,它必須嵌入到activity中,而且Fragment的生命週期直接受所在的activity的影響。例
如:當activity暫停時,他擁有的所有的Fragment都暫停了,當activity銷燬時,他擁有的所有Fragment都被銷燬。然
而,當activity運行時(在onResume()之後,onPause()之前),可以單獨地操作每個Fragment,比如添加或刪除它
們。當中執行上述針對Fragment的事務時,可以將事務添加到一個棧中,這個棧被activity管理,棧中的每一條都是一
個Fragment的一次事務。有了這個棧,就可以反向執行Fragment的事務,這樣就可以在Fragment級支持“返回”鍵
(向後導航)。
當向activity中添加一個Fragment時,它須置於ViewGroup控件中,並且需定義Fragment自己的界面。可以在
layout.xml佈局文件中聲明Fragment,元素爲:<fragment>;也可以在代碼中創建Fragment,然後把它加入到
ViewGroup控件中。然而,Fragment不一定非要放在activity的界面中,它可以隱藏在後臺爲activity工作。
1.1.2 生命週期
onCreate():
當創建fragment時系統調用此方法。在其中必須初始化fragment的基礎組件們。可參考activity的說明;
onCreateView():
系統在fragment要畫自己的界面時調用(在真正顯示之前)此方法,這個方法必須返回fragment的layout的根控
件,如果這個fragment不提供界面,那它應返回null;
onPause():
大多數程序應最少對fragment實現這三個方法,當然還有其它幾個回調方法可應該按情況實現之,所有的聲明週期
回調函數在“操控fragment的生命週期”一節中有詳細討論。
下圖爲fragment的生命週期(它所在的activity處於運行狀態)
Activity和Fragment生命週期對比圖如下:
兩個的生命週期很類似,也息息相關。
1.1.3 派生類
DialogFragment
顯示一個浮動的對話框。使用這個類創建對話框是替代activity創建對話框的最佳選擇。因爲可以把fragmentdialog
放入到activity的返回棧中,使用戶能再返回到這個對話框。
ListFragment
顯示一個列表控件,就像ListActivity類,它提供了很多管理列表的方法,比如onListItemClick()方法響應click事件。
PreferenceFragment
顯示一個由Preference對象組成的列表,與PreferenceActivity相同。它用於爲程序創建“設置”activity。
1.2 範例
寫一個讀新聞的程序,可以用一個fragment顯示標題列表,另一個fragment顯示選中標題的內容,這兩個fragment
都在一個activity上,並排顯示。那麼這兩個fragment都有自己的生命週期並響應自己感興趣的事件。於是,不需要再
像手機上那樣用一個activity顯示標題列表,用另一個activity顯示新聞內容;現在可以把兩者放在一個activity上同時顯
示出來。如下圖:
Fragment必須被寫成可重用的模塊。因爲fragment有自己的layout,自己進行事件響應,擁有自己的生命週期和
行爲,所以可以在多個activity中包含同一個Fragment的不同實例。這對於讓世界在不同的屏幕尺寸下都能給用戶完美
的體驗尤其重要。比如可以在程序運行於大屏幕中時啓動包含很多fragment的activity,而在運行小屏幕時啓動一個包
含少量fragment的activity。
剛纔讀新聞的程序,當檢測到程序運行於大屏幕時,啓動activityA,將標題列表和新聞內容這兩個fragment都放
在activityA中;當檢測到程序運行於小屏幕時,還是啓動activityA,但此時A中只有標題列表fragment,當選中一個標
題時,activityA啓動activityB,B中含有新聞內容fragment。
1.3 創建Fragment
要創建fragment,必須從Fragment或Fragment的派生類派生出一個類。Fragment的代碼寫起來有些像activity。
它具有跟activity一樣的回調方法,比如onCreate(),onStart(),onPause()和onStop()。實際上,如果想把老的程序改爲
使用fragment,基本上只需要把activity的回調方法的代碼移到fragment中對應的方法即可。
1.3.1添加有界面的Fragment
Fragment一般作爲activity的用戶界面的一部分,把它自己layout嵌入到activity的layout中。一個要爲fragment提
供layout,必須實現onCreateView()回調方法,然後在這個方法中返回一個View對象,這個對象時fragment的layout的
根。
注意:如果fragment是從ListFragment中派生的,就不需要實現onCreateView()方法了,因爲默認的實現已經返
回了ListView控件對象。
要從onCreateView()方法中返回layout對象,可以從layout.xml佈局文件中生成layout對象。爲了幫助這樣做,
onCreateView()提供了一個layoutInflater對象。舉例:以下代碼展示了一個Fragment的子類如何從layout.xml佈局文件
example_fragment.xml中生成對象。
- <span style="font-size:10px;">public static ExampleFragment extends Fragment {
- @Override
- publicView onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- returninflater.inflate(R.layout.example_fragment, container, false);
- }
- }</span>
onCreateView()參數中的container是存放fragment的layout的ViewGroup對象。saveInstanceState參數是一個
Bundle,跟activity的onCreate()中Bundle差不多,用於狀態恢復。但是fragment的onCreate()中也有Bundle參數,所
以此處的Bundle中存放的數據與onCreate()中存放的數據還是不同的。
Inflate()方法中有三個參數:
<1> layout的資源ID;
<2> 存放fragment的layout的ViewGroup;
<3> 布爾數據表示是否在創建fragment的layout期間,把layout附加到container上(在這個例子中,因爲系統已經把layout插入到container中了,所以值爲false,如果爲true會導致在最終的layout中創建多餘的ViewGroup)。
下面講述如何把它添加到activity中。把fragment添加到activity一般情況下,fragment把它的layout作爲activity的
layout的一部分合併到activity中,有兩種方法將一個fragment添加到activity中:
方法一:在activity的layout.xml文件中聲明fragment
- <?xmlversionxmlversion="1.0" encoding="utf-8" ?>
- <LinearLayoutxmlns:androidLinearLayoutxmlns:android=" http://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <fragmentandroid:namefragmentandroid:name="com.android.cwj.ArticleListFragment"
- android:id="@+id/list"
- android:layout_weight="1"
- android:layout_width="0dp"
- android:layout_height="match_parent" />
- <fragmentandroid:namefragmentandroid:name="com.android.cwj.ArticleReaderFragment"
- android:id="@+id/viewer"
- android:layout_weight="2"
- android:layout_width="0dp"
- android:layout_height="match_parent" />
- </LinearLayout>
以上代碼中,<fragment>中聲明一個fragment。當系統創建上例中的layout時,它實例化每一個fragment,然後調
用它們的onCreateView()方法,以獲取每個fragment的layout。系統把fragment返回的view對象插入到<fragment>元
素的位置,直接代替<fragment>元素。
注:每個fragment都需要提供一個ID,系統在activity重新創建時用它來恢復fragment,也可以用它來操作fragment進
行其它的事物,比如刪除它。有三種方法給fragment提供ID:
<1> 爲Android:id屬性賦一個數字;
<2> 爲Android:tag屬性賦一個字符串。
如果沒有使用上述任何一種方法,系統將使用fragment的容器的ID。
方法二:在代碼中添加fragment到一個ViewGroup
這種方法可以在運行時,把fragment添加到activity的layout中。只需指定一個要包含fragment的ViewGroup。爲
了完成fragment的事務(比如添加,刪除,替換等),必須使用FragmentTransaction的方法。可以從activity獲取
FragmentTransaction,如下:
- FragmentManager fragmentManager = getFragmentManager();
- FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
然後可以用add()方法添加一個fragment,它有參數用於指定容納fragment的ViewGroup。如,Add()的第一個參數
是容器ViewGroup,第二個是要添加的fragment。一旦通過FragmentTransaction對fragment做出了改變,必須調用方
法commit()提交這些改變。不僅在無界面的fragment中,在有界面的fragment中也可以使用tag來作爲唯一的標誌,這
樣在需要獲取fragment對象時,要調用findFragmentTag()。
1.3.2 添加沒有界面的Fragment
上面演示瞭如何添加fragment來提供界面,然而,也可以使用fragment爲activity提供後臺的行爲而不用顯示
fragment的界面。要添加一個沒有界面的fragment,需要在activity中調用方法add(Fragment,String)(它支持用一個唯
一的字符串做爲fragment的“tag”,而不是viewID)。這樣添加的fragment由於沒有界面,所以在實現它時不需要調用
實現onCreateView()方法。
使用tag字符串來標示一個fragment並不是只能用於沒有界面的fragment上,也可以把它用於有界面的fragment
上,但是,如果一個fragment沒有界面,tag字符串將成爲它唯一的選擇。獲取以tag表示的fragment,需使用方法
findFragmentByTab()。
1.4 Fragment管理
要管理fragment,需使用FragmentManager,要獲取它,需在activity中調用方法getFragmentManager()。
可以用FragmentManager來做以下事情:
<1> 使用方法findFragmentById()或findFragmentByTag(),獲取activity中已存在的fragment;
<2> 使用方法popBackStack()從activity的後退棧中彈出fragment(這可以模擬後退鍵引發的動作),用方法addOnBackStackChangedListenner()註冊一個偵聽器以監視後退棧的變化;
<3> 還可以使用FragmentManager打開一個FragmentTransaction來執行fragment的事務,比如添加或刪除fragment。
在activity中使用fragment的一個偉大的好處是能根據用戶的輸入對fragment進行添加、刪除、替換以及執行其他
動作的能力。提交的一組fragment的變化叫做一個事務。事務通過FragmentTransaction來執行。還可以把每個事務保
存在activity的後退棧中,這樣就可以讓用戶在fragment變化之間導航(跟在activity之間導航一樣)。
可以通過FragmentManager來取得FragmentTransaction的實例,如下:
- FragmentManager fragmentManager = getFragmentManager();
- FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
一個事務是在同一時刻執行的一組動作(很像數據庫中的事務)。可以用add(),remove(),replace()等方法構成事務
,最後使用commit()方法提交事務。在調用commit()之前,可以用addToBackStack()把事務添加到一個後退棧中,這
個後退棧屬於所在的activity。有了它,就可以在用戶按下返回鍵時,返回到fragment執行事務之前的狀態。如下例:
演示瞭如何用一個fragment代替另一個fragment,同時在後退棧中保存被代替的fragment的狀態。
- //創建一個fragment
- Fragment newFragment = new ExampleFragment();
- //實例化fragment事務管理器
- FragmentTransaction transaction = getFragmentManager().beginTransaction();
- //用新創建的fragment來代替fragment_container
- transaction.replace(R.id.fragment_container,newFragment);
- //添加進棧中
- transaction.addToBackStack(null);
- //提交事務
- transaction.commit();
解釋:newFragment代替了控件ID R.id.fragment_container所指向的ViewGroup中所含的任何fragment。然後調
用addToBackStack(),此時被代替的fragment就被放入後退棧中,於是當用戶按下返回鍵時,事務發生回溯,原先的
fragment又回來了。如果向事務添加了多個動作,比如多次調用了add(),remove()等之後又調用了addToBackStack(
)方法,那麼所有的在commit()之前調用的方法都被作爲一個事務。
當用戶按返回鍵時,所有的動作都被反向執行(事務回溯)。
事務中動作的執行順序可隨意,但要注意以下幾點:
<1> 必須最後調用commit();
<2> 如果添加了多個fragment,那麼它們的現實順序跟添加順序一致(後顯示的覆蓋前面的)
<3> 如果在執行的事務中有刪除fragment的動作,而且沒有調用addToBackStack(),那麼當事務提交時,那些被刪除
的fragment就被銷燬了。反之,那些fragment就不會被銷燬,而是處於停止狀態。當用戶返回時,它們會被恢復。
<4> 但是,調用commit()後,事務並不會馬上執行。它會在activity的UI線程(其實就是主線程)中等待直到現成能執
行的時候才執行。如果必要,可以在UI線程中調用executePendingTransactions()方法來立即執行事務。但一般不需
要這樣做,除非有其它線程在等待事務的執行。
注意:只能在activity處於可保存狀態的狀態時,比如running中,onPause()方法和onStop()方法中提交事務,否則
會引發異常。這是因爲fragment的狀態會丟失。如果要在可能丟失狀態的情況下提交事務,請使用
commitAllowingStateLoss()。
1.5 Fragment與Activity通訊
儘管fragment的實現是獨立於activity的,可以被用於多個activity,但是每個activity所包含的是同一個fragment的
不同的實例。Fragment可以調用getActivity()方法很容易的得到它所在的activity的對象,然後查找activity中的控件
們(findViewById())。
有時,可能需要fragment與activity共享事件。一個好辦法是在fragment中定義一個回調接口,然後在activity中實
現之。例如,還是那個新聞程序的例子,它有一個activity,activity中含有兩個fragment。fragmentA顯示新聞標題,
fragmentB現實標題對應的內容。fragmentA必須在用戶選擇了某個標題時告訴activity,然後activity再告訴
fragmentB,fragmentB就顯示出對應的內容。
二、Fragment實例講解一
2.1 實例效果圖
點擊“存儲”按鈕顯示的界面:
點擊wifi“按鈕”顯示的界面:
2.2 項目結構
2.3 具體代碼編寫
1、左邊頁面佈局界面,frag_list.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="無線和網絡"
- android:textSize="30sp" />
- <TextView
- android:layout_width="match_parent"
- android:layout_height="1px"
- android:background="@color/lineColor" />
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical"
- android:layout_marginLeft="10dp"
- android:orientation="horizontal" >
- <TextView
- android:id="@+id/wifi"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical"
- android:text="WI-Fi"
- android:textSize="30sp" />
- <ToggleButton
- android:id="@+id/toggleButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical"
- android:layout_marginLeft="20dp"
- android:text="開" />
- </LinearLayout>
- <TextView
- android:layout_width="match_parent"
- android:layout_height="1px"
- android:background="@color/lineColor" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="設備"
- android:textSize="30sp" />
- <TextView
- android:layout_width="match_parent"
- android:layout_height="1px"
- android:background="@color/lineColor" />
- <TextView
- android:id="@+id/saveBut"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="10dp"
- android:text="存儲"
- android:textSize="35sp" />
- </LinearLayout>
2、右邊頁面佈局界面,frag_detail.xml:
- <span style="font-size:12px;"><?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@color/right"
- android:orientation="vertical" >
- <RelativeLayout
- android:id="@+id/save"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_margin="10dp"
- android:visibility="gone" >
- <include layout="@layout/save" />
- </RelativeLayout>
- <RelativeLayout
- android:id="@+id/wifi"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_margin="10dp"
- android:visibility="gone" >
- <include layout="@layout/wifi" />
- </RelativeLayout>
- </LinearLayout></span>
3、主佈局界面,main.xml:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal"
- tools:context=".AndroidFragmentActivity" >
- <!-- 主頁面 -->
- <!-- 左邊頁面 -->
- <fragment
- android:id="@+id/frag_list"
- android:name="co.cm.fragement.FragementList"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="2" />
- <!-- 右面頁面 -->
- <fragment
- android:id="@+id/frag_detail"
- android:name="co.cm.fragement.FragementDetails"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1" />
- </LinearLayout>
4、list_item.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@color/left"
- android:orientation="horizontal" >
- <ImageView
- android:id="@+id/img"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- <TextView
- android:id="@+id/txt_title"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Large Text"
- android:textAppearance="?android:attr/textAppearanceLarge" />
- </LinearLayout>
5、save.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="match_parent"
- android:layout_height="1px"
- android:background="@color/lineColor" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="10dp"
- android:text="內部存儲空間"
- android:textSize="30sp" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="5dp"
- android:layout_marginLeft="10dp"
- android:layout_marginTop="5dp"
- android:text="1GB/1.98GB"
- android:textSize="20sp" />
- <TextView
- android:layout_width="match_parent"
- android:layout_height="1px"
- android:background="@color/lineColor" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="20dp"
- android:text="總容量"
- android:textSize="30sp" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="5dp"
- android:layout_marginLeft="20dp"
- android:layout_marginTop="5dp"
- android:text="1.98GB"
- android:textSize="20sp" />
- <TextView
- android:layout_width="match_parent"
- android:layout_height="1px"
- android:background="@color/lineColor" />
- </LinearLayout>
6、wifi_list:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/wifi_name"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="qinjin_tp_2" />
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="信號強度 :" />
- <TextView
- android:id="@+id/wifi_name_state"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="還沒有連接" />
- </LinearLayout>
- </LinearLayout>
7、wifi.xml:
- <?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"
- android:orientation="vertical" >
- <LinearLayout
- android:id="@+id/wifiLinear"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical" >
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="MAC地址 :"
- android:textSize="@dimen/textsize" />
- <TextView
- android:id="@+id/mac_address"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="MAC地址 "
- android:textSize="@dimen/textsize" />
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="接入點的BSSID :"
- android:textSize="@dimen/textsize" />
- <TextView
- android:id="@+id/bssid"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="接入點的BSSID "
- android:textSize="@dimen/textsize" />
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="IP地址: "
- android:textSize="@dimen/textsize" />
- <TextView
- android:id="@+id/ip_address"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="IP地址 "
- android:textSize="@dimen/textsize" />
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="id "
- android:textSize="@dimen/textsize" />
- <TextView
- android:id="@+id/id"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="id "
- android:textSize="@dimen/textsize" />
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text=" WifiInfo的所有信息包 "
- android:textSize="@dimen/textsize" />
- <TextView
- android:id="@+id/info"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="WifiInfo的所有信息包 "
- android:textSize="@dimen/textsize" />
- </LinearLayout>
- <ListView
- android:id="@+id/listview"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_marginBottom="2dp" >
- </ListView>
- </LinearLayout>
- <TextView
- android:id="@+id/wifiText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:text="要查看可用的網絡,請打開wifi"
- android:textSize="@dimen/textsize" />
- </RelativeLayout>
8、主界面類,AndroidFragmentActivity.java:
- package co.cm.fragement;
- import co.cm.fragement.R;
- import android.app.Activity;
- import android.content.Context;
- import android.os.Bundle;
- public class AndroidFragmentActivity extends Activity {
- // 主activity
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- WifiAdmin.getWifiAdmin().setmContext(AndroidFragmentActivity.this);
- WifiAdmin.getWifiAdmin().getWifiMeathod();
- }
- }
9、左面fragment界面類,FragmentList.java:
- package co.cm.fragement;
- import co.cm.fragement.R;
- import android.app.Fragment;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.ViewGroup;
- import android.widget.CompoundButton;
- import android.widget.CompoundButton.OnCheckedChangeListener;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- import android.widget.ToggleButton;
- /**
- * @author yuyang
- * 功能描述:左面fragment界面類,該類提供了選項操作
- */
- public class FragementList extends Fragment {
- //點擊切換到wifi存儲界面
- private TextView wifi;
- //點擊切換到save存儲界面
- private TextView saveBut;
- //定義右面fragment實例
- private FragementDetails frag_detail;
- //打開關閉wifi按鈕
- private ToggleButton toggleButton;
- //toggleButton按鈕是否被點擊
- private boolean isChecked = false;
- //監聽button狀態線程標誌位
- private boolean butIsRunning = false;
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
- // 在這裏初始化fragment的頁面
- return inflater.inflate(R.layout.frag_list, container, false);
- }
- @Override
- public void onActivityCreated(Bundle savedInstanceState) {
- super.onActivityCreated(savedInstanceState);
- // 由於fragment不是activity,不是oncreated,而是onActivityCreated
- setView();
- setListener();
- startThread();// 啓動控制button的線程,當wifi狀態不是在1或者3的時候,不可點擊,
- // if (frag != null && frag.isInLayout()) {
- // switch (arg2) {
- // case 0:
- // frag.setText("0000");
- }
- /**
- * 給按鈕設置監聽
- */
- public void setListener() {
- saveBut.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- frag_detail.setSaveShow();
- }
- });
- wifi.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- frag_detail.setWifiShow();
- Log.i("111", WifiAdmin.getWifiAdmin().checkState() + "===-=-");
- checktoggleButton();// 當點回到wifi界面時,刷新button的狀態
- }
- });
- toggleButton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- Log.i("111", isChecked + "/" + WifiAdmin.getWifiAdmin().checkState());
- if (isChecked) {
- WifiAdmin.getWifiAdmin().OpenWifi();
- frag_detail.setWifiShow();
- // toggleButton.setText("關閉");
- toggleButton.setChecked(false);
- isChecked = false;
- } else {
- WifiAdmin.getWifiAdmin().CloseWife();
- frag_detail.setWifiShow();
- // toggleButton.setText("打開");
- toggleButton.setChecked(true);
- isChecked = true;
- }
- toggleButton.setClickable(false);
- }
- });
- }
- //
- public void checktoggleButton() {
- if (WifiAdmin.getWifiAdmin().checkState() == 1) {
- toggleButton.setChecked(true);
- isChecked = true;
- }
- if (WifiAdmin.getWifiAdmin().checkState() == 3) {
- toggleButton.setChecked(false);
- isChecked = false;
- }
- }
- public void setView() {
- wifi = (TextView) getView().findViewById(R.id.wifi);
- toggleButton = (ToggleButton) getView().findViewById(R.id.toggleButton);
- saveBut = (TextView) getView().findViewById(R.id.saveBut);
- // 實例化右面界面,以便操縱裏面的方法F
- frag_detail = (FragementDetails) getFragmentManager().findFragmentById(R.id.frag_detail);
- // 初始化button的裝態
- if (WifiAdmin.getWifiAdmin().checkState() == 3) {
- toggleButton.setChecked(false);
- isChecked = false;
- }
- if (WifiAdmin.getWifiAdmin().checkState() == 1) {
- toggleButton.setChecked(true);
- isChecked = true;
- }
- toggleButton.setClickable(true);
- }
- @Override
- public void onDestroy() {
- frag_detail.stopWifiThread();
- butIsRunning = false;
- super.onDestroy();
- }
- private void startThread() {
- butIsRunning = true;
- new Thread(new Runnable() {
- @Override
- public void run() {
- while (butIsRunning) {
- //只有wifi狀態改變變化完畢之後才能允許點擊按鈕
- if (WifiAdmin.getWifiAdmin().checkState() == 3) {
- if (!isChecked) {
- toggleButton.setClickable(true);
- }
- } else if (WifiAdmin.getWifiAdmin().checkState() == 1) {
- if (isChecked) {
- toggleButton.setClickable(true);
- }
- }
- }
- }
- }).start();
- }
- }
10、右面fragment界面類
- package co.cm.fragement;
- import java.util.ArrayList;
- import java.util.List;
- import co.cm.fragement.R;
- import android.app.Fragment;
- import android.net.wifi.ScanResult;
- import android.net.wifi.WifiConfiguration;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.LinearLayout;
- import android.widget.ListView;
- import android.widget.RelativeLayout;
- import android.widget.TextView;
- /**
- * @author yangyu
- * 功能描述:右面fragment界面類,該類實現了右面顯示的操作
- */
- public class FragementDetails extends Fragment {
- private TextView mac_address, bssid, ip_address, id, info, wifiText;
- private ListView listView;
- private LinearLayout wifiLinear;
- private RelativeLayout save, wifi;
- private boolean ThreadFlag = false;
- //wifi數據適配器
- private WifiAdapter wifiAdapter;
- // 掃描出的網絡連接列表
- private List<ScanResult> mWifiList = new ArrayList<ScanResult>();
- // 網絡連接列表
- private List<WifiConfiguration> mWifiConfiguration = null;
- private int nowWifiState = 0;
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
- return inflater.inflate(R.layout.frag_detail, container, false);
- }
- @Override
- public void onActivityCreated(Bundle savedInstanceState) {
- super.onActivityCreated(savedInstanceState);
- setView();
- // setListener();
- setWifiShow();
- }
- /**
- * 顯示wifi界面
- */
- public void setWifiShow() {
- //通過隱藏顯示來達到不同頁面內容的切換
- save.setVisibility(View.GONE);
- wifi.setVisibility(View.VISIBLE);
- stopWifiThread();
- refreshWifi();
- }
- /**
- * 顯示保存界面
- */
- public void setSaveShow() {
- stopWifiThread();
- save.setVisibility(View.VISIBLE);
- wifi.setVisibility(View.GONE);
- }
- /**
- * 初始化組件
- */
- public void setView() {
- // -----------------wifi-----------------
- wifiText = (TextView) getView().findViewById(R.id.wifiText);
- mac_address = (TextView) getView().findViewById(R.id.mac_address);
- bssid = (TextView) getView().findViewById(R.id.bssid);
- ip_address = (TextView) getView().findViewById(R.id.ip_address);
- id = (TextView) getView().findViewById(R.id.id);
- info = (TextView) getView().findViewById(R.id.info);
- listView = (ListView) getView().findViewById(R.id.listview);
- wifiLinear = (LinearLayout) getView().findViewById(R.id.wifiLinear);
- save = (RelativeLayout) getView().findViewById(R.id.save);
- wifi = (RelativeLayout) getView().findViewById(R.id.wifi);
- wifiAdapter = new WifiAdapter();
- listView.setAdapter(wifiAdapter);
- }
- private Handler handler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- nowWifiState = WifiAdmin.getWifiAdmin().checkState();
- // 當wifi打開時,刷新wifi列表的內容
- if (nowWifiState == 3) {
- mWifiList = WifiAdmin.getWifiAdmin().GetWifiList();
- // 如果剛開始檢測的wifi列表爲空,則創建一個實例化的wifi而不是null,負責會在adpter裏面報錯
- if (mWifiList != null) {
- // 如果wifi列表發生改變,則更新,else不更新
- if (!mWifiList.toString().equals(
- WifiAdmin.getWifiAdmin().getLastWifiList().toString())) {
- WifiAdmin.getWifiAdmin().setLastWifiList(mWifiList);
- wifiAdapter.notifyDate();
- }
- } else {
- mWifiList = new ArrayList<ScanResult>();
- }
- }
- refreshMeathod();
- super.handleMessage(msg);
- }
- };
- /**
- * 刷新wifi的狀態
- */
- public void refreshWifi() {
- new Thread(new Runnable() {
- @Override
- public void run() {
- ThreadFlag = true;
- while (ThreadFlag) {
- // Log.i("111", WifiAdmin.getWifiAdmin().checkState() +
- // "!!!");
- Message msg = handler.obtainMessage();
- handler.sendMessage(msg);
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }).start();
- }
- public void refreshMeathod() {
- // 此處可用switch
- if (nowWifiState == 3) {
- wifiLinear.setVisibility(View.VISIBLE);
- wifiText.setVisibility(View.INVISIBLE);
- mac_address.setText(WifiAdmin.getWifiAdmin().GetMacAddress() + "");
- bssid.setText(WifiAdmin.getWifiAdmin().GetBSSID() + "");
- ip_address.setText(WifiAdmin.getWifiAdmin().GetIPAddress() + "");
- id.setText(WifiAdmin.getWifiAdmin().GetNetworkId() + "");
- info.setText(WifiAdmin.getWifiAdmin().GetWifiInfo() + "");
- } else if (nowWifiState == 1) {
- wifiText.setVisibility(View.VISIBLE);
- wifiLinear.setVisibility(View.INVISIBLE);
- wifiText.setText("要查看可用的網絡,請打開wifi");
- } else if (nowWifiState == 2) {
- wifiText.setVisibility(View.VISIBLE);
- wifiLinear.setVisibility(View.INVISIBLE);
- wifiText.setText("wifi正在打開");
- } else if (nowWifiState == 4) {
- wifiText.setVisibility(View.VISIBLE);
- wifiLinear.setVisibility(View.INVISIBLE);
- wifiText.setText("wifi正在關閉");
- } else {
- wifiText.setVisibility(View.VISIBLE);
- wifiLinear.setVisibility(View.INVISIBLE);
- wifiText.setText("我不知道wifi正在做什麼");
- }
- }
- public void stopWifiThread() {
- ThreadFlag = false;
- }
- public class WifiAdapter extends BaseAdapter {
- @Override
- public int getCount() {
- return mWifiList.size();
- }
- @Override
- public Object getItem(int position) {
- return mWifiList.get(position);
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- View view = convertView;
- final ChatViewHolder vh;
- if (convertView == null) {
- vh = new ChatViewHolder();
- view = View.inflate(WifiAdmin.getWifiAdmin().getmContext(),
- R.layout.wifi_list, null);
- vh.wifi_name = (TextView) view.findViewById(R.id.wifi_name);
- vh.wifi_name_state = (TextView) view
- .findViewById(R.id.wifi_name_state);
- view.setTag(vh);
- } else {
- vh = (ChatViewHolder) view.getTag();
- }
- vh.wifi_name.setText(mWifiList.get(position).SSID.toString());// 網絡的名字,唯一區別WIFI網絡的名字
- vh.wifi_name_state.setText(mWifiList.get(position).level + "");
- return view;
- }
- public void notifyDate() {
- notifyDataSetChanged();
- }
- }
- public class ChatViewHolder {
- TextView wifi_name;// 網絡的名字,唯一區別WIFI網絡的名字
- TextView wifi_name_state;// 所發現的WIFI網絡信號強度
- }
- }
11、wifiAdmin類,提供了wifi操作的方法,WifiAdmin.java:
- package co.cm.fragement;
- import java.util.ArrayList;
- import java.util.List;
- import android.content.Context;
- import android.net.wifi.ScanResult;
- import android.net.wifi.WifiConfiguration;
- import android.net.wifi.WifiInfo;
- import android.net.wifi.WifiManager;
- import android.net.wifi.WifiManager.WifiLock;
- import android.util.Log;
- /**
- * @author yangyu
- * wifiAdmin提供了wifi操作的方法
- */
- public class WifiAdmin {
- private static WifiAdmin wifiAdmin;
- private WifiManager mWifiManager = null;
- private WifiInfo mWifiInfo = null;
- // 掃描出的網絡連接列表
- private List<ScanResult> mWifiList = new ArrayList<ScanResult>();
- // 掃描出的網絡連接列表
- private List<ScanResult> lastWifiList = new ArrayList<ScanResult>();
- // 網絡連接列表
- private List<WifiConfiguration> mWifiConfiguration = null;
- private WifiLock mWifiLock = null;
- // 上次網絡狀態
- private int lastWifiState = 0;
- //定義上下文Context
- Context mContext;
- public List<ScanResult> getLastWifiList() {
- return lastWifiList;
- }
- public void setLastWifiList(List<ScanResult> lastWifiList) {
- this.lastWifiList = lastWifiList;
- }
- public int getLastWifiState() {
- return lastWifiState;
- }
- public void setLastWifiState(int lastWifiState) {
- this.lastWifiState = lastWifiState;
- }
- public static WifiAdmin getWifi() {
- return wifiAdmin;
- }
- public Context getmContext() {
- return mContext;
- }
- public void setmContext(Context mContext) {
- this.mContext = mContext;
- }
- public static WifiAdmin getWifiAdmin() {
- if (wifiAdmin == null) {
- wifiAdmin = new WifiAdmin();
- }
- return wifiAdmin;
- }
- public void getWifiMeathod() {
- mWifiManager = (WifiManager) mContext
- .getSystemService(mContext.WIFI_SERVICE);
- mWifiInfo = mWifiManager.getConnectionInfo();
- }
- /**
- * 打開wifi
- */
- public void OpenWifi() {
- if (!mWifiManager.isWifiEnabled()) {
- mWifiManager.setWifiEnabled(true);
- } else {
- Log.i("111", "open 失敗");
- }
- }
- /**
- * 關閉wifi
- */
- public void CloseWife() {
- if (mWifiManager.isWifiEnabled()) {
- mWifiManager.setWifiEnabled(false);
- } else {
- Log.i("111", "close 失敗");
- }
- }
- /**
- * 鎖定wifi
- */
- public void lockWifi() {
- mWifiLock.acquire();
- }
- public void rlockWifi() {
- if (mWifiLock.isHeld()) {
- mWifiLock.acquire();
- }
- }
- // 檢查當前wifi狀態WIFI網卡的狀態是由一系列的整形常量來表示的。
- //1.WIFI_STATE_DISABLED : WIFI網卡不可用(1)
- //2.WIFI_STATE_DISABLING : WIFI網卡正在關閉(0)
- //3.WIFI_STATE_ENABLED : WIFI網卡可用(3)
- //4.WIFI_STATE_ENABLING : WIFI網正在打開(2) (WIFI啓動需要一段時間)
- //5.WIFI_STATE_UNKNOWN : 未知網卡狀態
- public int checkState() {
- return mWifiManager.getWifiState();
- }
- /**
- * 創建一個wifilock
- */
- public void Createwifilock() {
- mWifiLock = mWifiManager.createWifiLock("Testss");
- }
- /**
- * 得到配置好的網絡
- * @return
- */
- public List<WifiConfiguration> GetConfinguration() {
- return mWifiConfiguration;
- }
- /**
- * 連接配置好的指定ID的網絡
- * @param index
- */
- public void ConnectConfiguration(int index) {
- if (index > mWifiConfiguration.size()) {
- return;
- }
- mWifiManager.enableNetwork(mWifiConfiguration.get(index).networkId,true);
- }
- /**
- * 開始掃描網絡
- */
- public void StartScan() {
- mWifiManager.startScan();
- // 得到掃描結果
- mWifiList = mWifiManager.getScanResults();
- // 得到配置好的網絡連接
- mWifiConfiguration = mWifiManager.getConfiguredNetworks();
- }
- /**
- * 得到網絡列表
- * @return
- */
- public List<ScanResult> GetWifiList() {
- mWifiManager.startScan();
- // 得到掃描結果
- mWifiList = mWifiManager.getScanResults();
- return mWifiList;
- }
- public List<WifiConfiguration> getmWifiConfiguration() {
- return mWifiConfiguration;
- }
- /**
- * 查看掃描結果
- */
- public StringBuilder LookUpScan() {
- StringBuilder stringBuilder = new StringBuilder();
- for (int i = 0; i < mWifiList.size(); i++) {
- stringBuilder.append("Index_" + new Integer(i + 1).toString() + ":");
- // 將ScanResult信息轉換成一個字符串包
- // 其中把包括:BSSID、SSID、capabilities、frequency、level
- stringBuilder.append((mWifiList.get(i)).toString());
- stringBuilder.append("\n");
- }
- return stringBuilder;
- }
- /**
- * 得到MAC地址
- */
- public String GetMacAddress() {
- return (mWifiInfo == null) ? "NULL" : mWifiInfo.getMacAddress();
- }
- /**
- * 得到接入點的BSSID
- */
- public String GetBSSID() {
- return (mWifiInfo == null) ? "NULL" : mWifiInfo.getBSSID();
- }
- /**
- * 得到IP地址
- */
- public int GetIPAddress() {
- return (mWifiInfo == null) ? 0 : mWifiInfo.getIpAddress();
- }
- /**
- * 得到連接的ID
- */
- public int GetNetworkId() {
- return (mWifiInfo == null) ? 0 : mWifiInfo.getNetworkId();
- }
- /**
- * 得到WifiInfo的所有信息包
- */
- public String GetWifiInfo() {
- return (mWifiInfo == null) ? "NULL" : mWifiInfo.toString();
- }
- /**
- * 添加一個網絡並連接
- */
- public void AddNetwork(WifiConfiguration wcg) {
- int wcgID = mWifiManager.addNetwork(wcg);
- mWifiManager.enableNetwork(wcgID, true);
- }
- /**
- * 斷開指定ID的網絡
- */
- public void DisconnectWifi(int netId) {
- mWifiManager.disableNetwork(netId);
- mWifiManager.disconnect();
- }
- }
小結: 當我們需要在一個界面中處理很多事情的時候,可以推薦使用fragment,因爲他會把我們的activity分割成很多小塊,每個小塊都有他的生命週期,非常方便,而有時我們會用單例模式來存儲每個頁面都有的東西。
三、Fragment實例講解二
3.1 項目的效果圖
3.2 項目結構目錄
3.3 代碼具體編寫
1、標題欄的佈局界面,title_view.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="50dip"
- android:background="@drawable/title_bg"
- android:orientation="horizontal" >
- <Button
- android:id="@+id/left_btn"
- style="@style/Text.Title_Button"
- android:layout_width="wrap_content"
- android:layout_height="35dip"
- android:layout_gravity="center_vertical"
- android:background="@drawable/title_btn_back"
- android:minWidth="60dip" />
- <TextView
- android:id="@+id/title_text"
- style="@style/Text.Title"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical"
- android:layout_weight="1" />
- <Button
- android:id="@+id/right_btn"
- style="@style/Text.Title_Button"
- android:layout_width="wrap_content"
- android:layout_height="35dip"
- android:layout_gravity="center_vertical"
- android:background="@drawable/title_btn"
- android:minWidth="70dip" />
- </LinearLayout>
2、首頁的fragment頁面,這裏就列出一個,fragment_home.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <com.eoe.tampletfragment.view.TitleView
- android:id="@+id/title"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" />
- <TextView
- android:id="@+id/fragment_home_text"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/fragment_home_text"
- android:textSize="18sp" />
- </LinearLayout>
3、幫助Activity界面,activity_help.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="@drawable/activity_bg"
- android:orientation="vertical" >
- <com.eoe.tampletfragment.view.TitleView
- android:id="@+id/title"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" />
- </LinearLayout>
4、主頁面佈局,activity_main.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="@drawable/activity_bg"
- android:orientation="vertical" >
- <fragment
- android:id="@+id/fragment_home"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="1"
- class="com.eoe.tampletfragment.fragment.HomeFragment" />
- <fragment
- android:id="@+id/fragment_search"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="1"
- class="com.eoe.tampletfragment.fragment.SearchFragment" />
- <fragment
- android:id="@+id/fragment_settings"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="1"
- class="com.eoe.tampletfragment.fragment.SettingsFragment" />
- <com.eoe.tampletfragment.fragment.FragmentIndicator
- android:id="@+id/indicator"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:background="@drawable/tab_footer_bg" />
- </LinearLayout>
詳細說明:
<1> 主頁面MainActivity繼承自FragmentActivity類,負責實現導航按鈕所對應頁面的顯示和隱藏。
(詳細實現見源碼)
<2> 主頁面由底部導航欄和麪板組成。
<3> fragment標籤所對應Fragment的實現類。
<4> com.eoe.tampletfragment.fragment.FragmentIndicator標籤所對應的是底部導航欄。
5、自定義頂部工具欄,TitleView.java:
- package com.eoe.tampletfragment.view;
- import android.content.Context;
- import android.util.AttributeSet;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.widget.Button;
- import android.widget.FrameLayout;
- import android.widget.TextView;
- import com.eoe.tampletfragment.R;
- /**
- * @author yangyu
- * 功能描述:自定義頂部工具欄
- */
- public class TitleView extends FrameLayout implements View.OnClickListener {
- private Button mLeftBtn;
- private Button mRightBtn;
- private TextView mTitle;
- private OnLeftButtonClickListener mOnLeftButtonClickListener;
- private OnRightButtonClickListener mOnRightButtonClickListener;
- public interface OnLeftButtonClickListener {
- public void onClick(View button);
- }
- public interface OnRightButtonClickListener {
- public void onClick(View button);
- }
- public void setLeftButton(String text, OnLeftButtonClickListener listener) {
- mLeftBtn.setText(text);
- mLeftBtn.setVisibility(View.VISIBLE);
- mOnLeftButtonClickListener = listener;
- }
- public void setLeftButton(int stringID, OnLeftButtonClickListener listener) {
- mLeftBtn.setText(stringID);
- mLeftBtn.setVisibility(View.VISIBLE);
- mOnLeftButtonClickListener = listener;
- }
- public void removeLeftButton() {
- mLeftBtn.setText("");
- mLeftBtn.setVisibility(View.INVISIBLE);
- mOnLeftButtonClickListener = null;
- }
- public void hiddenLeftButton() {
- mLeftBtn.setVisibility(View.INVISIBLE);
- }
- public void showLeftButton() {
- mLeftBtn.setVisibility(View.VISIBLE);
- }
- public void setRightButton(String text, OnRightButtonClickListener listener) {
- mRightBtn.setText(text);
- mRightBtn.setVisibility(View.VISIBLE);
- mOnRightButtonClickListener = listener;
- }
- public void setRightButton(int stringID, OnRightButtonClickListener listener) {
- mRightBtn.setText(stringID);
- mRightBtn.setVisibility(View.VISIBLE);
- mOnRightButtonClickListener = listener;
- }
- public void removeRightButton() {
- mRightBtn.setText("");
- mRightBtn.setVisibility(View.INVISIBLE);
- mOnRightButtonClickListener = null;
- }
- public void hiddenRightButton() {
- mRightBtn.setVisibility(View.INVISIBLE);
- }
- public void showRightButton() {
- mRightBtn.setVisibility(View.VISIBLE);
- }
- public TitleView(Context context) {
- this(context, null);
- }
- public TitleView(Context context, AttributeSet attrs) {
- this(context, attrs, 0);
- }
- public TitleView(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- LayoutInflater inflater = (LayoutInflater) context
- .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- inflater.inflate(R.layout.title_view, this, true);
- mLeftBtn = (Button) findViewById(R.id.left_btn);
- mLeftBtn.setVisibility(View.INVISIBLE);
- mLeftBtn.setOnClickListener(this);
- mRightBtn = (Button) findViewById(R.id.right_btn);
- mRightBtn.setVisibility(View.INVISIBLE);
- mRightBtn.setOnClickListener(this);
- mTitle = (TextView) findViewById(R.id.title_text);
- mTitle.setVisibility(View.INVISIBLE);
- }
- public void setTitle(String text) {
- mTitle.setVisibility(View.VISIBLE);
- mTitle.setText(text);
- }
- public void setTitle(int stringID) {
- mTitle.setVisibility(View.VISIBLE);
- mTitle.setText(stringID);
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.left_btn:
- if(mOnLeftButtonClickListener != null)
- mOnLeftButtonClickListener.onClick(v);
- break;
- case R.id.right_btn:
- if(mOnRightButtonClickListener != null)
- mOnRightButtonClickListener.onClick(v);
- break;
- }
- }
- }
6、自定義底部工具欄,FragmentIndicator.java:
- package com.eoe.tampletfragment.fragment;
- import android.content.Context;
- import android.graphics.Color;
- import android.util.AttributeSet;
- import android.util.TypedValue;
- import android.view.Gravity;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- import com.eoe.tampletfragment.R;
- /**
- * @author yangyu
- * 功能描述:自定義底部工具欄
- */
- public class FragmentIndicator extends LinearLayout implements OnClickListener {
- private int mDefaultIndicator = 0;
- private static int mCurIndicator;
- private static View[] mIndicators;
- private OnIndicateListener mOnIndicateListener;
- private static final String TAG_ICON_0 = "icon_tag_0";
- private static final String TAG_ICON_1 = "icon_tag_1";
- private static final String TAG_ICON_2 = "icon_tag_2";
- private static final String TAG_TEXT_0 = "text_tag_0";
- private static final String TAG_TEXT_1 = "text_tag_1";
- private static final String TAG_TEXT_2 = "text_tag_2";
- private static final int COLOR_UNSELECT = Color.argb(100, 0xff, 0xff, 0xff);
- private static final int COLOR_SELECT = Color.WHITE;
- private FragmentIndicator(Context context) {
- super(context);
- }
- public FragmentIndicator(Context context, AttributeSet attrs) {
- super(context, attrs);
- mCurIndicator = mDefaultIndicator;
- setOrientation(LinearLayout.HORIZONTAL);
- init();
- }
- private View createIndicator(int iconResID, int stringResID, int stringColor,
- String iconTag, String textTag) {
- LinearLayout view = new LinearLayout(getContext());
- view.setOrientation(LinearLayout.VERTICAL);
- view.setLayoutParams(new LinearLayout.LayoutParams(
- LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1));
- view.setGravity(Gravity.CENTER_HORIZONTAL);
- ImageView iconView = new ImageView(getContext());
- iconView.setTag(iconTag);
- iconView.setLayoutParams(new LinearLayout.LayoutParams(
- LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1));
- iconView.setImageResource(iconResID);
- TextView textView = new TextView(getContext());
- textView.setTag(textTag);
- textView.setLayoutParams(new LinearLayout.LayoutParams(
- LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1));
- textView.setTextColor(stringColor);
- textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
- textView.setText(stringResID);
- view.addView(iconView);
- view.addView(textView);
- return view;
- }
- private void init() {
- mIndicators = new View[3];
- mIndicators[0] = createIndicator(R.drawable.ic_home_focused,
- R.string.tab_home, COLOR_SELECT, TAG_ICON_0, TAG_TEXT_0);
- mIndicators[0].setBackgroundResource(R.drawable.indic_select);
- mIndicators[0].setTag(Integer.valueOf(0));
- mIndicators[0].setOnClickListener(this);
- addView(mIndicators[0]);
- mIndicators[1] = createIndicator(R.drawable.ic_search_normal,
- R.string.tab_search, COLOR_UNSELECT, TAG_ICON_1, TAG_TEXT_1);
- mIndicators[1].setBackgroundColor(Color.alpha(0));
- mIndicators[1].setTag(Integer.valueOf(1));
- mIndicators[1].setOnClickListener(this);
- addView(mIndicators[1]);
- mIndicators[2] = createIndicator(R.drawable.ic_settings_normal,
- R.string.tab_settings, COLOR_UNSELECT, TAG_ICON_2, TAG_TEXT_2);
- mIndicators[2].setBackgroundColor(Color.alpha(0));
- mIndicators[2].setTag(Integer.valueOf(2));
- mIndicators[2].setOnClickListener(this);
- addView(mIndicators[2]);
- }
- public static void setIndicator(int which) {
- // clear previous status.
- mIndicators[mCurIndicator].setBackgroundColor(Color.alpha(0));
- ImageView prevIcon;
- TextView prevText;
- switch(mCurIndicator) {
- case 0:
- prevIcon =(ImageView) mIndicators[mCurIndicator].findViewWithTag(TAG_ICON_0);
- prevIcon.setImageResource(R.drawable.ic_home_normal);
- prevText = (TextView) mIndicators[mCurIndicator].findViewWithTag(TAG_TEXT_0);
- prevText.setTextColor(COLOR_UNSELECT);
- break;
- case 1:
- prevIcon =(ImageView) mIndicators[mCurIndicator].findViewWithTag(TAG_ICON_1);
- prevIcon.setImageResource(R.drawable.ic_search_normal);
- prevText = (TextView) mIndicators[mCurIndicator].findViewWithTag(TAG_TEXT_1);
- prevText.setTextColor(COLOR_UNSELECT);
- break;
- case 2:
- prevIcon =(ImageView) mIndicators[mCurIndicator].findViewWithTag(TAG_ICON_2);
- prevIcon.setImageResource(R.drawable.ic_settings_normal);
- prevText = (TextView) mIndicators[mCurIndicator].findViewWithTag(TAG_TEXT_2);
- prevText.setTextColor(COLOR_UNSELECT);
- break;
- }
- // update current status.
- mIndicators[which].setBackgroundResource(R.drawable.indic_select);
- ImageView currIcon;
- TextView currText;
- switch(which) {
- case 0:
- currIcon =(ImageView) mIndicators[which].findViewWithTag(TAG_ICON_0);
- currIcon.setImageResource(R.drawable.ic_home_focused);
- currText = (TextView) mIndicators[which].findViewWithTag(TAG_TEXT_0);
- currText.setTextColor(COLOR_SELECT);
- break;
- case 1:
- currIcon =(ImageView) mIndicators[which].findViewWithTag(TAG_ICON_1);
- currIcon.setImageResource(R.drawable.ic_search_focused);
- currText = (TextView) mIndicators[which].findViewWithTag(TAG_TEXT_1);
- currText.setTextColor(COLOR_SELECT);
- break;
- case 2:
- currIcon =(ImageView) mIndicators[which].findViewWithTag(TAG_ICON_2);
- currIcon.setImageResource(R.drawable.ic_settings_focused);
- currText = (TextView) mIndicators[which].findViewWithTag(TAG_TEXT_2);
- currText.setTextColor(COLOR_SELECT);
- break;
- }
- mCurIndicator = which;
- }
- public interface OnIndicateListener {
- public void onIndicate(View v, int which);
- }
- public void setOnIndicateListener(OnIndicateListener listener) {
- mOnIndicateListener = listener;
- }
- @Override
- public void onClick(View v) {
- if (mOnIndicateListener != null) {
- int tag = (Integer) v.getTag();
- switch (tag) {
- case 0:
- if (mCurIndicator != 0) {
- mOnIndicateListener.onIndicate(v, 0);
- setIndicator(0);
- }
- break;
- case 1:
- if (mCurIndicator != 1) {
- mOnIndicateListener.onIndicate(v, 1);
- setIndicator(1);
- }
- break;
- case 2:
- if (mCurIndicator != 2) {
- mOnIndicateListener.onIndicate(v, 2);
- setIndicator(2);
- }
- break;
- default:
- break;
- }
- }
- }
- }
7、首頁fragment頁面,HomeFragment.java:
- package com.eoe.tampletfragment.fragment;
- import android.content.Intent;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.support.v4.app.FragmentActivity;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.TextView;
- import com.eoe.tampletfragment.HelpActivity;
- import com.eoe.tampletfragment.R;
- import com.eoe.tampletfragment.view.TitleView;
- import com.eoe.tampletfragment.view.TitleView.OnLeftButtonClickListener;
- import com.eoe.tampletfragment.view.TitleView.OnRightButtonClickListener;
- /**
- * @author yangyu
- * 功能描述:首頁fragment頁面
- */
- public class HomeFragment extends Fragment {
- private View mParent;
- private FragmentActivity mActivity;
- private TitleView mTitle;
- private TextView mText;
- /**
- * Create a new instance of DetailsFragment, initialized to show the text at
- * 'index'.
- */
- public static HomeFragment newInstance(int index) {
- HomeFragment f = new HomeFragment();
- // Supply index input as an argument.
- Bundle args = new Bundle();
- args.putInt("index", index);
- f.setArguments(args);
- return f;
- }
- public int getShownIndex() {
- return getArguments().getInt("index", 0);
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.fragment_home, container, false);
- return view;
- }
- @Override
- public void onActivityCreated(Bundle savedInstanceState) {
- super.onActivityCreated(savedInstanceState);
- mActivity = getActivity();
- mParent = getView();
- mTitle = (TitleView) mParent.findViewById(R.id.title);
- mTitle.setTitle(R.string.title_home);
- mTitle.setLeftButton(R.string.exit, new OnLeftButtonClickListener(){
- @Override
- public void onClick(View button) {
- mActivity.finish();
- }
- });
- mTitle.setRightButton(R.string.help, new OnRightButtonClickListener() {
- @Override
- public void onClick(View button) {
- goHelpActivity();
- }
- });
- mText = (TextView) mParent.findViewById(R.id.fragment_home_text);
- }
- private void goHelpActivity() {
- Intent intent = new Intent(mActivity, HelpActivity.class);
- startActivity(intent);
- }
- @Override
- public void onHiddenChanged(boolean hidden) {
- super.onHiddenChanged(hidden);
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- }
- }
8、Activity幫助界面,HelpActivity.java:
- package com.eoe.tampletfragment;
- import android.os.Bundle;
- import android.support.v4.app.FragmentActivity;
- import android.view.Window;
- /**
- * @author yangyu
- * 功能描述:幫助Activity界面
- */
- public class HelpActivity extends FragmentActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.activity_help);
- }
- }
9、Activity主界面,MainActivity.java:
- package com.eoe.tampletfragment;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.support.v4.app.FragmentActivity;
- import android.view.View;
- import android.view.Window;
- import com.eoe.tampletfragment.fragment.FragmentIndicator;
- import com.eoe.tampletfragment.fragment.FragmentIndicator.OnIndicateListener;
- /**
- * @author yangyu
- * 功能描述:主Activity類,繼承自FragmentActivity
- */
- public class MainActivity extends FragmentActivity {
- public static Fragment[] mFragments;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.activity_main);
- setFragmentIndicator(0);
- }
- /**
- * 初始化fragment
- */
- private void setFragmentIndicator(int whichIsDefault) {
- mFragments = new Fragment[3];
- mFragments[0] = getSupportFragmentManager().findFragmentById(R.id.fragment_home);
- mFragments[1] = getSupportFragmentManager().findFragmentById(R.id.fragment_search);
- mFragments[2] = getSupportFragmentManager().findFragmentById(R.id.fragment_settings);
- getSupportFragmentManager().beginTransaction().hide(mFragments[0])
- .hide(mFragments[1]).hide(mFragments[2]).show(mFragments[whichIsDefault]).commit();
- FragmentIndicator mIndicator = (FragmentIndicator) findViewById(R.id.indicator);
- FragmentIndicator.setIndicator(whichIsDefault);
- mIndicator.setOnIndicateListener(new OnIndicateListener() {
- @Override
- public void onIndicate(View v, int which) {
- getSupportFragmentManager().beginTransaction()
- .hide(mFragments[0]).hide(mFragments[1])
- .hide(mFragments[2]).show(mFragments[which]).commit();
- }
- });
- }
- @Override
- protected void onResume() {
- super.onResume();
- }
- @Override
- protected void onPause() {
- super.onPause();
- }
- }