Android 控件-TabLayout与ViewPager2-联合使用出现tab消失问题及其源码分析和解决方式

目录

一、前言

二、问题

三、源码分析

四、解决


一、前言

所使用到的控件如下:

com.google.android.material.tabs.TabLayout

com.google.android.material.tabs.TabItem

androidx.viewpager2.widget.ViewPager2

ViewPager2本质上是一个RecyclerView,只是被重新封装了,所以ViewPager2可以做一些很厉害的效果。

二、问题

TabLayout与ViewPager2进行联合使用,做一个关联滑动切换页面的效果时,明明在TabLayout里添加了TabItem,并且设置了文字。但两者关联上之后,还是出现tab都不显示文字,只是可以点击的现象。

布局文件关键代码如下:

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/home_tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:theme="@style/Theme.AppCompat"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/home_title"
        app:tabIndicatorFullWidth="false"
        app:tabIndicatorHeight="0dp"
        app:tabSelectedTextColor="@color/color_primary_text"
        app:tabTextAppearance="@style/TabLayoutTextAppearance"
        app:tabTextColor="@color/white">

        <com.google.android.material.tabs.TabItem
            android:id="@+id/home_tab_device"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/home_tab_device" />

        <com.google.android.material.tabs.TabItem
            android:id="@+id/home_tab_scene"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/home_tab_scene" />

        <com.google.android.material.tabs.TabItem
            android:id="@+id/home_tab_linkage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/home_tab_linkage" />
    </com.google.android.material.tabs.TabLayout>

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/home_view_pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/home_tab_layout" />

Java关联代码实现

因为TabLayout里只有和ViewPager的进行相关关联的接口,此时要联合使用ViewPager2则需要使用到TabLayoutMediator对TabLayout与ViewPager2进行attach(其实TabLayout和ViewPager联合使用也会有本文描述的问题存在,后面有机会再写一篇文章:

new TabLayoutMediator(homeTabLayout, homeViewPager, new 
        TabLayoutMediator.TabConfigurationStrategy() {
    @Override
    public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {

    }
}).attach();

此时TabLayout与ViewPager2已经关联成功,并且ViewPager2左右滑动,TabLayout也能响应。TabLayout点击,ViewPager2也能响应,只是TabLayout的item无文字显示,但点击有反馈效果。

三、源码分析

待后续空闲后补充

四、解决

1.方法一:直接在TabConfigurationStrategy的回调onConfigureTab里,重新设置Text

new TabLayoutMediator(homeTabLayout, homeViewPager, new 
        TabLayoutMediator.TabConfigurationStrategy() {
    @Override
    public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
        tab.setText(titles.get(position));
    }
}).attach();

2.方法二:在attach方法之后,重新对Tab设定Text值

homeTabLayout.getTabAt(0).setText(R.string.xxx);

3.其他解决方式:由于ViewPager2是个RecyclerView,从这入手,解决方式就可以发散了,如adapter处理等,待后续空闲后验证补充

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是实现步骤: 1. 首先在布局文件中添加TabLayout和ViewPager2控件,如下所示: ```xml <com.google.android.material.tabs.TabLayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabGravity="fill" app:tabMode="fixed"/> <androidx.viewpager2.widget.ViewPager2 android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="match_parent"/> ``` 2. 在java文件中实例化TabLayout和ViewPager2控件,并设置ViewPager2的适配器,如下所示: ```java TabLayout tabLayout = findViewById(R.id.tab_layout); ViewPager2 viewPager = findViewById(R.id.view_pager); // 设置ViewPager2的适配器 viewPager.setAdapter(new MyFragmentPagerAdapter(this)); ``` 3. 创建FragmentPagerAdapter类,并实现getItemCount()和createFragment()方法,如下所示: ```java public class MyFragmentPagerAdapter extends FragmentPagerAdapter { private Context mContext; public MyFragmentPagerAdapter(Context context) { super(context); mContext = context; } @NonNull @Override public Fragment getItem(int position) { // 根据位置返回不同的Fragment switch (position) { case 0: return new Fragment1(); case 1: return new Fragment2(); case 2: return new Fragment3(); default: return new Fragment1(); } } @Override public int getItemCount() { return 3; // 返回Fragment的数量 } } ``` 4. 在java文件中为TabLayout添加Tab选项,并设置TabLayout的监听器,如下所示: ```java // 添加Tab选项 tabLayout.addTab(tabLayout.newTab().setText("Tab1")); tabLayout.addTab(tabLayout.newTab().setText("Tab2")); tabLayout.addTab(tabLayout.newTab().setText("Tab3")); // 设置TabLayout的监听器 tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { // 当Tab被选中时,切换ViewPager2对应的页面 viewPager.setCurrentItem(tab.getPosition()); } @Override public void onTabUnselected(TabLayout.Tab tab) { } @Override public void onTabReselected(TabLayout.Tab tab) { } }); ``` 5. 最后,在Fragment中添加自己的UI布局和逻辑代码即可。 以上就是使用TabLayout和ViewPager2实现Fragment界面切换的步骤,希望能对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值