0

Screen shot of Tab where text color remains highlighted 我正在使用android視圖傳呼機和標籤頁佈局。在標籤佈局的XML我設置tablayout與此代碼查看傳呼機的標籤頁佈局文本未更新

 `<android.support.design.widget.TabLayout 
     android:id="@+id/tabs" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:tabMode="fixed" 
     app:tabBackground="@color/LightGrey" 
     app:tabIndicatorColor="@color/app_highlight_color" 
     app:tabIndicatorHeight="3dp" 
     app:tabTextAppearance="@style/MyCustomTextAppearance" 
     app:tabSelectedTextColor="@color/app_highlight_color" 
     app:tabGravity="fill"/>` 

的問題是,當我改變v​​iewpager的頁/片段與刷卡左/右它工作正常,並從正常更改標籤的文本顏色,以突出反之亦然。

但是,當我點擊任何選項卡來更改片段的片段更改,但選項卡的文本仍然保持選定狀態。當用戶觸摸其它tab.This是我的設置選項卡布局Java函數

private void setupTabLayout() { 
tabLayout.setTabTextColors(R.color.LightGrey,R.color.app_highlight_color); 
    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { 
     @Override 
     public void onTabSelected(TabLayout.Tab tab) { 


      //tab.setCustomView(R.drawable.tab_trending_selected); 
      switch (tab.getPosition()) { 
       case AppConstants.TRENDING_INDEX: 
        tab.setIcon(R.drawable.tab_trending_selected); 
        tab.select(); 

        break; 
       case AppConstants.DUB_INDEX: 
        tab.setIcon(R.drawable.tab_dub_selected); 
        tab.select(); 
        break; 
       case AppConstants.PROFILE_INDEX: 
        tab.setIcon(R.drawable.tab_profile_selected); 
        tab.select(); 
        break; 
       default: 
        break; 
      } 
     } 

     @Override 
     public void onTabUnselected(TabLayout.Tab tab) { 

      switch (tab.getPosition()) { 
       case AppConstants.TRENDING_INDEX: 
        tab.setIcon(R.drawable.tab_trending_unselected); 
        //tab.setText("Trending"); 

        break; 
       case AppConstants.DUB_INDEX: 
        tab.setIcon(R.drawable.tab_dub_unselected); 
        // tab.setText("Dub"); 
        break; 
       case AppConstants.PROFILE_INDEX: 
        tab.setIcon(R.drawable.tab_profile_unselected); 
        // tab.setText("Profile"); 
        break; 
       default: 
        break; 
      } 

     } 

     @Override 
     public void onTabReselected(TabLayout.Tab tab) { 

     } 
    }); 
    tabLayout.setTabTextColors(getResources().getColor(R.color.Gray),  getResources().getColor(R.color.app_highlight_color)); 
    tabLayout.getTabAt(AppConstants.TRENDING_INDEX).setIcon(R.drawable.tab_trending_selected); 
    tabLayout.getTabAt(AppConstants.DUB_INDEX).setIcon(R.drawable.tab_dub_unselected); 
    tabLayout.getTabAt(AppConstants.PROFILE_INDEX).setIcon(R.drawable.tab_profile_unselected); 

    tabLayout.getTabAt(AppConstants.TRENDING_INDEX).select(); 


} 
+0

添加tabLayout.getTabAt(position).select(); –

+0

當我通過點擊任意選項卡中的Tablayout –

+0

來更改頁面時,選項卡文本顏色不會改變刪除「tabLayout.setTabTextColors(R.color.LightGrey,R.color.app_highlight_color);」此行並檢查 –

回答

0

面對獨特的問題選擇應該取消。當我們設置setCurrentItem。它不會更改Tablayout的選項卡。然後,您必須在viewpager上添加OnPageChangeListener,您必須手動爲所選viewpager的位置選擇tablayout的選項卡。然後setupWithViewPager。

添加此代碼與您的viewPage這是爲我工作。

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { 
     @Override 
     public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 
     } 

     @Override 
     public void onPageSelected(int position) { 

      tabLayout.getTabAt(position).select(); 
     } 

     @Override 
     public void onPageScrollStateChanged(int state) { 
     } 
    }); 

    /* 
     NOTE: This is setup after addOnPageChangeListener. Don't know why but this is what works. Otherwise tabLayout.does not select. 
    */ 
    tabLayout.setupWithViewPager(this.viewPager); 
+0

當我通過點擊tablayout中的任何選項卡來更改頁面時,標籤文本顏色不會改變 –

0

試試這個:

viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { 
    @Override 
    public void onPageSelected(int pos) { 
// set the title 
     ab.setSelectedNavigationItem(pos); 
     ab.setTitle(titles[pos]); 
    } 

    @Override 
    public void onPageScrolled(int arg0, float arg1, int arg2) { 

    } 

    @Override 
    public void onPageScrollStateChanged(int arg0) { 

    } 
}); 
0

使用一些這樣的事。

<style name="CustomTabLayout" parent="Widget.Design.TabLayout"> 
    <item name="tabIndicatorColor">@color/colorAccent</item> 
    <item name="tabSelectedTextColor">@color/colorAccent</item> 
    <item name="tabTextAppearance">@style/CategoryTabTextAppearance</item> 
    <item name="tabBackground">@color/colorPrimary</item> 
</style> 

<style name="CategoryTabTextAppearance" parent="TextAppearance.Design.Tab"> 
    <item name="android:textColor">@color/secondaryText</item> 
</style>