提问者:小点点

在ViewPager2上添加片段(FragmentManager已经在执行事务)


我正在通过MainActivity在我的应用程序中使用viewpager2。

我想在该viewPager上添加一个新片段,但是,当尝试这样做时,我收到一个错误:

Caused by: java.lang.IllegalStateException: FragmentManager is already executing transactions

主要目标是膨胀片段,以便它可以显示在选项卡布局中。

这是我用来添加片段的代码:

val biggerImageFragment = BiggerImageFragment.newInstance()
val ft = parentFragmentManager.beginTransaction()
ft.add(rootView.fragmentContainerView.id,biggerImageFragment)
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
ft.addToBackStack(null)
ft.commit()

这是布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="@color/white"
    android:gravity="center"
    android:id="@+id/tests"
    tools:context=".ui.fragments.SettingsFragment">


    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_centerInParent="true"
        android:layout_marginStart="15dp"
        android:layout_marginTop="35dp"
        android:layout_marginEnd="15dp"
        android:layout_marginBottom="15dp"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/settingsCoverImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:src="@drawable/cover_image_place_holdedr"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/settingsProfileImage"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_gravity="center"
            android:src="@drawable/profile_picture_place_holder"
            app:layout_constraintBottom_toBottomOf="@+id/settingsCoverImage"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/settingsCoverImage" />

        <TextView
            android:id="@+id/settingsUserName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="Username"
            android:textSize="18sp"
            android:textStyle="bold"
            app:layout_constraintBottom_toTopOf="@+id/cardView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/settingsProfileImage" />

        <androidx.cardview.widget.CardView
            android:id="@+id/cardView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginTop="50dp"
            android:layout_marginEnd="10dp"
            app:cardBackgroundColor="@color/secondary_color"
            app:cardCornerRadius="20dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/settingsUserName">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="horizontal"
                android:padding="10dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/settingsCoverImage">

                <ImageButton
                    android:id="@+id/settingsFacebookButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/facebook_icon"
                    android:text="Button" />

                <ImageButton
                    android:id="@+id/settingsInstagramButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="25dp"
                    android:background="@drawable/instagram_icon"
                    android:text="Button" />

                <ImageButton
                    android:id="@+id/settingsWebButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:layout_marginStart="25dp"
                    android:background="@drawable/website_icon"
                    android:text="Button" />
            </LinearLayout>
        </androidx.cardview.widget.CardView>

        <androidx.fragment.app.FragmentContainerView
            android:id="@+id/fragmentContainerView"
            android:name="com.dapps.misronim.ui.fragments.SettingsFragment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </androidx.constraintlayout.widget.ConstraintLayout>



</RelativeLayout>

这是主要活动:

<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"
    tools:context=".ui.MainActivity"
    android:orientation="vertical">


    <LinearLayout
        android:id="@+id/mainActivityLoadingLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/main_color_dark"
        android:clickable="false"
        android:gravity="center"
        android:orientation="vertical"
        android:visibility="gone">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ProgressBar
                android:id="@+id/progressBar"
                style="?android:attr/progressBarStyle"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:indeterminateTint="@color/button_color" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginStart="5dp"
                android:gravity="center"
                android:text="Loading..." />
        </LinearLayout>
    </LinearLayout>

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/Theme.Misronim.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/mainToolBar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/main_color_dark"
            app:popupTheme="@style/Theme.Misronim.PopupOverlay">

            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/toolbarUserImage"
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:layout_marginStart="20dp"
                tools:src="@drawable/tool_bar_user_icon" />

            <TextView
                android:id="@+id/toolbarUserEmailTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:textSize="14sp"
                android:textStyle="bold"
                android:text="" />

        </androidx.appcompat.widget.Toolbar>

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/mainTabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabIndicator="@color/white"
            android:background="@color/main_color_dark">

        </com.google.android.material.tabs.TabLayout>

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/mainViewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <include
        layout="@layout/content_main" />

</LinearLayout>
ViewPager Class:

    class MainViewPagerAdapter(fragmentManager: FragmentManager, lifecycle: Lifecycle) :
        FragmentStateAdapter(fragmentManager, lifecycle) {
    
    
        private val fragmentsArray : Array<Fragment> = arrayOf(
            ChatsFragment(),
            SearchFragment(),
            SettingsFragment())
    
        private val fragmentsTitleArray : ArrayList<String> = arrayListOf(
            "Chats",
            "Search",
            "Settings")
    
        override fun getItemCount(): Int {
            return fragmentsArray.size
        }
    
        override fun createFragment(position: Int): Fragment {
            return fragmentsArray[position]
        }
    
        fun getPageTitle(position : Int) : String{
            return fragmentsTitleArray[position]
        }
    
    
    }

MainActivity启动视图第2页:

val viewPagerAdapter = MainViewPagerAdapter(supportFragmentManager,lifecycle)
binding.mainViewPager.adapter = viewPagerAdapter
TabLayoutMediator(binding.mainTabLayout, binding.mainViewPager) { tab, position ->
    tab.text = viewPagerAdapter.getPageTitle(position)
}.attach()

非常感谢。


共1个答案

匿名用户

因为我在活动中使用ViewPager,所以我使用parentFragmentManager,将FragmentContainerView添加到设置布局中,并使用它来膨胀一个新片段。

这是代码:

        val biggerImageFragment = BiggerImageFragment.newInstance()
        val ft = parentFragmentManager.beginTransaction()
        ft.replace(rootView.fragmentContainerView.id,biggerImageFragment)
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
        ft.addToBackStack(null)
        ft.commit()

布局:

<androidx.fragment.app.FragmentContainerView
    android:id="@+id/fragment_container_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />