1. 程式人生 > >Android-Fragment(基本知識,靜態載入,動態載入)

Android-Fragment(基本知識,靜態載入,動態載入)

1.回顧

   上篇瞭解了和學習了 Andorid 的webView 的使用

2.重點

   (1)Fragment基本知識

   (2)Fragment 靜態載入

   (3)Fragment 動態載入

   (4)Fragment的生命週期

   (5)Fragment與Activity 之間相互通訊(傳值)

3.Fragment基本知識

  (1)可以作為Activity一部分
  (2)可以在Activity 出現多個Fragment
  (3)可以在多個Activity 中出現一個Fragment
 
 (4)Fragment在Activity 執行過程中,可以新增,移除和替換
 (5)Fragment可以響應自己的輸入事件
 (6)Fragment的獨立的生命週期,受宿主Acitivity的生命週期影響

 (7)獨立的元件控制

 (8)獨立的使用者輸入響應

4.Fragment靜態 載入

   4.1 新建 layout 佈局檔案 fragmen.xml

        包括一個按鈕和一個 textview

<?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" />

    <Button
        android:id="@+id/btn_frag"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="改變" />

</LinearLayout>

    4.2 新建 MyFragment

          繼承自 Fragment  ,實現 onCreateView() 方法,其中的 View物件 中需要的佈局檔案為 ,上面新建的fragment.xml 佈局檔案

package com.example.fragment;

import com.example.studydemo.R;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;



public class MyFragment extends Fragment {

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// layout佈局檔案 轉成view 物件
		/**
		 * resource 佈局檔案
		 * root 載入layout 的父 ViewGroup
		 * attachToRoot :false 不反悔
		 */
		View view=inflater.inflate(R.layout.fragment, container, false);
		TextView textView1=(TextView) view.findViewById(R.id.textView1);
		textView1.setText("靜態載入Fragment");
		return view;
	}
}


   4.3 新建Activity 

          佈局檔案中新新增 fragment 標籤,引入剛剛新建的 MyFragment 實現類 ; 一定要給 fragment 新增id

  <fragment
       android:id="@+id/fragment_2"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" 
       android:name="com.example.fragment.MyFragment"
       >
   </fragment>

   4.4 測試 

        在 新建的Activity 類中 ,例項話 fragment中的 控制元件 按鈕和 textview ;

         btn_frag=(Button) findViewById(R.id.btn_frag);
		tv1=(TextView) findViewById(R.id.textView1);
		btn_frag.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//點選事件
				tv1.setText("我是Fragment2Activity");
			}
		});

    4.5 執行測試

5. Fragment 動態載入

   5.1 新建 Activity 

    在佈局檔案中 新增一個 按鈕和 一個 佈局檔案

 <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@+id/button3"
        android:text="動態載入Fragment" />

    <LinearLayout
        android:id="@+id/fragme"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="42dp"
        android:orientation="vertical" >
    </LinearLayout>

   5.2  事件

    初始化按鈕,和在按鈕點選事件裡新增,動態載入程式碼:

  例如:

     (1)MyFragment 和靜態載入一樣 實現 Fragment ,

     (2)transaction.add()方法中 的 R.id.fragme 是 上面 lineatLayout 的 id

     (3)最後一定 commit

                  MyFragment2 fragment2=new MyFragment2();
                  FragmentManager fragmentManager= getFragmentManager();
		  FragmentTransaction transaction=fragmentManager.beginTransaction();
		  transaction.add(R.id.fragme, fragment2);
		  
		  //回退
		  transaction.addToBackStack(null);
		  
		  transaction.commit();

6.總結

    動態載入的時候,FragmentTransaction 有 多個操作 Fragment操作方法,比如 add() , replace() ,remove()等

 下面將學習 Fragment的 生命週期和 與 Activity 相互通訊(傳參)