開發(fā)環(huán)境:Android 2.x
1 創(chuàng)建平板模擬器
在項目中點擊 “shift + F9”,這時會彈出模擬器選擇框,點擊 Create New Virtual Device:
選擇 Tablet澈吨,盡量選大屏的模擬器喲身冀,因為這樣看的清楚:
一路 next放椰,最后點擊運行我們剛創(chuàng)建的平板模擬器:
2 基本用法
我們在一個活動當(dāng)中添加兩個碎片匈辱,并讓這兩個碎片平分這個活動空間。
首先新建一個左側(cè)碎片布局:
<?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">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="按鈕"></Button>
</LinearLayout>
這個布局非常簡單球涛,只放置了一個按鈕劣针,并讓它水平居中顯示。接著新建右側(cè)碎片布局:
<?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="#00a000"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="我是右邊的碎片"
android:textSize="25sp"
android:textColor="#ffffff"
/>
</LinearLayout>
這個布局把背景色設(shè)置為深綠色亿扁,并放置了一個 TextView 用于顯示一段文本捺典。
接著新建一個繼承自 Fragment 的類。注意从祝,這里會有兩個不同包下的 Fragment 類供選擇襟己,一個是系統(tǒng)內(nèi)置的 android.app.Fragment引谜,一個是 support-v4 庫中的 android.support.v4.app.Fragment
。強烈建議使用 support-v4 庫中的 Fragment擎浴,因為它可以讓碎片在所有 Android 系統(tǒng)版本中保持功能一致喲员咽!因為 appcomcat-v7 庫會把 support-v4 庫一起引入進來,所以我們不必在 gradle 中引入這個庫贮预。
LeftFragment :
public class LeftFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.left_fragment, container, false);
}
}
這里僅僅是重寫了 Fragment 的 onCreateView() 方法贝室,然后在這個方法中通過 LayoutInflater 的 inflate()方法將剛剛定義的 fragment_left 布局動態(tài)加載進來而已。接著我們用同樣的方法創(chuàng)建來 RightFragment 類:
public class RightFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.right_fragment, container, false);
}
}
修改主活動類的布局文件:
<?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="horizontal">
<fragment
android:id="@+id/left_fragment"
android:name="net.deniro.android.fragmenttest.LeftFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
/>
<fragment
android:id="@+id/right_fragment"
android:name="net.deniro.android.fragmenttest.RightFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
/>
</LinearLayout>
這里使用了 <fragment>
標(biāo)簽用于在布局中添加碎片萌狂,通過 android:name
屬性來顯式指明要添加的碎片類名档玻,注意一 定要是類的全名哦O(∩_∩)O~
2 動態(tài)添加碎片
碎片真正的強大之處在于怀泊,它可以在程序運行時動態(tài)地被添加到活動當(dāng)中茫藏。根據(jù)具體情況來動態(tài)地添加碎片,就可以實現(xiàn)程序界面的定制功能霹琼。
我們新建一個布局文件:
<?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"
android:background="#ffff00"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="20sp"
android:text="我是另一個右碎片"
/>
</LinearLayout>
這個碎片只是把背景色改為黃色务傲。然后再新建一個 Fragment:
public class AnotherRightFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.another_right_fragment, container, false);
}
}
在主界面中加入一個 FrameLayout:
<?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="horizontal">
<fragment
android:id="@+id/left_fragment"
android:name="net.deniro.android.fragmenttest.LeftFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
/>
<FrameLayout
android:id="@+id/right_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
></FrameLayout>
</LinearLayout>
我們打算只放一個碎片,因為 FrameLayout 布局中的所有控件都會默認(rèn)擺放在布局的左上角枣申,所以這里很適合使用 FrameLayout 布局售葡。
最后修改主活動中的代碼:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
replace(new AnotherRightFragment());
}
});
}
private void replace(Fragment fragment) {
FragmentManager manager=getSupportFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
transaction.replace(R.id.right_layout,fragment);
transaction.commit();
}
}
我們給左側(cè)碎片中的按鈕注冊了一個點擊事件,事件內(nèi)部會調(diào)用 replace() 方法動態(tài)添加碎片忠藤。動態(tài)添加碎片分為 5 步:
- 創(chuàng)建待添加的碎片實例挟伙。
- 獲取 FragmentManager,在活動中可以直接調(diào)用 getSupportFragmentManager()方法獲取模孩。
- 開啟一個事務(wù)尖阔。
- 向容器內(nèi)添加或替換碎片,一般使用 replace() 方法實現(xiàn)榨咐,需要傳入容器的 id 和待添加或替換的碎片實例介却。
- 提交事務(wù)。
3 在碎片中模擬棧
上面块茁,我們實現(xiàn)了向活動中動態(tài)添加碎片的功能齿坷,但通過點擊按鈕添加了一個碎片之后,按下 Back 鍵程序就會直接退出数焊。如何才能夠模擬 “返回椨捞剩” 的效果,讓這個示例點擊兩次 Back 鍵才會退出程序呢佩耳?
FragmentTransaction 中有一個 addToBackStack() 方法遂蛀,可以將一個事務(wù)添加到棧中,我們修改 Activity 中的代碼:
private void replace(Fragment fragment) {
FragmentManager manager=getSupportFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
transaction.replace(R.id.right_layout,fragment);
transaction.addToBackStack(null);
transaction.commit();
}
4 碎片和活動之間的通信
為了方便碎片和活動之間進行通信蚕愤,F(xiàn)ragmentManager 提供了一個類似于 findViewById() 的方法答恶,專用于從布局文件中獲取碎片的實例:
//從布局文件中獲取碎片實例
RightFragment rightFragment = (RightFragment) getSupportFragmentManager().findFragmentById(R.id.right_layout);
** 注意:要使用 getSupportFragmentManager() 來獲取碎片管理器哦**
從碎片中也可以直接返回相關(guān)的活動:
//從碎片中調(diào)用活動
Activity activity = rightFragment.getActivity();
另外饺蚊,如果需要在碎片中獲取 Context 對象時,也可以直接調(diào)用 getActivity()悬嗓,因為 Activity 本身就是 Context 對象啦O(∩_∩)O哈哈~
從活動中也可以再調(diào)用碎片實例:
//從活動中再調(diào)用碎片實例
+ ((AppCompatActivity) activity).getSupportFragmentManager().findFragmentById(R.id.right_layout);
這里記得向下轉(zhuǎn)型為 AppCompatActivity 哦污呼。
有了上面的方法,我們就能讓碎片與碎片之間通信啦:
- 從 A 碎片中得到相關(guān)聯(lián)的活動包竹。
- 通過這個活動獲取 B 碎片的實例燕酷。
A 碎片 → 關(guān)聯(lián)活動 → B 碎片
是不是很清楚呀O(∩_∩)O~