詳解Android studio 動(dòng)態(tài)fragment的用法
fragment的使用時(shí)Android的基礎(chǔ),它有兩種用法,第一個(gè)就是靜態(tài)的fragment。第二個(gè)則是動(dòng)態(tài)的fragment。
靜態(tài)fragment直接在layout創(chuàng)建你想要的fragment的XML的文件,然后在你的Java包里面創(chuàng)建對(duì)應(yīng)fragment的class文件
布局代碼如下所示
<?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:layout_width="match_parent" android:layout_height="wrap_content" android:text="歡迎來(lái)到廣西!"/> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <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="vertical" tools:context=".MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="去廣西" android:id="@+id/bt_anjian1"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="去廣東" android:id="@+id/bt_anjian2"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:id="@+id/ll_rongqi" android:layout_weight="9"> </LinearLayout> <fragment android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/fragment_1"/> </LinearLayout>
*這里需要注意一下,如果你不給fragment加個(gè)id,那你運(yùn)行app的時(shí)候?qū)?huì)發(fā)生閃退現(xiàn)象。
package com.example.anyone_fragment_2; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; public class Fragment_1 extends Fragment { @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment_1,container,false); return view; } }
這樣靜態(tài)fragment算是弄好了,但是這次我們主要討論動(dòng)態(tài)fragment的用法
首先,我們先在activity_main中寫(xiě)下如下代碼
<?xml version="1.0" encoding="utf-8"?> <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="vertical" tools:context=".MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="去廣西" android:id="@+id/bt_anjian1"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="去廣東" android:id="@+id/bt_anjian2"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:id="@+id/ll_rongqi" android:layout_weight="9"> </LinearLayout> </LinearLayout>
布局效果圖是這樣的
這里fragment的XML文件和開(kāi)頭所說(shuō)的靜態(tài)fragment的那個(gè)XML文件的寫(xiě)法是一樣的
同理,fragment對(duì)應(yīng)的class文件也是相同的。
package com.example.anyone_fragment_2; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentTransaction; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity implements View.OnClickListener {//有abstract就閃退 private Button bt_anjian1,bt_anjian2; private Fragment Fragment_1,Fragmentnow,Fragment_2; private FragmentManager fragmentManager; private FragmentTransaction fragmentTransaction; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); chushihua(); shilihua(); } private void chushihua() { bt_anjian1=findViewById(R.id.bt_anjian1); bt_anjian2=findViewById(R.id.bt_anjian2); bt_anjian1.setOnClickListener(this); bt_anjian2.setOnClickListener(this); } private void shilihua(){ //用于實(shí)例化fragment Fragment_1=new Fragment_1(); Fragment_2=new Fragment_2(); Fragmentnow=Fragment_1; fragmentManager=getSupportFragmentManager(); fragmentTransaction=fragmentManager.beginTransaction(); //38:只要你要對(duì)fragment進(jìn)行操作就少不了這句 原來(lái)的寫(xiě)法是FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction(); fragmentTransaction.add(R.id.ll_rongqi,Fragment_1).commit(); } public void onClick(View vv) { fragmentTransaction=fragmentManager.beginTransaction(); switch (vv.getId()) { case R.id.bt_anjian1:if (Fragment_1.isAdded()) { fragmentTransaction.hide(Fragmentnow).show(Fragment_1).commit(); } else { fragmentTransaction.hide(Fragmentnow).add(R.id.ll_rongqi,Fragment_1).commit(); } Fragmentnow=Fragment_1; break; case R.id.bt_anjian2:if(Fragment_2.isAdded()) { fragmentTransaction.hide(Fragmentnow).show(Fragment_2).commit(); } else { fragmentTransaction.hide(Fragmentnow).add(R.id.ll_rongqi,Fragment_2).commit(); } Fragmentnow=Fragment_2; break; } } }
下面來(lái)分析一些地方
初始化功能函數(shù)
private void chushihua() { bt_anjian1=findViewById(R.id.bt_anjian1); bt_anjian2=findViewById(R.id.bt_anjian2); bt_anjian1.setOnClickListener(this); bt_anjian2.setOnClickListener(this); }
這樣寫(xiě)的目的是讓代碼可讀性更好,不至于很混亂。
其次就是實(shí)例化我們所寫(xiě)的fragment功能函數(shù)
private void shilihua(){ //用于實(shí)例化fragment Fragment_1=new Fragment_1(); Fragment_2=new Fragment_2(); Fragmentnow=Fragment_1; fragmentManager=getSupportFragmentManager(); fragmentTransaction=fragmentManager.beginTransaction(); //38:只要你要對(duì)fragment進(jìn)行操作就少不了這句 原來(lái)的寫(xiě)法是FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction(); fragmentTransaction.add(R.id.ll_rongqi,Fragment_1).commit(); }
其中的
FragmentManager fragmentManager;
這個(gè)是fragment和activity交互所要用到的。
fragmentManager=getSupportFragmentManager();
固定寫(xiě)法。
而
private FragmentTransaction fragmentTransaction; fragmentTransaction=fragmentManager.beginTransaction();
是調(diào)動(dòng)fragment操作的API,也必不可少。
fragmentTransaction.add(R.id.ll_rongqi,Fragment_1).commit();
是添加fragment所用的語(yǔ)句,在這里就相當(dāng)于是初始化吧。add(容器id,碎片對(duì)象),commit則是提交。
public void onClick(View vv) { fragmentTransaction=fragmentManager.beginTransaction(); switch (vv.getId()) { case R.id.bt_anjian1:if (Fragment_1.isAdded()) { fragmentTransaction.hide(Fragmentnow).show(Fragment_1).commit(); } else { fragmentTransaction.hide(Fragmentnow).add(R.id.ll_rongqi,Fragment_1).commit(); } Fragmentnow=Fragment_1; break; case R.id.bt_anjian2:if(Fragment_2.isAdded()) { fragmentTransaction.hide(Fragmentnow).show(Fragment_2).commit(); } else { fragmentTransaction.hide(Fragmentnow).add(R.id.ll_rongqi,Fragment_2).commit(); } Fragmentnow=Fragment_2; break; } }
這里的onClick的名字是不能改變的,否則你button沒(méi)辦法觸發(fā)。
用傳來(lái)的形參View vv來(lái)獲取到我們所點(diǎn)擊的按鈕來(lái)判斷操作。
思想就是,如果Fragment存在,則只需要把它展示出來(lái)即可。isAdded嘛,ed過(guò)去式,那就是代表存在過(guò)咯。
若是沒(méi)有,則添加就好。
好了,就到這吧,有錯(cuò)誤的話(huà)希望能指出來(lái),大家一起共同進(jìn)步!
到此這篇關(guān)于詳解Android studio 動(dòng)態(tài)fragment的用法的文章就介紹到這了,更多相關(guān)Android studio fragment用法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android?Fragment實(shí)現(xiàn)頂部、底部導(dǎo)航欄
- Android Fragment源碼分析Add方法
- Android開(kāi)發(fā)之Fragment懶加載的幾種方式及性能對(duì)比
- Android入門(mén)教程之Fragment的具體使用詳解
- Android Fragment使用全解
- Android使用Fragment實(shí)現(xiàn)兼容手機(jī)和平板的程序
- Android Fragment監(jiān)聽(tīng)返回鍵的一種合理方式
- Android Fragment實(shí)現(xiàn)底部通知欄
- Android Fragment的具體使用方式詳解
相關(guān)文章
Android開(kāi)發(fā) -- 控件的顯示與隱藏 setVisibility View.VISIBLE View.INVISI
本文簡(jiǎn)單介紹在Android開(kāi)發(fā)中控件的顯示與隱藏幾種常見(jiàn)的屬性,給大家一個(gè)參考,希望對(duì)大家學(xué)習(xí)有所幫助。2016-06-06Android用viewPager2實(shí)現(xiàn)UI界面翻頁(yè)滾動(dòng)的效果
自學(xué)Android中,然后需要實(shí)現(xiàn)UI頁(yè)面翻頁(yè)滑動(dòng)的效果,例如頭條的功能 我這邊利用了viewPager2的翻頁(yè)功能,具體流程如下:2021-05-05Android亮度調(diào)節(jié)的幾種實(shí)現(xiàn)方法
本篇文章詳細(xì)介紹了Android亮度調(diào)節(jié)的幾種實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-11-11Android studio實(shí)現(xiàn)加法軟件
這篇文章主要為大家詳細(xì)介紹了Android studio實(shí)現(xiàn)加法軟件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03Kotlin淺析延遲初始化與密封類(lèi)的實(shí)現(xiàn)方法
Kotlin語(yǔ)言的許多特性,包括變量不可變,變量不可為空,等等。這些特性都是為了盡可能地保證程序安全而設(shè)計(jì)的,但是有些時(shí)候這些特性也會(huì)在編碼時(shí)給我們帶來(lái)不少的麻煩,下面我們來(lái)了解延遲初始化和密封類(lèi)的特點(diǎn)2022-08-08Android使用 Spinner控件實(shí)現(xiàn)下拉框功能
Spinner是android的一種控件,用它我們可以實(shí)現(xiàn)下拉框。下面通過(guò)實(shí)例代碼給大家介紹Android使用 Spinner控件實(shí)現(xiàn)下拉框功能,感興趣的朋友一起看看吧2018-08-08Android自定義標(biāo)尺滑動(dòng)選擇值效果
這篇文章主要為大家詳細(xì)介紹了Android自定義標(biāo)尺滑動(dòng)選擇值效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09