如何在TabLayout中选择了相应的标题时更改当前选项卡?

How can I change the current tab when the corresponding title is selected in the TabLayout?

我正在使用 Kotlin 开发一个 Android 应用程序,其中有一个 ViewPager,但我有一个非常简单的问题。

如何在TabLayout中选中相应的标题后改变当前的tab?

这是我配置 ViewPager 的代码:

private fun configureViewPager(view: View, context: Context) {
    //Get ViewPager from layout
    val viewPager: ViewPager = view.findViewById(R.id.view_pager)
    //Set Adapter PageAdapter and glue it together
    viewPager.adapter = PageAdapter(context, (activity as AppCompatActivity).supportFragmentManager)
    //Get TabLayout from layout
    val tabs: PagerSlidingTabStrip = view.findViewById(R.id.tabs)
    //Glue TabLayout and ViewPager together
    tabs.setViewPager(viewPager)
}

请注意,我使用 this library 创建我的 TabLayout,但如果前者不支持我正在寻找的功能,我可以恢复到 Android TabLayout。

感谢您的帮助!

由于我正在寻找的功能通常是本地支持的,所以我意识到问题确实出在我的布局上。让我解释一下:我有一个包含导航抽屉的应用程序。从中,我可以访问三个片段,其中一个包含 TabLayout + ViewPager。

这是包含 TabLayout + 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=".ui.tag.TagFragment">

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_constraintTop_toTopOf="parent"
        app:tabIndicatorColor="@color/white"
        app:tabSelectedTextColor="@color/white"/>

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/view_pager"
        android:paddingTop="?attr/actionBarSize"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</androidx.constraintlayout.widget.ConstraintLayout>

确实,在此布局中,TabLayout 正在“覆盖”ViewPager。通过交换 ViewPager 和 TabLayout,我现在可以从 TabLayout 的选项卡移动到选项卡。

所以这是我知道的布局:

<?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=".ui.tag.TagFragment">

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/view_pager"
        android:paddingTop="?attr/actionBarSize"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_constraintTop_toTopOf="parent"
        app:tabIndicatorColor="@color/white"
        app:tabSelectedTextColor="@color/white"/>

</androidx.constraintlayout.widget.ConstraintLayout>

也许还有其他(更好的)方法可以解决这个问题,请告诉我!