使 viewpager2 响应选项卡单击事件

Making viewpager2 respond to tab click events

我目前使用 TabLayoutMediator 为我的 viewpager2 设置了 tablayout。 tablayout 与滑动手势完美配合,但我还希望选项卡响应直接点击事件。这是我的代码:

OnCreate 的 Activity:

        viewPager = accessBinding.pager;
        pagerAdapter = new ScreenSlidePagerAdapter(this);
        viewPager.setAdapter(pagerAdapter);
        accessBinding.setLifecycleOwner(this);

        TabLayout tabLayout = accessBinding.accessTabs;
        TabLayout dotTabs = accessBinding.tabsDots;

        new TabLayoutMediator(tabLayout, viewPager,
                (tab, position) -> tab.setText(TAB_TITLES[position])
        ).attach();

        new TabLayoutMediator(dotTabs, viewPager,
                (tab, position) -> {}
        ).attach();

ScreenSlidePagerAdapter.java:

private static class ScreenSlidePagerAdapter extends FragmentStateAdapter {
        // The number of tabs.
        private final int NUM_TABS = 2;
        public ScreenSlidePagerAdapter(FragmentActivity fa) {
            super(fa);
        }

        @Override
        public Fragment createFragment(int position) {
            Log.d("lifecycle","viewpager createFragment");
            if(position==0)
                return new LoginFragment();
            else
                return new RegisterFragment();
        }

        @Override
        public int getItemCount() {
            return NUM_TABS;
        }
    }

布局文件:

<androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/accesslayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintVertical_weight="0">

        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/topguideline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent="0.25" />

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/access_tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/topguideline"
            app:tabGravity="fill" />

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabs_dots"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/pager"
            app:tabBackground="@drawable/tab_selector"
            app:tabPadding="0dp"
            app:tabGravity="center"
            app:tabIndicatorHeight="0dp"
            app:tabIndicatorFullWidth="false"/>

        <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginBottom="50dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toBottomOf="@id/topguideline" />
</androidx.constraintlayout.widget.ConstraintLayout>

我试过使用

tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

但它不起作用。请帮我解决这个问题。我认为 tabselected 的 viewPager 实现覆盖了我的实现,但我需要弄清楚如何让 viewpager 也响应点击事件

对我有帮助的是将父布局从 ConstraintLayout 更改为 LinearLayout。这是帮助我解决此问题的 post: