最近在写新App,那么新App中使用的就是AndroidX那一套玩意了,然后尝试了个首页功能,Androidx viewPager + tabLayout。 结果就这点小功能就折腾了几个小时。写的过程中,中间也是踩了不少的坑,下面听我详细描述吧。

  找了几篇博客,看了下AndroidX下的代码写法,抄到了xml布局文件中, 布局如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabBackground="@android:color/transparent"
        app:tabGravity="fill"
        app:tabIndicatorColor="@color/colorAccent"
        app:tabIndicatorFullWidth="true"
        app:tabMaxWidth="0dp"
        app:tabMode="fixed"
        app:tabRippleColor="@android:color/transparent"
        app:tabSelectedTextColor="@color/colorPrimary"
        app:tabTextColor="@color/colorPrimary" />

    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="#E4E4E4" />

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </androidx.viewpager.widget.ViewPager>

</LinearLayout>

就是这样很常规的布局,然后写好了viewpager的adapter,以及代码调用等

imageFragment = ImageFragment.newInstance();
fragmentList.add(imageFragment);
videoFragment = VideoFragment.newInstance();
fragmentList.add(videoFragment);
mainAdapter = new MainAdapter(getSupportFragmentManager());
viewPager.setAdapter(mainAdapter);
tabLayout.setupWithViewPager(viewPager);

运行的结果,点击tabLayout,切换效果一切ok,但是呢,滑动ViewPager切换,就出现了问题,tabLayout滑动的效果不对,我只有两个title,期望的是滑动ViewPager,对应的tabItem切到另外一个,而不是在中间阶段停住,网上搜索了好久,也没找到答案。运行了几个开源项目写的sample,发现也如此,所以懵逼了。最后只好祭出了大招,看源码。发现ViewPager的onPageScrolled的方法里面,有个变量很关键,mDecorChildCount,用来控制viewPager子View的滑动,瞬间就明白了,找到了问题所在,改了布局,运行下,效果完美。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabBackground="@android:color/transparent"
            app:tabGravity="fill"
            app:tabIndicatorColor="@color/colorAccent"
            app:tabIndicatorFullWidth="true"
            app:tabMaxWidth="0dp"
            app:tabMode="fixed"
            app:tabRippleColor="@android:color/transparent"
            app:tabSelectedTextColor="@color/colorPrimary"
            app:tabTextColor="@color/colorPrimary" />

        <View
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:background="#E4E4E4" />


    </androidx.viewpager.widget.ViewPager>

</LinearLayout>

将tabLayout放到ViewPager里面即可。