[英]Making viewpager2 respond to tab click events
我目前使用 TabLayoutMediator 设置了带有 tablayout 的 viewpager2。 选项卡布局与滑动手势完美配合,但我也希望选项卡响应直接点击事件。 这是我的代码:
Activity 的 OnCreate:
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。 这是帮助我解决此问题的帖子: Android: TabLayout not respond to clicks
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.