2016-04-14 106 views
0

我用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); 
} 

综上所述,我有我的标签缺点和文本。当一个标签被选中时,图标被改变,文本变成绿色。当选项卡未被选中时,图标将被更改,并且文本将变为白色。这在用户点击标签时工作得很好。当用户点击后退按钮时,图标和文本颜色会正确更改,但对于上一个选项卡,文本保持绿色。

回答

3

试试这个,

@Override 
    public void onBackPressed() { 

     if (viewPager.getCurrentItem() != 0) { 
      viewPager.setCurrentItem(viewPager.getCurrentItem() - 1,false); 
     }else{ 
      finish(); 
     } 

    } 
+0

第二个参数是用于平滑滚动..无论如何我测试和OMG它的工作 – user4292106

+0

我不明白为什么.. – user4292106

+0

确实它真的为你工作? –

1

不知道如果我理解正确的话您的问题,但尝试调用

super.onBackPressed(); 

if后声明。

+0

如果我使用super.onBackPressed()将关闭我的活动。 – user4292106

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); 
      } 
     }); 
... 
+0

setOnPageChangeListener已弃用 – user4292106

+0

也是我的应用程序崩溃 – user4292106

+0

使用addOnPageChangeListener代替。 –