溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android中碎片的使用方法詳解

發(fā)布時間:2020-10-11 09:29:57 來源:腳本之家 閱讀:230 作者:Kepler 欄目:移動開發(fā)

Fragment的使用

其實碎片很簡單,但是網(wǎng)上胡亂充數(shù)的博文太多了,以至于我們有時候覺得比較亂,今天就來簡單講解一下碎片的使用.
碎片的使用分為兩種,靜態(tài)添加碎片和動態(tài)添加碎片,我們就先來看一下靜態(tài)添加碎片如何實現(xiàn).

靜態(tài)添加碎片

首先,先建兩個Layout文件,這就是碎片的布局文件,大家可能也發(fā)現(xiàn)了,Android Studio里面可以直接快速建立碎片,就像Activity一樣,但是這樣會生成很多沒用的代碼,所以我們還是選擇自己創(chuàng)建碎片布局.

兩個布局都很簡單,里面只有一個居中顯示的TextView,下面貼一下代碼.

第一個布局文件:fragment_first.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="@android:color/holo_blue_light"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="This is the first fragment!"/>
</LinearLayout>

第二個布局文件fragment_second.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="@android:color/holo_green_light"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="This is the second fragment!"/>
</LinearLayout>

現(xiàn)在布局文件建完了,我們該建立他們對應(yīng)的Fragment了,也就是后臺代碼了。新建兩個類,分別叫FirstFragment和SecondFragment,都繼承于Fragment,需要注意一點,我們教程里面所使用的Fragment全都是android.support.v4.app.Fragment這個包下的,這樣更有利于程序的兼容性.

貼一下兩個類的代碼,也很簡單,只是重寫了onCreateView方法來加載不同的布局文件.

public class FirstFragment extends Fragment {
private View view;//得到碎片對應(yīng)的布局文件,方便后續(xù)使用
//記住一定要重寫onCreateView方法
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_first, container, false);//得到對應(yīng)的布局文件
return view;
}
}
public class SecondFragment extends Fragment {
private View view;//得到碎片對應(yīng)的布局文件,方便后續(xù)使用
//記住一定要重寫onCreateView方法
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_second, container, false);//得到對應(yīng)的布局文件
return view;
}
}

好,基本的工作我們做完了,現(xiàn)在我們用兩個Activity展示如何靜態(tài)添加碎片和動態(tài)添加碎片.

靜態(tài)添加控件的話,需要使用fragment控件,指定其名稱是你剛才創(chuàng)建的Fragment就可以,讓我們來看一下.

先貼一下第一個Activity的布局:

<?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/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="me.worldskills.android.testfragment.MainActivity">
<fragment
android:id="@+id/main_firstfragment"
android:name="me.worldskills.android.testfragment.FirstFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></fragment>
<fragment
android:id="@+id/main_secondfragment"
android:name="me.worldskills.android.testfragment.SecondFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></fragment>
<Button
android:id="@+id/main_btnGoNext"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Go to next page"
android:textAllCaps="false"/>
</LinearLayout>


其中那個按鈕是用來跳轉(zhuǎn)到下一個界面的,也就是動態(tài)添加碎片案例的Activity,在這里可以忽略.

這里我們看見了,兩個fragment分別指定name為FirstFragment和SecondFragment,也就是你剛才創(chuàng)建的兩個Fragment,一定要記得加上包名.對了,還有一個問題,就是這樣的話是沒有預(yù)覽的,如果想要預(yù)覽,需要在fragment標(biāo)簽中加上一句代碼:

Tools:layout="@layout/布局文件名稱"  .

好了,靜態(tài)添加碎片就完成了,什么?就這么簡單,對啊...就這么簡單.

動態(tài)添加碎片

動態(tài)添加碎片我們就不需要用fragment控件了,而是需要用個FrameLayout控件,這是為什么呢,首先我們都知道FrameLayout中的控件,都是從左上角開始顯示,不用進行位置控制,動態(tài)添加碎片其實就是向容器里面動態(tài)添加碎片,而fragment控件只能用來靜態(tài)綁定一個碎片.

先貼一下第二個Activity的布局:

<?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/activity_second"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/second_btnLoadFirst"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Load First!"
android:textAllCaps="false"/>
<Button
android:id="@+id/second_btnLoadSecond"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Load Second!"
android:textAllCaps="false"/>
<FrameLayout
android:id="@+id/second_fl"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>

上面的兩個按鈕用來加載不同的碎片,而下面的FrameLayout就是碎片顯示的容器.

廢話不多說,貼代碼:

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);

//點擊第一個按鈕的時候加載第一個碎片
findViewById(R.id.second_btnLoadFirst).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.second_fl, new FirstFragment());
transaction.commit();
}
});
//點擊第二個按鈕的時候加載第二個碎片
findViewById(R.id.second_btnLoadSecond).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getSupportFragmentManager().beginTransaction().replace(R.id.second_fl, new SecondFragment()).commit();//簡寫
}
});
}
}

getSupportFragmentManager方法用來獲得一個碎片管理器對象(使用這個方法的時候注意是android.support.v4.app包下的哦),然后通過這個方法開始一個碎片事物對象,這個對象比較關(guān)鍵,可以用來動態(tài)添加碎片,調(diào)用它的replace方法,會把指定容器里面的其他控件全部清除掉,然后添加新的碎片進去.在這里就是先把R.id.second_f1里面的控件清空,然后添加傳入一個FirstFragment進去.

替換完之后一定要記得調(diào)用commit方法提交,要不然你的所有操作都不會生效,切記.

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI