Android Fragments ViewPager2不切换片段

tvokkenx  于 2022-11-24  发布在  Android
关注(0)|答案(1)|浏览(209)

我有一个应用程序,其中使用NavigationComponentsViewPager 2
我想使用ViewPager在片段之间切换。我已经把我的ActivityMain设置为FragmentContainerView,并且我想在我的一个片段中实现ViewPager
问题是,ViewPager根本不起作用。它不改变片段,不知道为什么。我应该在代码中改变什么?
主要活动

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">
    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragmentContainerView"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>

第一个片段

class BlankFragment : Fragment() {
    private var mPag: ViewPager2? = null

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_blank, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        mPag = view.findViewById(R.id.pager123)
        val adapter = PagerAdapter(this)
        val list = mutableListOf<Fragment>()
        list.add(BlankFragment())
        list.add(BlankFragment2())
        mPag?.adapter = adapter
    }
}

.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".BlankFragment"
    android:orientation="vertical">
    <TextView
        android:id="@+id/test123"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="111111111" />

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/pager123"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#a1a1"/>
</LinearLayout>

第二个片段

class BlankFragment2 : Fragment() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_blank2, container, false)
    }
}

.xml文件

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".BlankFragment2">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="222222222222222" />

</FrameLayout>

导航图

<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/nav_graph"
    app:startDestination="@id/blankFragment">

    <fragment
        android:id="@+id/blankFragment"
        android:name="com.example.myapplication.BlankFragment"
        android:label="fragment_blank"
        tools:layout="@layout/fragment_blank" />
    <fragment
        android:id="@+id/blankFragment2"
        android:name="com.example.myapplication.BlankFragment2"
        android:label="fragment_blank2"
        tools:layout="@layout/fragment_blank2" />
</navigation>

寻呼机适配器

class PagerAdapter(fragment: Fragment) : FragmentStateAdapter(fragment) {
    val mFragments = mutableListOf<Fragment>()
    
    override fun getItemCount(): Int {
        return mFragments.size
    }

    override fun createFragment(position: Int): Fragment {
        when (position){
            0 -> return BlankFragment()
            1 -> return BlankFragment2()
        }
        return mFragments[position]
    }
}
xn1cxnb4

xn1cxnb41#

你所缺少的正是我在上一个问题中试图解释的。
需要将Navigation componentViewPager2作为两个单独的图元使用。
您的PagerAdapter也有问题。片段配接器与您可能使用过的其他配接器(mFragments)不一样。它们不应该包含那些片段的指涉。有两个主要的片段配接器(more here)。

class PagerAdapter(fragment: Fragment) : FragmentStateAdapter(fragment) {

    override fun getItemCount(): Int = 2

    override fun createFragment(position: Int): Fragment {
        return when (position){
            0 -> BlankFragment1()
            1 -> BlankFragment2()
            else -> throw IllegalArgumentException("Out of fragments, will depend on getItemCount")
        }
    }
}

您需要具有仅位于导航组件中的Fragments(例如FirstFragmentSecondFragment)和属于ViewPager2Fragments(例如BlankFragment1BlankFragment2

<?xml version="1.0" encoding="utf-8"?>
<navigation android:id="@+id/nav_graph"
    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"
    app:startDestination="@id/FirstFragment">

    <fragment
        android:id="@+id/FirstFragment"
        android:name="com.example.myapplication.FirstFragment"
        android:label="@string/first_fragment_label"
        tools:layout="@layout/fragment_first" >
        <action
            android:id="@+id/action_FirstFragment_to_SecondFragment"
            app:destination="@id/SecondFragment" />
    </fragment>

    <fragment
        android:id="@+id/SecondFragment"
        android:name="com.example.myapplication.SecondFragment"
        android:label="@string/second_fragment_label"
        tools:layout="@layout/fragment_second" />
</navigation>

ViewPager2的片段:

class BlankFragment1: Fragment() {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_blank_1, container, false)
    }
}
class BlankFragment2: Fragment() {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_blank_2, container, false)
    }
}

FirstFragment将托管ViewPager2,它将允许您在BlankFragment1BlankFragment2之间滑动。

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,savedInstanceState: Bundle?): View? {
    val view = inflater.inflate(R.layout.fragment_first, container, false)

    view.findViewById<ViewPager2>(R.id.vp2)?.let { viewPager2 ->
        val pagerAdapter = PagerAdapter(this)
        viewPager2.adapter = pagerAdapter
    }

    return view
}

现在,在任何一个片段中,要使用Navigation component,唯一需要做的事情就是简单地将findNavControlernavigate转换为所需的destination

class BlankFragment1: Fragment() {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view =  inflater.inflate(R.layout.fragment_blank_1, container, false)

        view.findViewById<MaterialButton>(R.id.blank_1_button).setOnClickListener {
            findNavController().navigate(R.id.action_FirstFragment_to_SecondFragment)
        }
        return view
    }
}

相关问题