10
10
import android .graphics .Typeface ;
11
11
import android .os .Handler ;
12
12
import android .os .Looper ;
13
+ import android .os .SystemClock ;
13
14
import android .text .SpannableStringBuilder ;
14
15
import android .text .Spanned ;
15
16
import android .text .StaticLayout ;
30
31
import androidx .appcompat .widget .AppCompatTextView ;
31
32
import androidx .core .content .ContextCompat ;
32
33
33
- public class TextViewReadMore2 extends AppCompatTextView {
34
+ public class TextViewReadMore extends AppCompatTextView {
34
35
35
36
private static final String DEFAULT_EXPAND_TEXT = "Read More" ;
36
37
private static final String DEFAULT_COLLAPSE_TEXT = "Close" ;
@@ -57,7 +58,7 @@ public class TextViewReadMore2 extends AppCompatTextView {
57
58
private boolean isAnimate = false ;
58
59
private boolean isEllipsized = false ;
59
60
private int animationDuration = 200 ;
60
- private TextViewReadMoreCallback callback ;
61
+ private ToggleListener toggleListener ;
61
62
62
63
private SpannableStringBuilder spanCollapsed ;
63
64
private SpannableStringBuilder spanExpanded ;
@@ -66,17 +67,19 @@ public class TextViewReadMore2 extends AppCompatTextView {
66
67
private View .OnClickListener onClickCollapse ;
67
68
private int actionClickColor = 0 ;
68
69
69
- public TextViewReadMore2 (@ NonNull Context context ) {
70
+ private static long mLastClickTime = 0 ;
71
+
72
+ public TextViewReadMore (@ NonNull Context context ) {
70
73
super (context );
71
74
init (context , null , 0 );
72
75
}
73
76
74
- public TextViewReadMore2 (@ NonNull Context context , @ Nullable AttributeSet attrs ) {
77
+ public TextViewReadMore (@ NonNull Context context , @ Nullable AttributeSet attrs ) {
75
78
super (context , attrs );
76
79
init (context , attrs , 0 );
77
80
}
78
81
79
- public TextViewReadMore2 (@ NonNull Context context , @ Nullable AttributeSet attrs , int defStyleAttr ) {
82
+ public TextViewReadMore (@ NonNull Context context , @ Nullable AttributeSet attrs , int defStyleAttr ) {
80
83
super (context , attrs , defStyleAttr );
81
84
init (context , attrs , defStyleAttr );
82
85
}
@@ -85,30 +88,30 @@ private void init(Context context, AttributeSet attrs, int defStyleAttr) {
85
88
Resources .Theme theme = context .getTheme ();
86
89
if (theme != null ) {
87
90
TypedArray typedArray = theme .obtainStyledAttributes (
88
- attrs , R .styleable .TextViewReadMore2 , defStyleAttr , 0
91
+ attrs , R .styleable .TextViewReadMore , defStyleAttr , 0
89
92
);
90
93
try {
91
- text = typedArray .getString (R .styleable .TextViewReadMore2_android_text );
92
- int getMaxLines = typedArray .getInt (R .styleable .TextViewReadMore2_readMoreMaxLines , 2 );
94
+ text = typedArray .getString (R .styleable .TextViewReadMore_android_text );
95
+ int getMaxLines = typedArray .getInt (R .styleable .TextViewReadMore_readMoreMaxLines , 2 );
93
96
maxLines = Math .max (getMaxLines , 1 );
94
- collapsed = typedArray .getBoolean (R .styleable .TextViewReadMore2_collapsed , true );
97
+ collapsed = typedArray .getBoolean (R .styleable .TextViewReadMore_collapsed , true );
95
98
96
- String getExpandText = typedArray .getString (R .styleable .TextViewReadMore2_expandText );
99
+ String getExpandText = typedArray .getString (R .styleable .TextViewReadMore_expandText );
97
100
expandText = TextUtils .isEmpty (getExpandText ) ? DEFAULT_EXPAND_TEXT : getExpandText ;
98
- expandTextColor = typedArray .getColor (R .styleable .TextViewReadMore2_expandTextColor , Color .BLUE );
99
- expandTextStyle = typedArray .getInt (R .styleable .TextViewReadMore2_expandTextStyle , 0 );
100
- expandTextUnderline = typedArray .getBoolean (R .styleable .TextViewReadMore2_expandTextUnderline , expandTextUnderline );
101
+ expandTextColor = typedArray .getColor (R .styleable .TextViewReadMore_expandTextColor , Color .BLUE );
102
+ expandTextStyle = typedArray .getInt (R .styleable .TextViewReadMore_expandTextStyle , 0 );
103
+ expandTextUnderline = typedArray .getBoolean (R .styleable .TextViewReadMore_expandTextUnderline , expandTextUnderline );
101
104
102
- String getCollapseText = typedArray .getString (R .styleable .TextViewReadMore2_collapseText );
105
+ String getCollapseText = typedArray .getString (R .styleable .TextViewReadMore_collapseText );
103
106
collapseText = getCollapseText == null ? DEFAULT_COLLAPSE_TEXT : getCollapseText ;
104
- collapseTextColor = typedArray .getColor (R .styleable .TextViewReadMore2_collapseTextColor , Color .BLUE );
105
- collapseTextStyle = typedArray .getInt (R .styleable .TextViewReadMore2_collapseTextStyle , 0 );
106
- collapseTextUnderline = typedArray .getBoolean (R .styleable .TextViewReadMore2_collapseTextUnderline , collapseTextUnderline );
107
+ collapseTextColor = typedArray .getColor (R .styleable .TextViewReadMore_collapseTextColor , Color .BLUE );
108
+ collapseTextStyle = typedArray .getInt (R .styleable .TextViewReadMore_collapseTextStyle , 0 );
109
+ collapseTextUnderline = typedArray .getBoolean (R .styleable .TextViewReadMore_collapseTextUnderline , collapseTextUnderline );
107
110
108
111
int defaultActionClickColor = ContextCompat .getColor (context , R .color .text_view_read_more_button_hover_color );
109
- actionClickColor = typedArray .getColor (R .styleable .TextViewReadMore2_actionClickColor , defaultActionClickColor );
112
+ actionClickColor = typedArray .getColor (R .styleable .TextViewReadMore_actionClickColor , defaultActionClickColor );
110
113
111
- int getAnimationDuration = typedArray .getInt (R .styleable .TextViewReadMore2_android_animationDuration , animationDuration );
114
+ int getAnimationDuration = typedArray .getInt (R .styleable .TextViewReadMore_android_animationDuration , animationDuration );
112
115
if (getAnimationDuration > 1000 ) {
113
116
animationDuration = 1000 ;
114
117
} else animationDuration = Math .max (getAnimationDuration , 100 );
@@ -311,11 +314,14 @@ public void updateDrawState(@NonNull TextPaint ds) {
311
314
invalidate ();
312
315
}
313
316
}, start , end , Spanned .SPAN_INCLUSIVE_EXCLUSIVE );
314
- //setMovementMethod(LinkMovementMethod.getInstance());
315
317
return span ;
316
318
}
317
319
318
320
public void toggle () {
321
+ if (SystemClock .elapsedRealtime () - mLastClickTime <= animationDuration ) { // 1000 = 1second
322
+ return ;
323
+ }
324
+ mLastClickTime = SystemClock .elapsedRealtime ();
319
325
setMovementMethod (null );
320
326
int start = collapsed ? halfHeight : fullHeight ;
321
327
int end = collapsed ? fullHeight : halfHeight ;
@@ -341,7 +347,7 @@ public void onAnimationEnd(Animator animation) {
341
347
collapsed = !collapsed ;
342
348
rebuild = true ;
343
349
new Handler (Looper .getMainLooper ()).postDelayed (() -> {
344
- if (callback != null ) callback . actionListener (collapsed );
350
+ if (toggleListener != null ) toggleListener . onToggle (collapsed );
345
351
}, 100 );
346
352
}
347
353
});
@@ -367,8 +373,8 @@ private void underlineText(SpannableStringBuilder builder, int start, int end) {
367
373
);
368
374
}
369
375
370
- public void actionListener ( TextViewReadMoreCallback callback ) {
371
- this .callback = callback ;
376
+ public void toggleListener ( ToggleListener toggleListener ) {
377
+ this .toggleListener = toggleListener ;
372
378
}
373
379
public void onClickExpand (View .OnClickListener onClickExpand ) {
374
380
this .onClickExpand = onClickExpand ;
@@ -381,4 +387,8 @@ private void debug(String message) {
381
387
Log .d ("TextViewReadMore" , message );
382
388
}
383
389
390
+ public interface ToggleListener {
391
+ public void onToggle (boolean collapsed );
392
+ }
393
+
384
394
}
0 commit comments