TabLayout.setOnTabSelectedListener不切换fragment

来源:互联网 发布:linux查看日志命令tail 编辑:程序博客网 时间:2024/04/30 04:50

用TabLayout+ViewPager实现选项卡的地方多了,就想着把它们封装成一个类库tablayoutlibrary,布局中:

<com.yanxing.tablayoutlibrary.TabLayoutPager    android:id="@+id/tabLayoutPager"    android:layout_width="match_parent"    android:layout_height="match_parent"    app:tabLayoutTextColor="@color/colorDark"    app:tabLayoutIndicatorColor="@color/colorPrimary"    app:tabLayoutSelectedTextColor="@color/colorPrimary"/>

但是发现调用setOnTabSelectedListener点击菜单时没有切换fragment,不监听则正常。查看源码发现OnTabSelectedListener中的onTabSelected方法有个默认实现:

@Overridepublic void onTabSelected(TabLayout.Tab tab) {     mViewPager.setCurrentItem(tab.getPosition());}

所以自己监听OnTabSelectedListener事件时需要在onTabSelected方法中调用ViewPager的setCurrentItem方法。

4 1
原创粉丝点击