Android-實(shí)現(xiàn)切換Fragment頁功能的實(shí)現(xiàn)代碼
場景:使用Fragment實(shí)現(xiàn)切頁。
類結(jié)構(gòu):

一:Activity
Activity中使用getSupportFragmentManager().beginTransaction()來填充一個(gè)Fragment(管理用的FragmentA)
Activity部分代碼:
FragmentA fragment = FragmentA.newInstant(null); getSupportFragmentManager().beginTransaction().add(R.id.f_tab_fragment,fragment).commit();
XML:
<FrameLayout
android:id="@+id/fl_container"
android:layout_width="match_parent"
android:layout_above="@+id/f_tab_fragment"
android:layout_height="match_parent"/>
<FrameLayout
android:id="@+id/f_tab_fragment"
android:layout_width="match_parent"
android:layout_height="52dp"
android:layout_alignParentBottom="true"/>
二:FragmentA
加載一個(gè)主FragmentA,作為管理其它子葉片F(xiàn)ragmentX。
現(xiàn)在比如有兩個(gè)子葉片F(xiàn)ragmentB、FragmentC.
FragmentA 使用FragmentManager和FragmentTransaction管理FragmentB、FragmentC的切換
FragmentA代碼:
public class FragmentA extends BaseFragment {
private static final String TAB_HOME = com.timediffproject.module.home.MyMainFragment.class.getName();
private static final String TAB_TEST = com.timediffproject.module.home.TestFragment.class.getName();
private BaseFragment mLastShowFragment;
private static TabFragment fragment;
public static TabFragment newInstant(Bundle bundle){
if (fragment == null){
fragment = new TabFragment();
fragment.setArguments(bundle);
}
return fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initTabInfo();
}
private void initTabInfo(){
FragmentManager fm = getFragmentManager();
if (fm == null){
return;
}
FragmentTransaction ft = fm.beginTransaction();
BaseFragment home = (BaseFragment) fm.findFragmentByTag(TAB_HOME);
if (home != null){
ft.hide(home);
}
BaseFragment test = (BaseFragment) fm.findFragmentByTag(TAB_TEST);
if (test != null){
ft.hide(test);
}
ft.commit();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_tab,container,false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.findViewById(R.id.btn_change_home).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
switchTo(TAB_HOME,null);
}
});
view.findViewById(R.id.btn_change_test).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
switchTo(TAB_TEST,null);
}
});
switchTo(TAB_HOME,null);
}
//切換Fragment的方式(FragmentB、FragmentC)
//tab為Fragment的類名(如:FragmentB.class.getName())
//R.id.fl_container是在Activity的布局里,不是在FragmentA的布局里
private void switchTo(String tab, Bundle bundle){
//初始化管理Fragment的類
FragmentManager fm = getFragmentManager();
if (fm == null){
return;
}
FragmentTransaction ft = fm.beginTransaction();
//從FragmentManager里尋找類名為tab的Fragment
BaseFragment fragment = (BaseFragment)fm.findFragmentByTag(tab);
if (fragment == null){
fragment = (BaseFragment) Fragment.instantiate(getActivity(),tab);
fragment.setArguments(bundle);
ft.add(R.id.fl_container,fragment,tab);
}else{
ft.show(fragment);
}
//隱藏現(xiàn)在正顯示的Fragment
if (mLastShowFragment != null) {
ft.hide(mLastShowFragment);
}
//記錄最后點(diǎn)擊的Fragment
mLastShowFragment = fragment;
ft.commitAllowingStateLoss();
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="52dp">
<Button
android:id="@+id/btn_change_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="切換home"
/>
<Button
android:id="@+id/btn_change_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="切換test"
/>
</LinearLayout>
三:FragmentX(FragmentB、FragmentC)
子頁的邏輯根據(jù)具體業(yè)務(wù)自己定義,實(shí)現(xiàn)與一般Fragmeng一樣
例如:
public class TestFragment extends BaseFragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {return inflater.inflate(R.layout.fragment_test,container,false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
到這里,就可以簡單的實(shí)現(xiàn)-用底部Tab切換Fragment實(shí)現(xiàn)切頁的功能
附圖:

PS:實(shí)現(xiàn)過程中出現(xiàn)的錯(cuò)誤
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
正確方式: 有關(guān)的fragment的初始化布局應(yīng)該加上false,與父類布局建立關(guān)系。
原因:不加的話這個(gè)inflater出來的view系統(tǒng)會(huì)綁定一個(gè)未知父類,這時(shí)候當(dāng)你把這個(gè)fragment又作為子頁綁定給Activity或者另一個(gè)fragment時(shí),就會(huì)出現(xiàn)以上錯(cuò)誤。
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//這里正確的寫法是:
//return inflater.inflate(R.layout.fragment_test,container,false);
return inflater.inflate(R.layout.fragment_test,container);
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android中Fragment相互切換間不被回收的實(shí)現(xiàn)方法
- 一個(gè)Activity中多個(gè)Fragment的切換
- Android UI實(shí)現(xiàn)底部切換標(biāo)簽fragment
- Android 保存Fragment 切換狀態(tài)實(shí)例代碼
- Android App中使用ViewPager+Fragment實(shí)現(xiàn)滑動(dòng)切換效果
- Android中使用TabHost 與 Fragment 制作頁面切換效果
- Android使用Fragment打造萬能頁面切換框架
- Android Fragment中使用SurfaceView切換時(shí)閃一下黑屏的解決辦法
- Android基礎(chǔ)之使用Fragment控制切換多個(gè)頁面
- anndroid使用ViewPager實(shí)現(xiàn)三個(gè)fragment切換
相關(guān)文章
Android源碼解析onResume方法中獲取不到View寬高
這篇文章主要為大家介紹了Android源碼解析onResume方法中獲取不到View寬高示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Android向node.js編寫的服務(wù)器發(fā)送數(shù)據(jù)并接收請求
這篇文章主要為大家詳細(xì)介紹了Android向node.js編寫的服務(wù)器發(fā)送數(shù)據(jù),并接收請求,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
android判斷應(yīng)用是否已經(jīng)啟動(dòng)的實(shí)例
這篇文章主要介紹了android判斷應(yīng)用是否已經(jīng)啟動(dòng)的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Android實(shí)現(xiàn)recyclerview城市字母索引列表
大家好,本篇文章主要講的是Android實(shí)現(xiàn)recyclerview城市字母索引列表,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01
Android基于API的Tabs3實(shí)現(xiàn)仿優(yōu)酷t(yī)abhost效果實(shí)例
這篇文章主要介紹了Android基于API的Tabs3實(shí)現(xiàn)仿優(yōu)酷t(yī)abhost效果,結(jié)合完整實(shí)例形式分析了Android實(shí)現(xiàn)優(yōu)酷界面效果的相關(guān)技巧,需要的朋友可以參考下2015-12-12
Android實(shí)現(xiàn)底部導(dǎo)航欄功能(選項(xiàng)卡)
這篇文章主要介紹了Android實(shí)現(xiàn)底部導(dǎo)航欄功能,可以隨意切換不同的頁面,實(shí)現(xiàn)選項(xiàng)卡功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-12-12
基于android實(shí)現(xiàn)五子棋開發(fā)
這篇文章主要為大家詳細(xì)介紹了基于android實(shí)現(xiàn)五子棋開發(fā),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02
Android實(shí)現(xiàn)Recycleview懸浮粘性頭部外加右側(cè)字母導(dǎo)航
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)Recycleview懸浮粘性頭部外加右側(cè)字母導(dǎo)航,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06

