我正在使用viewPager和android.support.design.widget.TabLayout。我的xml是
<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);
}
总而言之,我有图标和文字标签。选择选项卡后,图标将更改,文本将变为绿色。取消选择选项卡后,图标将更改,文本将变为白色。当用户单击选项卡时,这很好用。当用户单击后退按钮时,图标和文本颜色会正确更改,但对于上一个选项卡,文本仍保持绿色。
答案 0 :(得分:7)
试试这个,
@Override
public void onBackPressed() {
if (viewPager.getCurrentItem() != 0) {
viewPager.setCurrentItem(viewPager.getCurrentItem() - 1,false);
}else{
finish();
}
}
但不要调用super.onBackPressed()方法。
答案 1 :(得分:1)
不确定我是否正确理解了您的问题,但请尝试致电
super.onBackPressed();
在您if
陈述之后。
答案 2 :(得分:0)
您的ViewPager需要一个OnPageChangeListener来更改选定的标签。
mViewPager.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// When swiping between pages, select the
// corresponding tab.
getActionBar().setSelectedNavigationItem(position);
}
});
...
答案 3 :(得分:0)
使用以下代码覆盖onBackPressed()
的{{1}}方法:
Activity