Android MainFragment中使用多个Fragment

2021/5/11 10:58:57

本文主要是介绍Android MainFragment中使用多个Fragment,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

当前 MainFragment中创添加多个Fragment代码如下:

MainFragment的Layout文件main_dialog_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"
    android:id="@+id/ll_root_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:clickable="true"
    android:fitsSystemWindows="true"
    android:focusableInTouchMode="true"
    android:orientation="vertical">
        //...省略无关代码
        <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/white">
        </FrameLayout>
    </LinearLayout>
</LinearLayout>

MainFragment.Java文件如下:

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        initParams();
        View rootView = inflater.inflate(R.layout.main_dialog_fragment, container, false);
        initFragments(savedInstanceState);
        return rootView;
    }
    
 //初始化两个Fragment 并且显示第一个
    private void initFragments(@Nullable Bundle savedInstanceState) {
        FragmentActivity fragmentActivity = getActivity();
        if (fragmentActivity == null) {
            return;
        }
        //savedInstanceState是对Bundle对象的引用,可以使用此捆绑包中存储的数据将自身恢复到以前的状态
        if (savedInstanceState == null) {
            try {
                //初始化Fragment
                m_fragment1 = new PaymentSubFragment1();
                m_fragment2 = new PaymentSubFragment2();
                
                fragmentActivity.getSupportFragmentManager().beginTransaction()
                        .add(R.id.fragment_container, m_fragment1, kTagFragment1)
                        .add(R.id.fragment_container, m_fragment2, kTagFragment2)
                        .detach(m_fragment2)
                        .commit();
            }catch (Exception e){
                Utils.printBBException(e,279);
            }
            //设定当前的Fragment,知道下一次hide那个Fragment
            m_CurFragment = m_fragment1;
        } else {
            //由于FragmentManager已经存在了这些Fragment直接获取就可以了
            m_fragment1 = (PaymentSubFragment1) fragmentActivity.getSupportFragmentManager()
                    .findFragmentByTag(kTagFragment1);
            m_fragment2 = (PaymentSubFragment2) fragmentActivity.getSupportFragmentManager()
                    .findFragmentByTag(kTagFragment2);
        }
    }

这时运行会报出如下错误:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.xxx.android, PID: 29708
    java.lang.IllegalArgumentException: No view found for id 0x7f08030b (com.xxx.android:id/fragment_container) for fragment PaymentSubFragment1{3820d34 (412762d8-5144-47fd-9ff7-abf3ce161066) id=0x7f08030b kTagFragment1}
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:875)
        at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
        at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439)
        at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2079)
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1869)
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824)
        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
        at androidx.fragment.app.FragmentManagerImpl$2.run(FragmentManagerImpl.java:150)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:264)
        at android.app.ActivityThread.main(ActivityThread.java:7581)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:980)

提示原因找不到View 名称为 fragment_container, 实际上Layout文件中是存在的,原因是在MainFragment中使用了getSupportFragmentManager方法,因为getSupportFragmentManager方法是属于FragmentActivity类的,因为查找的是Activity的Layout文件所以找不到这个View, 将其改为getChildFragmentManager就是当前MainFragment的Layout了,修改后代码如下:

    //初始化两个Fragment 并且显示第一个
    private void initFragments(@Nullable Bundle savedInstanceState) {
        if (savedInstanceState == null) {
            try {
                //初始化Fragment
                m_fragment1 = new PaymentSubFragment1();
                m_fragment2 = new PaymentSubFragment2();

                getChildFragmentManager().beginTransaction()
                        .add(R.id.fragment_container, m_fragment1, kTagFragment1)
                        .add(R.id.fragment_container, m_fragment2, kTagFragment2)
                        .detach(m_fragment2)
                        .commit();
            }catch (Exception e){
                Utils.printBBException(e,279);
            }
            //设定当前的Fragment,知道下一次hide那个Fragment
            m_CurFragment = m_fragment1;
        } else {
            //由于FragmentManager已经存在了这些Fragment直接获取就可以了
            m_fragment1 = (PaymentSubFragment1) getChildFragmentManager()
                    .findFragmentByTag(kTagFragment1);
            m_fragment2 = (PaymentSubFragment2) getChildFragmentManager()
                    .findFragmentByTag(kTagFragment2);
        }
    }



这篇关于Android MainFragment中使用多个Fragment的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程