您的位置:首页 > 其它

TabLayout.setOnTabSelectedListener不切换fragment

2016-05-02 23:34 399 查看
用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方法有个默认实现:

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


所以自己监听OnTabSelectedListener事件时需要在onTabSelected方法中调用ViewPager的setCurrentItem方法。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  TabLayout