一、需求:
项目开发中的需求,需要scrollview和tablayout实现联动:
1、点击tablayout的tab,scrollview滑动到指定的位置
2、scrollview滑动到某位置时tablayout切换到对应的tab
效果如下:
效果.gif
二、开始撸代码
布局文件:
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="50dp"
app:tabIndicatorColor="@color/colorPrimary"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/colorPrimary"
app:tabTextColor="@android:color/black