亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

詳解Android studio 動態fragment的用法

瀏覽:33日期:2022-09-22 11:33:59

fragment的使用時Android的基礎,它有兩種用法,第一個就是靜態的fragment。第二個則是動態的fragment。靜態fragment直接在layout創建你想要的fragment的XML的文件,然后在你的Java包里面創建對應fragment的class文件布局代碼如下所示

詳解Android studio 動態fragment的用法

<?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='歡迎來到廣西!'/></LinearLayout>

詳解Android studio 動態fragment的用法

<?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: /> <Button android:layout_width='wrap_content' android:layout_height='wrap_content' android:text='去廣東' android: /> <LinearLayout android:layout_width='match_parent' android:layout_height='wrap_content' android:orientation='vertical' android: android:layout_weight='9'> </LinearLayout> <fragment android:layout_width='wrap_content' android:layout_height='wrap_content' android: /></LinearLayout>

*這里需要注意一下,如果你不給fragment加個id,那你運行app的時候將會發生閃退現象。

詳解Android studio 動態fragment的用法

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; }}

這樣靜態fragment算是弄好了,但是這次我們主要討論動態fragment的用法

首先,我們先在activity_main中寫下如下代碼

<?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: /> <Button android:layout_width='wrap_content' android:layout_height='wrap_content' android:text='去廣東' android: /> <LinearLayout android:layout_width='match_parent' android:layout_height='wrap_content' android:orientation='vertical' android: android:layout_weight='9'> </LinearLayout></LinearLayout>

布局效果圖是這樣的

詳解Android studio 動態fragment的用法

這里fragment的XML文件和開頭所說的靜態fragment的那個XML文件的寫法是一樣的同理,fragment對應的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(){ //用于實例化fragment Fragment_1=new Fragment_1(); Fragment_2=new Fragment_2(); Fragmentnow=Fragment_1; fragmentManager=getSupportFragmentManager(); fragmentTransaction=fragmentManager.beginTransaction(); //38:只要你要對fragment進行操作就少不了這句 原來的寫法是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; } }}

下面來分析一些地方初始化功能函數

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); }

這樣寫的目的是讓代碼可讀性更好,不至于很混亂。

其次就是實例化我們所寫的fragment功能函數

private void shilihua(){ //用于實例化fragment Fragment_1=new Fragment_1(); Fragment_2=new Fragment_2(); Fragmentnow=Fragment_1; fragmentManager=getSupportFragmentManager(); fragmentTransaction=fragmentManager.beginTransaction(); //38:只要你要對fragment進行操作就少不了這句 原來的寫法是FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction(); fragmentTransaction.add(R.id.ll_rongqi,Fragment_1).commit(); }

其中的

FragmentManager fragmentManager;

這個是fragment和activity交互所要用到的。

fragmentManager=getSupportFragmentManager();

固定寫法。

private FragmentTransaction fragmentTransaction; fragmentTransaction=fragmentManager.beginTransaction();

是調動fragment操作的API,也必不可少。

fragmentTransaction.add(R.id.ll_rongqi,Fragment_1).commit();

是添加fragment所用的語句,在這里就相當于是初始化吧。add(容器id,碎片對象),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沒辦法觸發。用傳來的形參View vv來獲取到我們所點擊的按鈕來判斷操作。思想就是,如果Fragment存在,則只需要把它展示出來即可。isAdded嘛,ed過去式,那就是代表存在過咯。若是沒有,則添加就好。

好了,就到這吧,有錯誤的話希望能指出來,大家一起共同進步!

到此這篇關于詳解Android studio 動態fragment的用法的文章就介紹到這了,更多相關Android studio fragment用法內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Android
相關文章:
主站蜘蛛池模板: 毛片大全免费观看 | 国产cdts| 久久青草91免费观看 | 国产三级精品三级国产 | 国产一区高清视频 | 午夜第一页 | 国产成人精品一区二区不卡 | 手机国产日韩高清免费看片 | 一区在线视频 | 欧美aaaaa一级毛片在线 | 国产三级在线观看a | 五月激激激综合网色播免费 | 美国一级毛片免费看成人 | 亚洲激情视频网 | 欧美黑人c黑人做人爱视频 欧美黑人vs亚裔videos | 韩国美女激情视频一区二区 | 免费国产黄网站在线观看视频 | 国产精品久久久久久亚洲小说 | 99久久亚洲国产高清观看 | 亚洲欧美日韩国产精品久久 | 日韩女同性互慰免费视频 | 青草视频入口 在线观看 | 国产三香港三韩国三级不卡 | 国产萝控精品福利视频免费观看 | 亚洲一区黄色 | 日韩欧美国产中文字幕 | 黑人性视频 | 成人美女黄网站视频大全 | 国产亚洲综合一区二区在线 | 久久伊人中文字幕 | 正在播放国产酒店露脸 | 成人777777| 精品国产免费观看一区高清 | 免费看欧美成人性色生活片 | 欧美日韩综合一区 | 亚洲 日本 欧美 日韩精品 | 亚洲啪 | 国产精品久久毛片蜜月 | 免费国产成人手机在线观看 | 美国一级片免费 | 免费一级a毛片在线搐放正片 |