gpt4 book ai didi

android - 使 viewpager2 响应选项卡单击事件

转载 作者:行者123 更新时间:2023-12-04 23:59:42 26 4
gpt4 key购买 nike

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

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 responding to clicks

关于android - 使 viewpager2 响应选项卡单击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62438297/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com