我实施了一个有三个标签的TabLayout
。当我刷页面一切正常工作时,它也适用于我只使用标签点击导航。但是,当我滑动页面并单击任何先前选择的选项卡时,问题就出现了,只有选项卡指示符更改为新选项卡,但文本突出显示并且页面没有更改。
我检查了TabLayout
的点击监听器,它在上述情况下没有执行。
更多信息:
minSdk:16
targetSdk:24
我尝试过支持库24.0.0和24.1.1
TabSelectedListener。
tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
tabPages.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
PagerAdapter
public class TabPageAdapter extends FragmentStatePagerAdapter {
public TabPageAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment f1 = new Fragment1();
Fragment f2 = new Fragment2();
Fragment f3 = new Fragment3();
switch (position) {
case 0:
return f1;
case 1:
return f2;
case 2:
return f3;
}
return null;
}
@Override
public int getCount() {
return 3;
}
}
答案 0 :(得分:1)
您可以完全避免OnTabSelectedListener
,TabLayout
有convenience method:
tabs.setupWithViewPager(tabPages);
答案 1 :(得分:0)
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Comparison"));
tabLayout.addTab(tabLayout.newTab().setText("Details"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
在活动中插入此代码。 在活动xml文件中添加以下代码
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorM3signin"
app:tabSelectedTextColor="@color/com_facebook_button_background_color_pressed"
app:tabTextColor="@color/com_facebook_button_background_color_pressed"
android:elevation="6dp"
android:inputType="textCapCharacters"
app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget"
app:tabIndicatorColor="@color/com_facebook_button_background_color_pressed"
app:tabIndicatorHeight="5dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:layout_below="@+id/toolbar" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="@id/tab_layout"/>
检查此PagerAdapeter类。
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
TabFragmentOne tab1 = new TabFragmentOne();
return tab1;
case 1:
TabFragmentTwo tab2 = new TabFragmentTwo();
return tab2;
default:
return null;
}
}
@Override
public int getCount() {
return mNumOfTabs;
}
}