本文介绍了具有TabLayout的ViewPager2无法导航到正确的选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 TabLayout (包含5个标签)似乎与 ViewPager2 不能正常工作,以便重新使用。该应用程序可以很好地加载,但是当我单击Tab D时,它将转到TabE。为什么会发生这种情况,可以采取什么措施解决此问题?

My TabLayout (containing 5 tabs) doesn't seem to behave with ViewPager2 properly for some reasomn. The app loads fine but when I click Tab D, it goes to Tab E instead. Why does that happen and what can be done to fix this?

活动

片段

class MyFragment : androidx.fragment.app.Fragment() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val v = inflater.inflate(R.layout.my_fragment, container, false)
        super.onCreate(savedInstanceState)

        return v
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {

        // Set TabLayout tab names
        mViewPager2!!.adapter = MyAdapter(fragmentManager, lifecycle)
        TabLayoutMediator(mTabLayout, mViewPager2, TabLayoutMediator.TabConfigurationStrategy{ tab, position ->
            when (position) {
                0 -> tab.text = getString(R.string.a)
                1 -> tab.text = getString(R.string.b)
                2 -> tab.text = getString(R.string.c)
                3 -> tab.text = getString(R.string.d)
                4 -> tab.text = getString(R.string.e)
            }
        }).attach()

        super.onActivityCreated(savedInstanceState)
    }

    // Set TabLayout tab classes
    private inner class MyAdapter(fm: FragmentManager?, lifecycle: Lifecycle) : FragmentStateAdapter(fm!!, lifecycle) {
        private val intItems = 5

        override fun createFragment(position: Int): Fragment {
            var fragment: Fragment? = null
            when (position) {
                0 -> fragment = FragmentA()
                1 -> fragment = FragmentB()
                2 -> fragment = FragmentC()
                3 -> fragment = FragmentD()
                4 -> fragment = FragmentE()
            }
            return fragment!!
        }

        override fun getItemCount(): Int {
            return intItems
        }
    }
}

my_fragment.xml(带选项卡的ViewPager2)

<?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:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/detail_container">

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

    <com.google.android.material.tabs.TabLayout
            android:id="@+id/myTabLayout"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"
            app:tabMode="scrollable"
            app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget" />

    <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/mViewPager2"
            android:layout_height="0dp"
            android:layout_width="match_parent"
            android:layout_weight="1" />
</LinearLayout>


推荐答案

我的想法是您使用了错误的 fragmentManager 链接的教程在 Activity 中创建viewpager2,就像您在Fragment中创建它并可能使用 fragmentManager 已经与 fragment MyFragment相关联的东西。

My thoughts are that you are using the wrong fragmentManager the tutorial you linked creates the viewpager2 in an Activity where as you are creating it in a Fragment and possibly using the fragmentManager that already has a fragment "MyFragment" associated with it.

在我对您的其他问题的回答上,它链接到片段的viewpager2的另一个构造函数的源代码使用 getChildFragmentManager

In my answer https://stackoverflow.com/a/60961499/2373819 to your other question it links to the source code of the other constructor of viewpager2 for a Fragment uses the getChildFragmentManager

这是

因此,因为我认为您使用的不是正确的 fragmentManager

Therefore because I don't think you are using the right fragmentManager the extra parent fragment is causing this off by one bug.

我建议您构建一个适配器,为其提供当前的片段例如

I suggest you either construct you adapter giving it the current fragment like

mViewPager2!!.adapter = MyAdapter(this)

 mViewPager2!!.adapter = MyAdapter(this.getChildFragmentManager(), this.getLifecycle())

对不起,我的kotlin不好,但是希望您能理解(基本两个选项是相同的,第一个可能是最好的使用方式)

Sorry my kotlin is not great, but hopefully you get the idea (basic the two options are the same and the first is probably the best to use)

这篇关于具有TabLayout的ViewPager2无法导航到正确的选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 07:53