我用android.support.design.widget.TabLayout使用viewPager。我的XML是後退按鈕和ViewPager
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/home_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="@dimen/custom_tab_layout_height"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<!-- tabs -->
<android.support.design.widget.TabLayout
android:id="@+id/home_tabs"
android:layout_width="match_parent"
android:layout_height="@dimen/custom_tab_layout_height"
android:layout_alignParentBottom="true"
app:tabGravity="fill"
android:background="@color/light_blue_low_opacity"
app:tabIndicatorHeight="0dp"
app:tabMode="fixed"
app:tabPaddingEnd="0dp"
app:tabPaddingStart="0dp"
app:tabSelectedTextColor="@color/light_green"
app:tabTextAppearance="@style/HomeTabsTextAppearance"
app:tabTextColor="@color/white" />
</RelativeLayout>
我設置viewPager如下
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.home_container);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setPagingEnabled(false);
mViewPager.setOffscreenPageLimit(5);
tabLayout = (TabLayout) findViewById(R.id.home_tabs);
tabLayout.setTabTextColors(ContextCompat.getColorStateList(this, R.color.color_home_tabs));
tabLayout.setupWithViewPager(mViewPager);
我tabListener是
tabLayout.setOnTabSelectedListener(
new TabLayout.ViewPagerOnTabSelectedListener(mViewPager) {
@Override
public void onTabSelected(TabLayout.Tab tab) {
super.onTabSelected(tab);
int position = tab.getPosition();
mViewPager.setCurrentItem(position);
String tab0TextUnSelected = "<font color='" + ContextCompat.getColor(getApplicationContext(), R.color.white) + "'> " + getResources().getString(R.string.home_tab1) + " </font>";
String tab1TextUnSelected = "<font color='" + ContextCompat.getColor(getApplicationContext(), R.color.white) + "'> " + getResources().getString(R.string.home_tab2) + "</font>";
String tab2TextUnSelected = "<font color='" + ContextCompat.getColor(getApplicationContext(), R.color.white) + "'> " + getResources().getString(R.string.home_tab3) + "</font>";
String tab3TextUnSelected = "<font color='" + ContextCompat.getColor(getApplicationContext(), R.color.white) + "'> " + getResources().getString(R.string.home_tab4) + "</font>";
String tab4TextUnSelected = "<font color='" + ContextCompat.getColor(getApplicationContext(), R.color.white) + "'> " + getResources().getString(R.string.home_tab5) + "</font>";
String tab0TextSelected = "<font color='" + ContextCompat.getColor(getApplicationContext(), R.color.light_green) + "'> " + getResources().getString(R.string.home_tab1) + " </font>";
String tab1TextSelected = "<font color='" + ContextCompat.getColor(getApplicationContext(), R.color.light_green) + "'> " + getResources().getString(R.string.home_tab2) + "</font>";
String tab2TextSelected = "<font color='" + ContextCompat.getColor(getApplicationContext(), R.color.light_green) + "'> " + getResources().getString(R.string.home_tab3) + "</font>";
String tab3TextSelected = "<font color='" + ContextCompat.getColor(getApplicationContext(), R.color.light_green) + "'> " + getResources().getString(R.string.home_tab4) + "</font>";
String tab4TextSelected = "<font color='" + ContextCompat.getColor(getApplicationContext(), R.color.light_green) + "'> " + getResources().getString(R.string.home_tab5) + "</font>";
TabLayout.Tab tab0 = tabLayout.getTabAt(0);
TabLayout.Tab tab1 = tabLayout.getTabAt(1);
TabLayout.Tab tab2 = tabLayout.getTabAt(2);
TabLayout.Tab tab3 = tabLayout.getTabAt(3);
TabLayout.Tab tab4 = tabLayout.getTabAt(4);
if (tab0 != null) {
tab0.setIcon(R.drawable.ic_home_profile_unselected);
tab0.setText(Html.fromHtml(tab0TextUnSelected));
}
if (tab1 != null) {
tab1.setIcon(R.drawable.ic_home_rewards_unselected);
tab1.setText(Html.fromHtml(tab1TextUnSelected));
}
if (tab2 != null) {
tab2.setIcon(R.drawable.ic_home_gallups_unselected);
tab2.setText(Html.fromHtml(tab2TextUnSelected));
}
if (tab3 != null) {
tab3.setIcon(R.drawable.ic_home_statistics_unselected);
tab3.setText(Html.fromHtml(tab3TextUnSelected));
}
if (tab4 != null) {
tab4.setIcon(R.drawable.ic_home_settings_unselected);
tab4.setText(Html.fromHtml(tab4TextUnSelected));
}
if (position == 0) {
if (tab0 != null) {
tab0.setIcon(R.drawable.ic_home_profile_selected);
tab0.setText(Html.fromHtml(tab0TextSelected));
}
} else if (position == 1) {
if (tab1 != null) {
tab1.setIcon(R.drawable.ic_home_rewards_selected);
tab1.setText(Html.fromHtml(tab1TextSelected));
}
} else if (position == 2) {
if (tab2 != null) {
tab2.setIcon(R.drawable.ic_home_gallups_selected);
tab2.setText(Html.fromHtml(tab2TextSelected));
}
} else if (position == 3) {
if (tab3 != null) {
tab3.setIcon(R.drawable.ic_home_statistics_selected);
tab3.setText(Html.fromHtml(tab3TextSelected));
}
} else if (position == 4) {
if (tab4 != null) {
tab4.setIcon(R.drawable.ic_home_settings_selected);
tab4.setText(Html.fromHtml(tab4TextSelected));
}
}
}
});
我重寫onBackPressed
方法如下
if (mViewPager.getCurrentItem() != 0) {
mViewPager.setCurrentItem(mViewPager.getCurrentItem() - 1, true);
}
綜上所述,我有我的標籤缺點和文本。當一個標籤被選中時,圖標被改變,文本變成綠色。當選項卡未被選中時,圖標將被更改,並且文本將變爲白色。這在用戶點擊標籤時工作得很好。當用戶點擊後退按鈕時,圖標和文本顏色會正確更改,但對於上一個選項卡,文本保持綠色。
第二個參數是用於平滑滾動..無論如何我測試和OMG它的工作 – user4292106
我不明白爲什麼.. – user4292106
確實它真的爲你工作? –