Fragment(碎片)是一種可以嵌入在活動當中的UI片段,它可以讓程序更加合理和充分的利用大屏幕的空間。碎片和活動太像了,同樣都包含布局,都有自己的聲明周期,可以將碎片理解為一種迷你型的活動。
新建FragmentTest項目。假設項目已經建立完成。
新建一個左側布局left_fragment.xml,代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="match_parent"android:layout_height="match_parent"><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content" android:layout_gravity="center_horizontal"android:text="Button"/>
</LinearLayout>
只是放置了一個按鈕,并讓它水平居中顯示,然后:
新建右側碎片布局right_fragment.xml,代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:background="#00ff00"android:layout_height="match_parent"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:textSize="20sp"android:text="This is right fragment"/>
</LinearLayout>
我們只是將這個布局的背景設置成了綠色,并放置了一個TextView用于顯示一段文本。
接著新建一個LeftFragment類,這個類繼承自Fragment,這里有兩個不同包下的Fragment供你選擇,我們要選擇support-v4庫中的Fragment,因為它可以讓碎片在所有Android 版本中保持功能的一致性。另外,我們不需要在build.gradle文件中添加support-v4庫的依賴,因為build.gradle文件中已經添加了appcompat-v7庫的依賴,而這個庫,會將support-v4庫也一并引入進來。
現在編寫LeftFragment中的代碼,如下所示:
package com.example.fragmenttest;import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;/*** Created by ZHJ on 2018/3/6.*/public class LeftFragment extends Fragment {public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){View view = inflater.inflate(R.layout.left_fragment,container,false);return view ;}
}
這里僅僅重寫了Fragment的onCreata()方法,然后在這個方法中通過LayoutInflater的inflate()方法將剛才定義的left_fragment布局動態加載進來,整個方法就簡單明了。
我們再新建一個RightFragment,代碼如下:
package com.example.fragmenttest;import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;/*** Created by ZHJ on 2018/3/6.*/public class RightFragment extends Fragment {public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){View view = inflater.inflate(R.layout.right_fragment,container,false);return view ;}}
基本山代碼都是相同的,接下里修改acitivity_main.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"><fragmentandroid:id="@+id/left_fragment"android:name ="com.example.fragmenttest.LeftFragment"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"/><fragmentandroid:id="@+id/right_fragment"android:name ="com.example.fragmenttest.RightFragment"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"/></LinearLayout>
我們使用了<fragment>標簽在布局中添加碎片,其中指定的大多數屬性都是比較熟悉的,只不過通過android:name屬性來顯式指明要添加地碎片類名,注意一定要將類的包名也加上。
我們可以運行一下程序:
兩個碎片平分了整個活動地布局。
2、動態添加碎片
碎片地真正地強大之處在于,它可以在程序運行時動態地添加到活動當中,根據具體情況來添加碎片,你就可以將程序界面定制的更加多樣化。
我們接著上一節的內容,新建another_right_fragment.xml,代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="match_parent"android:background="#ffff00"android:layout_height="match_parent"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content" android:layout_gravity="center_horizontal"android:textSize="20sp"android:text="This is another right fragment"/></LinearLayout>
這個布局文件和right_fragment.xml中的代碼基本相同,只是將背景色變成了黃色。
新建AnotherRightFragment作為另一個右側碎片,代碼如下:
package com.example.fragmenttest;import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;/*** Created by ZHJ on 2018/3/6.*/public class AnotherRightFragment extends Fragment {public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){View view = inflater.inflate(R.layout.another_right_fragment,container,false);return view ;}
}
代碼同樣簡單。在onCreateView()方法中加載了剛剛創建的another_right_fragment布局。這樣我們就準備好了另一個碎片,接下來,看一下如何動態將它添加到活動當中,修改activity_main.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"><fragmentandroid:id="@+id/left_fragment"android:name ="com.example.fragmenttest.LeftFragment"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"/><FrameLayoutandroid:id="@+id/right_layout"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"></FrameLayout></LinearLayout>
現在將右側碎片替換成一個Fragment中,Fragment是Android中最簡單的一種布局,所有控件默認都會擺放在布局的左上角。
由于這里僅僅需要在布局里放入一個碎片,不需要任何定位,非常適合使用FragmentLayout。
我們在代碼中向Fragment中添加內容,從而實現動態添加碎片的功能。修改MainActivity中的代碼,如下:
package com.example.fragmenttest;import android.app.Fragment;
import android.app.FragmentTransaction;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;public class MainActivity extends AppCompatActivity implements View.OnClickListener {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main); Button button =(Button)findViewById(R.id.button);button.setOnClickListener(this);replaceFragment(new RightFragment());} @Overridepublic void onClick(View view) {switch (view.getId()){case R.id.button:replaceFragment(new AnotherRightFragment());break;default:break;}}private void replaceFragment(android.support.v4.app.Fragment fragment){FragmentManager fragmentManager = getSupportFragmentManager();android.support.v4.app.FragmentTransaction tranction = fragmentManager.beginTransaction();tranction.replace(R.id.right_layout,fragment);tranction.commit();}
}
首先我們給左側碎片中的按鈕注冊了一個點擊事件,然后調用replaceFragment()方法動態的添加Fragment這個碎片。當點擊按鈕時,又會調用replaceFragment()方法將右側碎片替換成AnotherRightFragment.結合replaceFramment()方法中的代碼可以看出,動態添加碎片主要分為5步。
1、創建待添加的碎片實例
2、創建FragmentManager,在活動中可以直接通過調用getSupportFragmentManager()方法得到。
3、開啟一個事務,通過調用beginTransaction方法開啟。
4、向容器內添加或替換碎片,一般使用replace()方法實現,需要傳入容器的id和待添加的碎片實例。
5、提交事務,調用commit()方法來完成。
運行一下程序:點擊按鈕可以看到效果: