Android4.0新特性----Fragment與ActionBar

Android的1.x和2.x適用於手機,3.x適用於平板,而從4.0開始整合了手機與平板的系統,讓4.x的系統適用於手機與平板。
java

一:Fragment
android

Fragment是Android的一個重要的特性,從字面意思理解爲「碎片」,實質上就是可讓Activity有多個顯示區域來顯示內容,他有本身的生命週期,可是他的生命週期依賴於他所附屬的Activity的生命週期。
app

1:生命週期
ide

132638229.png

經過上面的圖片咱們可總結出以下的幾條規律:
佈局

(1)Activity的生命週期將直接影響Fragment的生命週期
this

(2)Fragment實質上是將Activity劃分紅了幾個部分
spa

(3)Fragment能夠處於Activity狀態與之對應的狀態及如下狀態。如:Activity處於stopped狀態時,Fragment能夠處於onStop、onDestoryView及如下狀態。
設計

2:管理與操做Fragment
xml

(1)管理Fragment
對象

經過getFragmentManager得到管理對象,主要方法有

findFragmentById(int id):經過id查找到Fragment

findFragmentByTag(String tag):經過Tag找到Fragment

(2)操做Fragment

經過FragmentManager對象的getTranscation得到操做對象

add(int containerViewID,Fragment fragment,String tag):向Activity中添加一個Fragment對象

show(Fragment fragment):顯示已經隱藏的Fragment

remove(Fragment fragment):移除已經存在的Fragment

hide(Fragment fragment):隱藏顯示的Fragment

commit():註冊這個Transcation

二:ActionBar

在2.x版本中的實現導航樣式的TabHost,在4.x的版本中對齊又進行了美化。原來用2.x開發的導航能夠運行在4.x版本的設備上,可是4.x開發的導航卻沒法運行在2.x版本的設備上。

用途之一:作導航標籤

使用ActionBar設計的導航能夠自適應屏幕的分辨率,能夠避免適配的問題。具體實現主要由如下幾個步驟

(1)實現ActionBar.TabListener接口,重寫其中的幾個方法以響應用戶切換導航標籤的操做。也就是說,一個標籤註冊一個監聽,添加一個Fragment到Activity上,完成一個操做。

(2)實例化ActionBar.Tab對象,一個對象就是一個標籤,能夠調用set系列方法設置標籤的樣式及操做等

(3)調用addTab()將標籤添加到導航ActionBar中。

三:結合兩大特性製做導航

這裏想達到的效果是:在一個Activity上添加一個導航,隨着單擊導航標籤,在同一個Activity上的不一樣區域上切換顯示內容。

1:界面佈局

主界面就是一個LinearLayout,且給這個佈局一個id;另外的2個界面,隨意添加什麼控件都可以(這裏我添加的一個是TextVIew一個是ImageView)。

主界面佈局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity" >
</LinearLayout>

佈局1:textfragment.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:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
</LinearLayout>

佈局2:p_w_picpathfragment.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" >
    <ImageView
        android:id="@+id/p_w_picpathView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />
</LinearLayout>

2:Fragment類

有多少個轉換的界面就有多少個繼承自Fragment的類,重寫onCreateView方法,加載對應界面要顯示的xml文件,得到界面上的視圖View

區域1:TextFragment.java

//繼承自Fragment
public class TextFragment extends Fragment{
    private View view;//聲明視圖
                                                                                
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        //加載對應界面的佈局,得到視圖View
        view = inflater.inflate(R.layout.textfragment, container,false);
        //返回View
        return view;
    }
}

區域2:ImageFragment.java

//繼承自Fragment
public class ImageFragment extends Fragment{
//聲明視圖
    private View p_w_picpathview;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
//加載佈局,返回視圖
        p_w_picpathview = inflater.inflate(R.layout.p_w_picpathfragment, container, false);
                                                                                  
        return p_w_picpathview;
    }
}

3:主界面

建立導航,導航標籤對象,設置導航標籤的監聽以響應操做等,添加標籤到導航上。

public class MainActivity extends Activity {
    // 聲明導航對象
    private ActionBar actionBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 加載視圖
        setContentView(R.layout.activity_main);
        // 實例化導航對象
        actionBar = getActionBar();
        // 設置導航的顯示樣式
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        // 建立第一個區域
        TextFragment textFragment = new TextFragment();
        // 實例化第一個標籤對象
        Tab tab1 = actionBar.newTab();
        tab1.setIcon(R.drawable.ic_launcher);// 設置標籤的圖標樣式
        tab1.setText("選項卡1");// 設置圖標顯示的文字
        tab1.setTabListener(new TextListener(textFragment));// 給標籤添加相應的監聽
        actionBar.addTab(tab1);// 添加標籤到導航上
        // 建立第二個區域
        ImageFragment p_w_picpathFragment = new ImageFragment();
        // 建立第二個標籤
        Tab tab2 = actionBar.newTab();
        tab2.setIcon(R.drawable.ic_launcher);// 設置標籤的圖標樣式
        tab2.setText("選項卡2");// 設置標籤的文字
        tab2.setTabListener(new IamgeListener(p_w_picpathFragment));// 給標籤添加相應的監聽
        actionBar.addTab(tab2);// 添加標籤到導航上
    }
    // 內部類形式        實現監聽第一個區域的類
    class TextListener implements android.app.ActionBar.TabListener {
        private TextFragment textFragment;// 聲明區域
        // 構造方法
        public TextListener(TextFragment textFragment) {
            this.textFragment = textFragment;
        }
        // 重寫的三個方法
        // 當標籤從新選中時觸發的方法
        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
        }
        // 當標籤選中時觸發的方法
        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            ft.add(R.id.layout, textFragment, null);// 添加區域到Activity上的佈局上
        }
        // 當標籤取消選中時觸發的方法
        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            ft.remove(textFragment);// 移除區域
        }
    }
    // 內部類形式        實現監聽第二個區域的類
    class IamgeListener implements android.app.ActionBar.TabListener {
        private ImageFragment p_w_picpathFragment;// 聲明區域
        // 構造方法
        public IamgeListener(ImageFragment p_w_picpathFragment) {
            this.p_w_picpathFragment = p_w_picpathFragment;
        }
        // 當標籤從新選中時觸發的方法
        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
        }
        // 當標籤選中時觸發的方法
        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            ft.add(R.id.layout, p_w_picpathFragment, null);// 添加區域到Activity上的佈局上
        }
        // 當標籤取消選中時觸發的方法
        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            ft.remove(p_w_picpathFragment);// 移除區域
        }
    }
}

4:結果

和2.x版本中的導航相似,單擊不一樣的標籤時,下方會顯示出不一樣的界面。


快要崩潰。。。j_0046.gif

相關文章
相關標籤/搜索