因爲TabActivity在Android4.0之後已經被徹底棄用,那麼我就再也不浪費口水繼續講解它了,取而代之的是Fragment。Fragment是Android3.0新增的概念,Fragment翻譯成中文是碎片的意思,不過卻和Activity十分的類似,這一篇我花大量的篇幅來詳細的講解Fragment的介紹和使用方法。php
1、Fragment的基礎知識介紹java
1.1概述android
1.1.1 特性數據庫
Fragment是activity的界面中的一部分或一種行爲。能夠把多個Fragment組合到一個activity中來建立一個多界面網絡
而且能夠在多個activity中重用一個Fragment。能夠把Fragment任務模塊化的一段activity,它具備本身的生命週期,app
接收它本身的事件,並能夠在activity運行時被添加或刪除。ide
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。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()。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()以前調用的方法都被做爲一個事務。當用戶按返回鍵時,全部的動做都被反向執行(事務回溯)。<?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>
複製代碼
二、右邊頁面佈局界面,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>
複製代碼
三、主佈局界面,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>
複製代碼
四、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>
複製代碼
五、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>
複製代碼
六、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>
複製代碼
七、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>
複製代碼
八、主界面類,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();
}
}
複製代碼
九、左面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();
}
}
複製代碼
十、右面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網絡信號強度
}
}
複製代碼
十一、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分割成不少小塊,每一個小塊都有他的生命週期,很是方便,而有時咱們會用單例模式來存儲每一個頁面都有的東西。<?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>
複製代碼
二、首頁的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>
複製代碼
二、首頁的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>
複製代碼
三、幫助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>
複製代碼
四、主頁面佈局,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類,負責實現導航按鈕所對應頁面的顯示和隱藏。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;
}
}
}
複製代碼
六、自定義底部工具欄,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;
}
}
}
}
複製代碼
七、首頁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();
}
}
複製代碼
八、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);
}
}
複製代碼
九、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();
}
}
複製代碼
MyFragment01.zip (153.64 KB, 下載次數: 772)