1010import android .graphics .Typeface ;
1111import android .os .Handler ;
1212import android .os .Looper ;
13+ import android .os .SystemClock ;
1314import android .text .SpannableStringBuilder ;
1415import android .text .Spanned ;
1516import android .text .StaticLayout ;
3031import androidx .appcompat .widget .AppCompatTextView ;
3132import androidx .core .content .ContextCompat ;
3233
33- public class TextViewReadMore2 extends AppCompatTextView {
34+ public class TextViewReadMore extends AppCompatTextView {
3435
3536 private static final String DEFAULT_EXPAND_TEXT = "Read More" ;
3637 private static final String DEFAULT_COLLAPSE_TEXT = "Close" ;
@@ -57,7 +58,7 @@ public class TextViewReadMore2 extends AppCompatTextView {
5758 private boolean isAnimate = false ;
5859 private boolean isEllipsized = false ;
5960 private int animationDuration = 200 ;
60- private TextViewReadMoreCallback callback ;
61+ private ToggleListener toggleListener ;
6162
6263 private SpannableStringBuilder spanCollapsed ;
6364 private SpannableStringBuilder spanExpanded ;
@@ -66,17 +67,19 @@ public class TextViewReadMore2 extends AppCompatTextView {
6667 private View .OnClickListener onClickCollapse ;
6768 private int actionClickColor = 0 ;
6869
69- public TextViewReadMore2 (@ NonNull Context context ) {
70+ private static long mLastClickTime = 0 ;
71+
72+ public TextViewReadMore (@ NonNull Context context ) {
7073 super (context );
7174 init (context , null , 0 );
7275 }
7376
74- public TextViewReadMore2 (@ NonNull Context context , @ Nullable AttributeSet attrs ) {
77+ public TextViewReadMore (@ NonNull Context context , @ Nullable AttributeSet attrs ) {
7578 super (context , attrs );
7679 init (context , attrs , 0 );
7780 }
7881
79- public TextViewReadMore2 (@ NonNull Context context , @ Nullable AttributeSet attrs , int defStyleAttr ) {
82+ public TextViewReadMore (@ NonNull Context context , @ Nullable AttributeSet attrs , int defStyleAttr ) {
8083 super (context , attrs , defStyleAttr );
8184 init (context , attrs , defStyleAttr );
8285 }
@@ -85,30 +88,30 @@ private void init(Context context, AttributeSet attrs, int defStyleAttr) {
8588 Resources .Theme theme = context .getTheme ();
8689 if (theme != null ) {
8790 TypedArray typedArray = theme .obtainStyledAttributes (
88- attrs , R .styleable .TextViewReadMore2 , defStyleAttr , 0
91+ attrs , R .styleable .TextViewReadMore , defStyleAttr , 0
8992 );
9093 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 );
9396 maxLines = Math .max (getMaxLines , 1 );
94- collapsed = typedArray .getBoolean (R .styleable .TextViewReadMore2_collapsed , true );
97+ collapsed = typedArray .getBoolean (R .styleable .TextViewReadMore_collapsed , true );
9598
96- String getExpandText = typedArray .getString (R .styleable .TextViewReadMore2_expandText );
99+ String getExpandText = typedArray .getString (R .styleable .TextViewReadMore_expandText );
97100 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 );
101104
102- String getCollapseText = typedArray .getString (R .styleable .TextViewReadMore2_collapseText );
105+ String getCollapseText = typedArray .getString (R .styleable .TextViewReadMore_collapseText );
103106 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 );
107110
108111 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 );
110113
111- int getAnimationDuration = typedArray .getInt (R .styleable .TextViewReadMore2_android_animationDuration , animationDuration );
114+ int getAnimationDuration = typedArray .getInt (R .styleable .TextViewReadMore_android_animationDuration , animationDuration );
112115 if (getAnimationDuration > 1000 ) {
113116 animationDuration = 1000 ;
114117 } else animationDuration = Math .max (getAnimationDuration , 100 );
@@ -311,11 +314,14 @@ public void updateDrawState(@NonNull TextPaint ds) {
311314 invalidate ();
312315 }
313316 }, start , end , Spanned .SPAN_INCLUSIVE_EXCLUSIVE );
314- //setMovementMethod(LinkMovementMethod.getInstance());
315317 return span ;
316318 }
317319
318320 public void toggle () {
321+ if (SystemClock .elapsedRealtime () - mLastClickTime <= animationDuration ) { // 1000 = 1second
322+ return ;
323+ }
324+ mLastClickTime = SystemClock .elapsedRealtime ();
319325 setMovementMethod (null );
320326 int start = collapsed ? halfHeight : fullHeight ;
321327 int end = collapsed ? fullHeight : halfHeight ;
@@ -341,7 +347,7 @@ public void onAnimationEnd(Animator animation) {
341347 collapsed = !collapsed ;
342348 rebuild = true ;
343349 new Handler (Looper .getMainLooper ()).postDelayed (() -> {
344- if (callback != null ) callback . actionListener (collapsed );
350+ if (toggleListener != null ) toggleListener . onToggle (collapsed );
345351 }, 100 );
346352 }
347353 });
@@ -367,8 +373,8 @@ private void underlineText(SpannableStringBuilder builder, int start, int end) {
367373 );
368374 }
369375
370- public void actionListener ( TextViewReadMoreCallback callback ) {
371- this .callback = callback ;
376+ public void toggleListener ( ToggleListener toggleListener ) {
377+ this .toggleListener = toggleListener ;
372378 }
373379 public void onClickExpand (View .OnClickListener onClickExpand ) {
374380 this .onClickExpand = onClickExpand ;
@@ -381,4 +387,8 @@ private void debug(String message) {
381387 Log .d ("TextViewReadMore" , message );
382388 }
383389
390+ public interface ToggleListener {
391+ public void onToggle (boolean collapsed );
392+ }
393+
384394}
0 commit comments