2323import android .graphics .Paint ;
2424import android .util .AttributeSet ;
2525import android .util .TypedValue ;
26+ import android .util .Log ;
2627import android .view .View ;
2728import android .widget .LinearLayout ;
2829import android .widget .TextView ;
@@ -45,7 +46,6 @@ class TabStrip extends LinearLayout {
4546 private int mSelectedPosition ;
4647 private float mSelectionOffset ;
4748
48- private TabLayout .TabColorizer mCustomTabColorizer ;
4949 private final SimpleTabColorizer mDefaultTabColorizer ;
5050
5151 private int mTabTextColor ;
@@ -94,14 +94,7 @@ class TabStrip extends LinearLayout {
9494 setMeasureWithLargestChildEnabled (true );
9595 }
9696
97- void setCustomTabColorizer (TabLayout .TabColorizer customTabColorizer ) {
98- mCustomTabColorizer = customTabColorizer ;
99- invalidate ();
100- }
101-
10297 void setSelectedIndicatorColors (int ... colors ) {
103- // Make sure that the custom colorizer is removed
104- mCustomTabColorizer = null ;
10598 mDefaultTabColorizer .setIndicatorColors (colors );
10699 invalidate ();
107100 }
@@ -128,12 +121,24 @@ void setShouldUpdateTabsTextColor(boolean value) {
128121 mShouldUpdateTabsTextColor = value ;
129122 }
130123
124+
125+ private void updateTabTextColor (int index , boolean selected ){
126+ if (mShouldUpdateTabsTextColor ) {
127+ TextView textView = getTextViewAt (index );
128+ if (selected ){
129+ textView .setTextColor (mSelectedTabTextColor );
130+ }
131+ else {
132+ textView .setTextColor (mTabTextColor );
133+ }
134+ }
135+ }
136+
131137 private void updateTabsTextColor (){
132138 if (mShouldUpdateTabsTextColor ) {
133139 final int childCount = getChildCount ();
134140 for (int i = 0 ; i < childCount ; i ++){
135- LinearLayout linearLayout = (LinearLayout )getChildAt (i );
136- TextView textView = (TextView )linearLayout .getChildAt (1 );
141+ TextView textView = getTextViewAt (i );
137142 if (i == mSelectedPosition ){
138143 textView .setTextColor (mSelectedTabTextColor );
139144 }
@@ -153,26 +158,21 @@ float getTabTextFontSize(){
153158 return mTabTextFontSize ;
154159 }
155160
161+ private TextView getTextViewAt (int index ) {
162+ LinearLayout linearLayout = (LinearLayout )getChildAt (index );
163+ return (TextView )linearLayout .getChildAt (1 );
164+ }
165+
156166 private void updateTabsTextFontSize (){
157167 final int childCount = getChildCount ();
158168 for (int i = 0 ; i < childCount ; i ++){
159- LinearLayout linearLayout = (LinearLayout )getChildAt (i );
160- TextView textView = (TextView )linearLayout .getChildAt (1 );
169+ TextView textView = getTextViewAt (i );
161170 textView .setTextSize (mTabTextFontSize );
162171 }
163172 }
164173
165- // Used by TabLayout (the 'old' tab-view control)
166- void onViewPagerPageChanged (int position , float positionOffset ) {
167- mSelectedPosition = position ;
168- mSelectionOffset = positionOffset ;
169- invalidate ();
170- updateTabsTextColor ();
171- }
172174
173- // Used by TabsBar
174175 void onTabsViewPagerPageChanged (int position , float positionOffset ) {
175- mSelectedPosition = position ;
176176 mSelectionOffset = positionOffset ;
177177 invalidate ();
178178 }
@@ -182,9 +182,14 @@ int getSelectedPosition(){
182182 }
183183
184184 void setSelectedPosition (int position ) {
185- mSelectedPosition = position ;
186- invalidate ();
187- updateTabsTextColor ();
185+ if (mSelectedPosition != position ) {
186+ getChildAt (mSelectedPosition ).setSelected (false );
187+ getChildAt (position ).setSelected (true );
188+ updateTabTextColor (mSelectedPosition , false );
189+ updateTabTextColor (position , false );
190+ mSelectedPosition = position ;
191+ invalidate ();
192+ }
188193 }
189194
190195 @ Override
@@ -193,9 +198,7 @@ protected void dispatchDraw(Canvas canvas) {
193198
194199 final int height = getHeight ();
195200 final int childCount = getChildCount ();
196- final TabLayout .TabColorizer tabColorizer = mCustomTabColorizer != null
197- ? mCustomTabColorizer
198- : mDefaultTabColorizer ;
201+ final SimpleTabColorizer tabColorizer = mDefaultTabColorizer ;
199202
200203 // Thick colored underline below the current selection
201204 if (childCount > 0 && mSelectedPosition < childCount ) {
@@ -249,10 +252,9 @@ private static int blendColors(int color1, int color2, float ratio) {
249252 return Color .rgb ((int ) r , (int ) g , (int ) b );
250253 }
251254
252- private static class SimpleTabColorizer implements TabLayout . TabColorizer {
255+ private static class SimpleTabColorizer {
253256 private int [] mIndicatorColors ;
254257
255- @ Override
256258 public final int getIndicatorColor (int position ) {
257259 return mIndicatorColors [position % mIndicatorColors .length ];
258260 }
0 commit comments