TabLayout.setOnTabSelectedListener不切换fragment
2016-05-02 23:34
399 查看
用TabLayout+ViewPager实现选项卡的地方多了,就想着把它们封装成一个类库tablayoutlibrary,布局中:
但是发现调用setOnTabSelectedListener点击菜单时没有切换fragment,不监听则正常。查看源码发现OnTabSelectedListener中的onTabSelected方法有个默认实现:
所以自己监听OnTabSelectedListener事件时需要在onTabSelected方法中调用ViewPager的setCurrentItem方法。
<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方法有个默认实现:
@Override public void onTabSelected(TabLayout.Tab tab) { mViewPager.setCurrentItem(tab.getPosition()); }
所以自己监听OnTabSelectedListener事件时需要在onTabSelected方法中调用ViewPager的setCurrentItem方法。
相关文章推荐
- Android TabLayout(选项卡布局)简单用法实例分析
- TabLayout 与 FragmentTabHost
- 使用tablayout 打造高度不一样的tab
- TabLayout的使用分享
- Support Design Library 之 TabLayout 使用与分析
- TabLayout的使用
- MaterialDesign 之TabLayout和ViewPager
- android.support.design.widget.TabLayout 用法解析
- TabLayout android Tab的一种实现方式
- AndroidDesignSupportLibrary 之 TabLayout
- TabLayout 简单实现底部图标文字导航,实现滑动底部变化效果
- 关于在eclipse中导入android support design包出错的问题【不定时更新】
- Eclipse下使用Android Design Support Library中的控件(比如TabLayout)
- TabLayout中的getSelectedTabPosition()方法返回值错误
- Android TabLayout使用注意点。
- 使用Android Support Design 控件TabLayout 方便快捷实现选项卡功能
- 带你实现开发者头条APP(四)---首页优化(加入design包)
- Android Support Design Library 之 TabLayout
- 自定义View实现顶部Tab指示器
- Android TabLayout学习资料