2017-08-30 135 views
0

我想改變我的TabLayout中的選項卡圖標的顏色,實際上我使用addOnTabSelectedListener來實現這一點,但這隻適用於當我在選項卡之間滑動時。所以我希望第一個選項卡的圖標顏色與更改選項卡時的顏色相同。如何以編程方式調用ViewPagerOnTabSelectedListener?

我試過viewPager.setCurrentItem(),但這隻適用於傳遞的索引不同於0(第一個標籤)的情況。

那麼,如何以編程方式調用ViewPagerOnTabSelectedListener?

這是我的代碼:

tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager) { 
     @Override 
     public void onTabSelected(TabLayout.Tab tab){ 
      super.onTabReselected(tab); 
      int tabIconColor = ContextCompat.getColor(MainActivity.this,R.color.prehipertension); 
      tab.getIcon().setColorFilter(tabIconColor, PorterDuff.Mode.SRC_IN); 
     } 

     @Override 
     public void onTabUnselected(TabLayout.Tab tab){ 
      super.onTabUnselected(tab); 
      int tabIconColor = ContextCompat.getColor(MainActivity.this,R.color.blancoTransparencia); 
      tab.getIcon().setColorFilter(tabIconColor, PorterDuff.Mode.SRC_IN); 
     } 

     @Override 
     public void onTabReselected(TabLayout.Tab tab) { 
      int tabIconColor = ContextCompat.getColor(MainActivity.this,R.color.blanco); 
      tab.getIcon().setColorFilter(tabIconColor, PorterDuff.Mode.SRC_IN); 
     } 
    }); 

    viewPager.setCurrentItem(0); 

這是當拳頭時間運行它的應用程序。 tablayout

當滑動到第二個選項卡(並返回第一個選項卡)時,圖標顏色會正確更改。

tablayout icon enter image description here

回答

0

我不知道我能正確或不理解你的問題,但我用尋呼機查看工作和處理它通過該算法的過程。 (檢查它可能是你找到你想要的)。

1-首先確定你的標籤。

private void createTabIcons(){ 
    // i get custom tab view. 
    View profileTab = LayoutInflater.from(this) 
      .inflate(R.layout.custom_tab,null); 
    // set it's views such as its text and icon 
    ViewAppHolder.CustomTabViewHolder customTabProfileViewHolder = 
      new ViewAppHolder.CustomTabViewHolder(profileTab); 
    // set properties of icon ... note this is my main tab so i set 'color.active' 
    customTabProfileViewHolder.TAB_IMAGE_VIEW 
      .setImageDrawable(getDrawable(R.drawable.ic_person_black_24dp)); 
    customTabProfileViewHolder.TAB_IMAGE_VIEW 
      .setColorFilter(ContextCompat.getColor(this,R.color.active_tab)); 

    tabLayout.getTabAt(Constants.USER_PROFILE_TAB).setCustomView(profileTab); 
    // it's the next tab by the same previous steps 
    View friendsTab = LayoutInflater.from(this) 
      .inflate(R.layout.custom_tab,null); 

    ViewAppHolder.CustomTabViewHolder customTabFriendsViewHolder = 
      new ViewAppHolder.CustomTabViewHolder(friendsTab); 
    // the diff is here .. i set the color of icon 'color.non_active' 
    customTabFriendsViewHolder.TAB_IMAGE_VIEW 
      .setImageDrawable(getDrawable(R.drawable.ic_group_black_24dp)); 
    customTabFriendsViewHolder.TAB_IMAGE_VIEW 
      .setColorFilter(ContextCompat.getColor(this,R.color.non_active_tab)); 

/// .... and remain tabs also have the same thing with color non_active. 

2 - 然後在這兩個onTabSelectedonTabUnselected我只是調用方法稱爲setCurrentTab()和它的實現是

private void setCurrentTab() { 
    if (tabLayout != null) { 
     int currentPosition = tabLayout.getSelectedTabPosition(); 
     int unSelectedTabs = currentPosition; 

     do { 
      unSelectedTabs = (unSelectedTabs + 1) % 4; 

      Log.e("un selected ", String.valueOf(unSelectedTabs)); 
      Log.e("un selected ", String.valueOf(currentPosition)); 

      ViewAppHolder.CustomTabViewHolder customTabViewHolder = 
        new ViewAppHolder.CustomTabViewHolder(
          tabLayout.getTabAt(unSelectedTabs).getCustomView() 
        ); 


      if (unSelectedTabs != currentPosition) { 
       customTabViewHolder.TAB_IMAGE_VIEW 
         .setColorFilter(ContextCompat.getColor(this, R.color.non_active_tab)); 
      } else { 
       customTabViewHolder.TAB_IMAGE_VIEW 
         .setColorFilter(ContextCompat.getColor(this, R.color.active_tab)); 
      } 

     } while (unSelectedTabs != currentPosition); 
    } 
}