Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在ViewPager2中启动子片段_Android_Android Viewpager_Android Viewpager2 - Fatal编程技术网

Android 在ViewPager2中启动子片段

Android 在ViewPager2中启动子片段,android,android-viewpager,android-viewpager2,Android,Android Viewpager,Android Viewpager2,我正在将ViewPager2与FragmentStateAdapter一起使用。基本3个片段(A-B-C)。这意味着,你可以从A滚动到B,B滚动到C 我想做什么? val ft: FragmentTransaction = requireActivity().supportFragmentManager.beginTransaction() ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out

我正在将
ViewPager2
FragmentStateAdapter
一起使用。基本3个片段(A-B-C)。这意味着,你可以从A滚动到B,B滚动到C

我想做什么?

val ft: FragmentTransaction = requireActivity().supportFragmentManager.beginTransaction()
ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
ft.replace(android.R.id.content, DetailsFragment())
ft.addToBackStack(null)
ft.commit();
从片段B开始子片段D

(A-B-C)
| 
D
从片段B开始片段D,当用户单击“后退箭头”(顶部栏)时,片段D将被销毁,用户将返回到B。(我不想使用活动)

我尝试了什么?

val ft: FragmentTransaction = requireActivity().supportFragmentManager.beginTransaction()
ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
ft.replace(android.R.id.content, DetailsFragment())
ft.addToBackStack(null)
ft.commit();
对于上面的代码,我有两个问题

  • 两个片段(B和D)重叠(我同时看到B和D)
  • 单击后退按钮(在顶部栏上)时,碎片B或D都不会关闭
  • 编辑: 将片段管理器传递到onCreate()方法中的main_活动中的
    ViewPager2

    PagerAdapter是一个简单的类:
    class PagerAdapter(fm:FragmentManager,lifecycle:lifecycle):FragmentStateAdapter(fm,lifecycle)

    使用
    getChildFragmentManager()
    getSupportFragmentManager()的插入部分

    这是将ViewPager片段中的细节片段作为问题状态的答案

    我还通过将viewPager放入一个片段使其变得有点复杂,如果您愿意,可以跳过并将其添加到活动中

  • 为活动创建布局
  • 包含视图管理器布局的碎片

    <?xml version="1.0" encoding="utf-8"?>
    <layout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto">
    
        <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
            <com.google.android.material.tabs.TabLayout
                    android:id="@+id/tabLayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:tabMode="scrollable" />
    
            <androidx.viewpager2.widget.ViewPager2
                    android:id="@+id/viewPager"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/tabLayout" />
    
        </androidx.constraintlayout.widget.ConstraintLayout>
    </layout>
    
    
    
    容器片段代码

    包含视图管理器的类碎片:BaseDataBindingFragment(){
    覆盖有趣的getLayoutRes():Int{
    返回R.layout.fragment_和_viewpager
    }
    覆盖创建视图(
    充气机,
    容器:视图组?,
    savedInstanceState:捆绑?
    ):查看{
    返回super.onCreateView(充气机、容器、savedInstanceState)
    }
    覆盖已创建的视图(视图:视图,保存状态:捆绑?){
    super.onViewCreated(视图,savedInstanceState)
    //表格布局
    val tabLayout=dataBinding.tabLayout
    //查看页面2
    val viewPager=dataBinding.viewPager
    /*
    
    我得到了相同的结果,但我必须将
    android.R.id.content
    更改为
    R.id.main
    ,这是
    MasterFragment()
    (B)的主容器(因为我有错误“无法在详细片段中找到
    android.R.id.content
    )已经有可用的答案。这不是viewpager v 1.0的旧解决方案?因为当您使用版本2.0时,documantation会说“使用FragmentStateAdapter”。是的。您是对的。您需要使用侦听器从viewpager片段中替换主容器。您在片段或活动中初始化viewpageradapter的位置?告诉我使用什么片段管理器你传给你的朋友adapter@CôngHả我更新了1。您应该使用添加而不是替换。2.确保为内容视图DetailFragment设置背景。设置背景时,背景不会重叠。3.当您按上一步按钮时,使用什么方法?您应该调用activity?.onBackPress()这里是我回答此问题的地方
    //  Replace Fragment with DSL style
        supportFragmentManager.commit {
            replace<FragmentThatContainsViewPager>(R.id.fragment_container_view)
    
        }
    
    <?xml version="1.0" encoding="utf-8"?>
    <layout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto">
    
        <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
            <com.google.android.material.tabs.TabLayout
                    android:id="@+id/tabLayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:tabMode="scrollable" />
    
            <androidx.viewpager2.widget.ViewPager2
                    android:id="@+id/viewPager"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/tabLayout" />
    
        </androidx.constraintlayout.widget.ConstraintLayout>
    </layout>