30
30
31
31
import java .util .ArrayList ;
32
32
33
+ /**
34
+ * @Author: AriesHoo on 2018/11/30 11:22
35
+ * @E-Mail: AriesHoo@126.com
36
+ * @Function:
37
+ * @Description: 4、2018年11月30日11:18:41 修改原作者 https://github.com/H07000223/FlycoTabLayout 选中粗体当初始化选中第一项不生效BUG
38
+ * * * {@link #updateTabStyles()}
39
+ */
33
40
public class SegmentTabLayout extends FrameLayout implements ValueAnimator .AnimatorUpdateListener {
34
41
private Context mContext ;
35
42
private String [] mTitles ;
36
43
private LinearLayout mTabsContainer ;
37
44
private int mCurrentTab ;
38
45
private int mLastTab ;
39
46
private int mTabCount ;
40
- /** 用于绘制显示器 */
47
+ /**
48
+ * 用于绘制显示器
49
+ */
41
50
private Rect mIndicatorRect = new Rect ();
42
51
private GradientDrawable mIndicatorDrawable = new GradientDrawable ();
43
52
private GradientDrawable mRectDrawable = new GradientDrawable ();
@@ -48,7 +57,9 @@ public class SegmentTabLayout extends FrameLayout implements ValueAnimator.Anima
48
57
private boolean mTabSpaceEqual ;
49
58
private float mTabWidth ;
50
59
51
- /** indicator */
60
+ /**
61
+ * indicator
62
+ */
52
63
private int mIndicatorColor ;
53
64
private float mIndicatorHeight ;
54
65
private float mIndicatorCornerRadius ;
@@ -60,19 +71,23 @@ public class SegmentTabLayout extends FrameLayout implements ValueAnimator.Anima
60
71
private boolean mIndicatorAnimEnable ;
61
72
private boolean mIndicatorBounceEnable ;
62
73
63
- /** divider */
74
+ /**
75
+ * divider
76
+ */
64
77
private int mDividerColor ;
65
78
private float mDividerWidth ;
66
79
private float mDividerPadding ;
67
80
68
- /** title */
81
+ /**
82
+ * title
83
+ */
69
84
private static final int TEXT_BOLD_NONE = 0 ;
70
85
private static final int TEXT_BOLD_WHEN_SELECT = 1 ;
71
86
private static final int TEXT_BOLD_BOTH = 2 ;
72
- private float mTextsize ;
87
+ private float mTextSize ;
73
88
private int mTextSelectColor ;
74
89
private int mTextUnselectColor ;
75
- private int mTextBold ;
90
+ private TextBold mTextBold ;
76
91
private boolean mTextAllCaps ;
77
92
78
93
private int mBarColor ;
@@ -81,7 +96,9 @@ public class SegmentTabLayout extends FrameLayout implements ValueAnimator.Anima
81
96
82
97
private int mHeight ;
83
98
84
- /** anim */
99
+ /**
100
+ * anim
101
+ */
85
102
private ValueAnimator mValueAnimator ;
86
103
private OvershootInterpolator mInterpolator = new OvershootInterpolator (0.8f );
87
104
@@ -143,10 +160,10 @@ private void obtainAttributes(Context context, AttributeSet attrs) {
143
160
mDividerWidth = ta .getDimension (R .styleable .SegmentTabLayout_tl_divider_width , dp2px (1 ));
144
161
mDividerPadding = ta .getDimension (R .styleable .SegmentTabLayout_tl_divider_padding , 0 );
145
162
146
- mTextsize = ta .getDimension (R .styleable .SegmentTabLayout_tl_textsize , sp2px (13f ));
163
+ mTextSize = ta .getDimension (R .styleable .SegmentTabLayout_tl_textsize , sp2px (13f ));
147
164
mTextSelectColor = ta .getColor (R .styleable .SegmentTabLayout_tl_textSelectColor , Color .parseColor ("#ffffff" ));
148
165
mTextUnselectColor = ta .getColor (R .styleable .SegmentTabLayout_tl_textUnselectColor , mIndicatorColor );
149
- mTextBold = ta .getInt (R .styleable .SegmentTabLayout_tl_textBold , TEXT_BOLD_NONE );
166
+ mTextBold = TextBold . valueOf ( ta .getInt (R .styleable .SegmentTabLayout_tl_textBold , TEXT_BOLD_NONE ) );
150
167
mTextAllCaps = ta .getBoolean (R .styleable .SegmentTabLayout_tl_textAllCaps , false );
151
168
152
169
mTabSpaceEqual = ta .getBoolean (R .styleable .SegmentTabLayout_tl_tab_space_equal , true );
@@ -170,13 +187,17 @@ public void setTabData(String[] titles) {
170
187
notifyDataSetChanged ();
171
188
}
172
189
173
- /** 关联数据支持同时切换fragments */
190
+ /**
191
+ * 关联数据支持同时切换fragments
192
+ */
174
193
public void setTabData (String [] titles , FragmentActivity fa , int containerViewId , ArrayList <Fragment > fragments ) {
175
194
mFragmentChangeManager = new FragmentChangeManager (fa .getSupportFragmentManager (), containerViewId , fragments );
176
195
setTabData (titles );
177
196
}
178
197
179
- /** 更新数据 */
198
+ /**
199
+ * 更新数据
200
+ */
180
201
public void notifyDataSetChanged () {
181
202
mTabsContainer .removeAllViews ();
182
203
this .mTabCount = mTitles .length ;
@@ -190,7 +211,9 @@ public void notifyDataSetChanged() {
190
211
updateTabStyles ();
191
212
}
192
213
193
- /** 创建并添加tab */
214
+ /**
215
+ * 创建并添加tab
216
+ */
194
217
private void addTab (final int position , View tabView ) {
195
218
TextView tv_tab_title = (TextView ) tabView .findViewById (R .id .tv_tab_title );
196
219
tv_tab_title .setText (mTitles [position ]);
@@ -226,17 +249,18 @@ private void updateTabStyles() {
226
249
for (int i = 0 ; i < mTabCount ; i ++) {
227
250
View tabView = mTabsContainer .getChildAt (i );
228
251
tabView .setPadding ((int ) mTabPadding , 0 , (int ) mTabPadding , 0 );
229
- TextView tv_tab_title = ( TextView ) tabView .findViewById (R .id .tv_tab_title );
252
+ TextView tv_tab_title = tabView .findViewById (R .id .tv_tab_title );
230
253
tv_tab_title .setTextColor (i == mCurrentTab ? mTextSelectColor : mTextUnselectColor );
231
- tv_tab_title .setTextSize (TypedValue .COMPLEX_UNIT_PX , mTextsize );
232
- // tv_tab_title.setPadding((int) mTabPadding, 0, (int) mTabPadding, 0);
254
+ tv_tab_title .setTextSize (TypedValue .COMPLEX_UNIT_PX , mTextSize );
233
255
if (mTextAllCaps ) {
234
256
tv_tab_title .setText (tv_tab_title .getText ().toString ().toUpperCase ());
235
257
}
236
-
237
- if (mTextBold == TEXT_BOLD_BOTH ) {
258
+ if (mTextBold == TextBold .BOTH ) {
238
259
tv_tab_title .getPaint ().setFakeBoldText (true );
239
- } else if (mTextBold == TEXT_BOLD_NONE ) {
260
+ } else if (mTextBold == TextBold .SELECT ) {
261
+ //增加-以修正原作者第一次选中粗体不生效问题
262
+ tv_tab_title .getPaint ().setFakeBoldText (mCurrentTab == i );
263
+ } else {
240
264
tv_tab_title .getPaint ().setFakeBoldText (false );
241
265
}
242
266
}
@@ -248,7 +272,7 @@ private void updateTabSelection(int position) {
248
272
final boolean isSelect = i == position ;
249
273
TextView tab_title = (TextView ) tabView .findViewById (R .id .tv_tab_title );
250
274
tab_title .setTextColor (isSelect ? mTextSelectColor : mTextUnselectColor );
251
- if (mTextBold == TEXT_BOLD_WHEN_SELECT ) {
275
+ if (mTextBold == TextBold . SELECT ) {
252
276
tab_title .getPaint ().setFakeBoldText (isSelect );
253
277
}
254
278
}
@@ -481,8 +505,8 @@ public void setDividerPadding(float dividerPadding) {
481
505
invalidate ();
482
506
}
483
507
484
- public void setTextsize (float textsize ) {
485
- this .mTextsize = sp2px (textsize );
508
+ public void setTextSize (float textSize ) {
509
+ this .mTextSize = sp2px (textSize );
486
510
updateTabStyles ();
487
511
}
488
512
@@ -496,9 +520,10 @@ public void setTextUnselectColor(int textUnselectColor) {
496
520
updateTabStyles ();
497
521
}
498
522
499
- public void setTextBold (int textBold ) {
523
+ public SegmentTabLayout setTextBold (TextBold textBold ) {
500
524
this .mTextBold = textBold ;
501
525
updateTabStyles ();
526
+ return this ;
502
527
}
503
528
504
529
public void setTextAllCaps (boolean textAllCaps ) {
@@ -578,8 +603,8 @@ public float getDividerPadding() {
578
603
return mDividerPadding ;
579
604
}
580
605
581
- public float getTextsize () {
582
- return mTextsize ;
606
+ public float getTextSize () {
607
+ return mTextSize ;
583
608
}
584
609
585
610
public int getTextSelectColor () {
@@ -590,7 +615,7 @@ public int getTextUnselectColor() {
590
615
return mTextUnselectColor ;
591
616
}
592
617
593
- public int getTextBold () {
618
+ public TextBold getTextBold () {
594
619
return mTextBold ;
595
620
}
596
621
@@ -672,7 +697,7 @@ public void setMsgMargin(int position, float leftPadding, float bottomPadding) {
672
697
MsgView tipView = (MsgView ) tabView .findViewById (R .id .rtv_msg_tip );
673
698
if (tipView != null ) {
674
699
TextView tv_tab_title = (TextView ) tabView .findViewById (R .id .tv_tab_title );
675
- mTextPaint .setTextSize (mTextsize );
700
+ mTextPaint .setTextSize (mTextSize );
676
701
float textWidth = mTextPaint .measureText (tv_tab_title .getText ().toString ());
677
702
float textHeight = mTextPaint .descent () - mTextPaint .ascent ();
678
703
MarginLayoutParams lp = (MarginLayoutParams ) tipView .getLayoutParams ();
@@ -684,7 +709,9 @@ public void setMsgMargin(int position, float leftPadding, float bottomPadding) {
684
709
}
685
710
}
686
711
687
- /** 当前类只提供了少许设置未读消息属性的方法,可以通过该方法获取MsgView对象从而各种设置 */
712
+ /**
713
+ * 当前类只提供了少许设置未读消息属性的方法,可以通过该方法获取MsgView对象从而各种设置
714
+ */
688
715
public MsgView getMsgView (int position ) {
689
716
if (position >= mTabCount ) {
690
717
position = mTabCount - 1 ;
@@ -715,7 +742,8 @@ protected void onRestoreInstanceState(Parcelable state) {
715
742
mCurrentTab = bundle .getInt ("mCurrentTab" );
716
743
state = bundle .getParcelable ("instanceState" );
717
744
if (mCurrentTab != 0 && mTabsContainer .getChildCount () > 0 ) {
718
- updateTabSelection (mCurrentTab );
745
+ //updateTabSelection(mCurrentTab); 原作者恢复状态时未将Fragment选中CurrentTab
746
+ setCurrentTab (mCurrentTab );
719
747
}
720
748
}
721
749
super .onRestoreInstanceState (state );
0 commit comments