碎片(Fragment)是一種可以嵌入在活動當中的UI片段,可以理解為,碎片就是一個活動,我們可以把這幾個活動進行拼接,就好似碎片拼接在一起,所以這種UI片段被稱之為碎片
碎片加載方式有兩種,分為:1、靜態碎片 2、動態碎片
1、靜態加載碎片
我們在layout里面建立兩個新的活動,分別命名為left_fragment和right_fragment,這樣就相當于建立了兩個碎片的布局了
left_fragment:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#00ff00"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Button">
</Button>
</LinearLayout>
right_fragment:
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="20sp"
android:text="This is right fragment">
</TextView>
</LinearLayout>
然后分別建兩個類LeftFragment和RightFragment來繼承Fragment
LeftFragment:
public class LeftFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.left_fragment,container,false);
return view;
}
}
RightFragment:
public class RightFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.right_fragment,container,false);
return view;
}
}
在方法里要重寫onCreateView()方法,然后在這個方法里通過Inflate()方法將剛才定義的left_fragment和right_fragment布局加載進來
這樣兩個靜態碎片都建立完畢了,只需要在主布局activity_main.xml里加載出來就行了
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity">
<fragment
android:id="@+id/left_fragment"
android:name="com.example.fragment.LeftFragment"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent">
</fragment>
<fragment
android:id="@+id/right_fragment"
android:name="com.example.fragment.RightFragment"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent">
</fragment>
</LinearLayout>
給碎片的name填寫上剛剛建立的兩個碎片的類名即可
注意:一定要給fragment定義id,不然會閃退,這里是個坑
運行起來就能看到效果了

2、動態加載碎片
來繼續動態碎片的加載
我們刪掉右側的碎片,這樣就只剩下左側的碎片了,然后用動態加載來加載右側的碎片,這樣可觀性很強
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity">
<fragment
android:id="@+id/left_fragment"
android:name="com.example.fragment.LeftFragment"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent">
</fragment>
<FrameLayout
android:id="@+id/right_layout"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
FrameLayout是幀布局,所有的控制元件默認會擺放在布局的左上角,所以我們在幀布局里放一個碎片,碎片位置就被定好了
然后修改MainActivity中的代碼
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fragmentManager=getSupportFragmentManager();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
//在幀布局里加入右側碎片
fragmentTransaction.replace(R.id.right_layout,new RightFragment());
fragmentTransaction.commit();
}
});
}
}
我們先給左側碎片里的按鈕注冊了點擊事件,然后對碎片進行了動態添加,動態添加分為以下步驟
(1)創建待添加的碎片實體
(1)獲取FragmentManager,在活動中可以直接呼叫getSupportFragmentManager()方法得到
(2)開啟一個事務,通過呼叫beginTransaction()方法開啟
(3)向容器中添加/替換碎片,一般用replace()方法實作,需要傳入容器的id和待添加的碎片實體
(4)提交事務,呼叫commit()方法來完成
3、在碎片中模擬回傳堆疊
我們只需要在動態加載布局的時候,在事務里呼叫addToBackStack()方法,就可以將加載的布局加入堆疊中,此時我們手機點擊BACK回傳鍵,不會直接退出程式,而是碎片先還原,然后再按一次才會退出,
FragmentManager fragmentManager=getSupportFragmentManager();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
//在幀布局里加入右側碎片
fragmentTransaction.replace(R.id.right_layout,new RightFragment());
//碎片入堆疊
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
4、碎片和活動之間進行通信
我們想在活動中呼叫碎片里的方法,或者在碎片里呼叫活動中的方法,該如何實作呢?
(1)活動中呼叫碎片中的方法
FragmentManager提供了一個類似于findViewById()的方法,專門從布局檔案中獲取碎片的實體,代碼如下:
RightFragment rightFragment=(RightFragment) getSupportFragmentManager().findFragmentById(R.id.right_layout);
然后就可以輕松呼叫碎片里的方法了
諸如 rightFragment.*****();
(2)碎片中呼叫活動中的方法
在每個碎片中都可以通過呼叫getActivity()方法來得到與這個碎片相關聯的活動物件
MainActivity activity=(MainActivity)getActivity();
諸如 activity.*****();
碎片到這里也就是淺層的學習了下,算是挺簡單的一個內容了,因為很容易理解,
到今天為止,書上只剩下兩個專案地圖定位和天氣預報了,爭取在12月初結束掉當前的任務,再自主開發兩個小專案,其實作在技術儲蓄已經足夠了,內心早已蠢蠢欲動,不過還是打算先學習更多的新知識再去實戰,之后打算用半年多的時間來專攻《瘋狂Android》和《瘋狂Java》這兩本書,加起來一共有1678頁,任務量還是很大的,希望自己能堅持下去,
注:文章內容摘自書籍《第一行代碼》,本文僅用來自己學習Android的復習,不存在抄襲的意圖
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/225884.html
標籤:其他
上一篇:微信瀏覽器通過WeixinJSBridge禁止右上角分享,安卓、iOS適配。(vue開發)
下一篇:90個vue常見問題總結
