在我的另一个片段有类似的TabLayout和ViewPager2架构,但该片段的TabLayout工作正常。这个问题只发生在点击标签,但滑动页面在ViewPager2的指示器工作正常,如果我点击标签,然后滑动到其他页面的指示器也会工作正常。我的代码有什么问题吗?
xml中的选项卡布局和ViewPager2:
<com.google.android.material.tabs.TabLayout
android:id="@+id/mTabLayout"
style="@style/MyTabStyle2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/light_gray_1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:tabGravity="fill"
tools:ignore="SpeakableTextPresentCheck"
tools:layout_height="50dp">
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/mViewPager"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/light_gray_1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/mTabLayout" />
下面是放置TabLayout和ViewPager2片段:
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initAdapter();
initViewPager();
initTabLayout();
}
private void initAdapter() {
titlesList.add(getString(R.string.message_tab_multi));
titlesList.add(getString(R.string.message_tab_single));
titlesList.add(getString(R.string.message_tab_welcome));
productPagerAdapter = new MessagePager2Adapter(this);
}
public void initViewPager() {
mViewPager.setAdapter(productPagerAdapter);
mViewPager.setOffscreenPageLimit(titlesList.size());
}
public void initTabLayout() {
new TabLayoutMediator(mTabLayout, mViewPager, (tab, position) ->
tab.setText(titlesList.get(position))).attach();
}
private class MessagePager2Adapter extends FragmentStateAdapter {
public MessagePager2Adapter(@NonNull FragmentActivity fragmentActivity) {
super(fragmentActivity);
}
public MessagePager2Adapter(@NonNull Fragment fragment) {
super(fragment);
}
public MessagePager2Adapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
super(fragmentManager, lifecycle);
}
@NonNull
@Override
public Fragment createFragment(int position) {
switch (position) {
default:
case TAB_MESSAGE_MULTI:
return MessagePostMultiFragment.newInstance();
case TAB_MESSAGE_SINGLE:
return MessagePostSingleFragment.newInstance();
case TAB_MESSAGE_WELCOME:
return MessagePostWelcomeFragment.newInstance();
}
}
@Override
public int getItemCount() {
return titlesList.size();
}
}
问题图像和视频:Click tab at position 1(click position 1 but indicator still at position 0)video显示器
下面是另一个片段架构:
xml中的选项卡布局和ViewPager2:
<com.google.android.material.tabs.TabLayout
android:id="@+id/mTabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
style="@style/MyTabStyle"
app:tabGravity="fill"
android:background="@color/light_gray_2"
tools:layout_height="50dp"/>
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/mViewPager"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/light_gray_1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/mTabLayout" />
放置TabLayout和ViewPager2片段:
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initAdapter();
initViewPager();
initTabLayout();
}
private void initAdapter() {
titlesList.add(getString(R.string.product_tab_product_search));
titlesList.add(getString(R.string.product_tab_product_serving));
pagerAdapter = new ProductPagerAdapter(this);
}
public void initViewPager() {
mViewPager.setAdapter(pagerAdapter);
mViewPager.setOffscreenPageLimit(titlesList.size());
}
public void initTabLayout() {
new TabLayoutMediator(mTabLayout, mViewPager, (tab, position) ->
tab.setText(titlesList.get(position))).attach();
}
public class ProductPagerAdapter extends FragmentStateAdapter {
public ProductPagerAdapter(@NonNull FragmentActivity fragmentActivity) {
super(fragmentActivity);
}
public ProductPagerAdapter(@NonNull Fragment fragment) {
super(fragment);
}
public ProductPagerAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
super(fragmentManager, lifecycle);
}
@NonNull
@Override
public Fragment createFragment(int position) {
switch (position) {
case TAB_PRODUCT_SEARCH:
return ProductSearchFragment.newInstance();
case TAB_PRODUCT_SERVING:
return ProductServingFragment.newInstance();
default:
return ProductSearchFragment.newInstance();
}
}
@Override
public int getItemCount() {
return titlesList.size();
}
}
更新:我的ViewPager2中的每个片段都有recyclerView,这种情况只在recyclerView有项时发生,如果我设置适配器getItemCount返回0,那么指示器工作正常,但是返回1或更多,指示器将被破坏,甚至我的TabLayout不会与ViewPager2连接。
1条答案
按热度按时间nxowjjhe1#
我通过设置TabLayout IndicatorAnimationDuration 0解决了这个问题,但是我不知道问题的确切原因。有人能为我解释一下吗?谢谢。