-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathExpandableTextView.java
More file actions
476 lines (402 loc) · 15.2 KB
/
ExpandableTextView.java
File metadata and controls
476 lines (402 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
package at.blogc.android.views;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.TimeInterpolator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import at.blogc.expandabletextview.BuildConfig;
import at.blogc.expandabletextview.R;
/**
* Copyright (C) 2017 Cliff Ophalvens (Blogc.at)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author Cliff Ophalvens (Blogc.at)
*/
public class ExpandableTextView extends TextView
{
private final List<OnExpandListener> onExpandListeners;
private TimeInterpolator expandInterpolator;
private TimeInterpolator collapseInterpolator;
private final int maxLines;
private long animationDuration;
private boolean animating;
private boolean expanded;
private int collapsedHeight;
public ExpandableTextView(final Context context)
{
this(context, null);
}
public ExpandableTextView(final Context context, @Nullable final AttributeSet attrs)
{
this(context, attrs, 0);
}
public ExpandableTextView(final Context context, @Nullable final AttributeSet attrs, final int defStyle)
{
super(context, attrs, defStyle);
// read attributes
final TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.ExpandableTextView, defStyle, 0);
this.animationDuration = attributes.getInt(R.styleable.ExpandableTextView_animation_duration, BuildConfig.DEFAULT_ANIMATION_DURATION);
attributes.recycle();
// keep the original value of maxLines
this.maxLines = this.getMaxLines();
// create bucket of OnExpandListener instances
this.onExpandListeners = new ArrayList<>();
// create default interpolators
this.expandInterpolator = new AccelerateDecelerateInterpolator();
this.collapseInterpolator = new AccelerateDecelerateInterpolator();
}
@Override
protected void onMeasure(final int widthMeasureSpec, int heightMeasureSpec)
{
// if this TextView is collapsed and maxLines = 0,
// than make its height equals to zero
if (this.maxLines == 0 && !this.expanded && !this.animating)
{
heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.EXACTLY);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
//region public helper methods
/**
* Toggle the expanded state of this {@link ExpandableTextView}.
* @return true if toggled, false otherwise.
*/
public boolean toggle()
{
return this.expanded
? this.collapse()
: this.expand();
}
/**
* Expand this {@link ExpandableTextView}.
* @return true if expanded, false otherwise.
*/
public boolean expand()
{
if (!this.expanded && !this.animating && this.maxLines >= 0)
{
// notify listener
this.notifyOnExpand();
// measure collapsed height
this.measure
(
MeasureSpec.makeMeasureSpec(this.getMeasuredWidth(), MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
);
this.collapsedHeight = this.getMeasuredHeight();
// indicate that we are now animating
this.animating = true;
// set maxLines to MAX Integer, so we can calculate the expanded height
this.setMaxLines(Integer.MAX_VALUE);
// measure expanded height
this.measure
(
MeasureSpec.makeMeasureSpec(this.getMeasuredWidth(), MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
);
final int expandedHeight = this.getMeasuredHeight();
// animate from collapsed height to expanded height
final ValueAnimator valueAnimator = ValueAnimator.ofInt(this.collapsedHeight, expandedHeight);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
{
@Override
public void onAnimationUpdate(final ValueAnimator animation)
{
ExpandableTextView.this.setHeight((int) animation.getAnimatedValue());
}
});
// wait for the animation to end
valueAnimator.addListener(new AnimatorListenerAdapter()
{
@Override
public void onAnimationEnd(final Animator animation)
{
// reset min & max height (previously set with setHeight() method)
ExpandableTextView.this.setMaxHeight(Integer.MAX_VALUE);
ExpandableTextView.this.setMinHeight(0);
// if fully expanded, set height to WRAP_CONTENT, because when rotating the device
// the height calculated with this ValueAnimator isn't correct anymore
final ViewGroup.LayoutParams layoutParams = ExpandableTextView.this.getLayoutParams();
layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
ExpandableTextView.this.setLayoutParams(layoutParams);
// keep track of current status
ExpandableTextView.this.expanded = true;
ExpandableTextView.this.animating = false;
}
});
// set interpolator
valueAnimator.setInterpolator(this.expandInterpolator);
// start the animation
valueAnimator
.setDuration(this.animationDuration)
.start();
return true;
}
return false;
}
/**
* Collapse this {@link TextView}.
* @return true if collapsed, false otherwise.
*/
public boolean collapse()
{
if (this.expanded && !this.animating && this.maxLines >= 0)
{
// notify listener
this.notifyOnCollapse();
// measure expanded height
final int expandedHeight = this.getMeasuredHeight();
// indicate that we are now animating
this.animating = true;
// animate from expanded height to collapsed height
final ValueAnimator valueAnimator = ValueAnimator.ofInt(expandedHeight, this.collapsedHeight);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
{
@Override
public void onAnimationUpdate(final ValueAnimator animation)
{
ExpandableTextView.this.setHeight((int) animation.getAnimatedValue());
}
});
// wait for the animation to end
valueAnimator.addListener(new AnimatorListenerAdapter()
{
@Override
public void onAnimationEnd(final Animator animation)
{
// keep track of current status
ExpandableTextView.this.expanded = false;
ExpandableTextView.this.animating = false;
// set maxLines back to original value
ExpandableTextView.this.setMaxLines(ExpandableTextView.this.maxLines);
// if fully collapsed, set height back to WRAP_CONTENT, because when rotating the device
// the height previously calculated with this ValueAnimator isn't correct anymore
final ViewGroup.LayoutParams layoutParams = ExpandableTextView.this.getLayoutParams();
layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
ExpandableTextView.this.setLayoutParams(layoutParams);
}
});
// set interpolator
valueAnimator.setInterpolator(this.collapseInterpolator);
// start the animation
valueAnimator
.setDuration(this.animationDuration)
.start();
return true;
}
return false;
}
//endregion
//region public getters and setters
/**
* Sets the duration of the expand / collapse animation.
* @param animationDuration duration in milliseconds.
*/
public void setAnimationDuration(final long animationDuration)
{
this.animationDuration = animationDuration;
}
/**
* Adds a listener which receives updates about this {@link ExpandableTextView}.
* @param onExpandListener the listener.
*/
public void addOnExpandListener(final OnExpandListener onExpandListener)
{
this.onExpandListeners.add(onExpandListener);
}
/**
* Removes a listener which receives updates about this {@link ExpandableTextView}.
* @param onExpandListener the listener.
*/
public void removeOnExpandListener(final OnExpandListener onExpandListener)
{
this.onExpandListeners.remove(onExpandListener);
}
/**
* Sets a {@link TimeInterpolator} for expanding and collapsing.
* @param interpolator the interpolator
*/
public void setInterpolator(final TimeInterpolator interpolator)
{
this.expandInterpolator = interpolator;
this.collapseInterpolator = interpolator;
}
/**
* Sets a {@link TimeInterpolator} for expanding.
* @param expandInterpolator the interpolator
*/
public void setExpandInterpolator(final TimeInterpolator expandInterpolator)
{
this.expandInterpolator = expandInterpolator;
}
/**
* Returns the current {@link TimeInterpolator} for expanding.
* @return the current interpolator, null by default.
*/
public TimeInterpolator getExpandInterpolator()
{
return this.expandInterpolator;
}
/**
* Sets a {@link TimeInterpolator} for collpasing.
* @param collapseInterpolator the interpolator
*/
public void setCollapseInterpolator(final TimeInterpolator collapseInterpolator)
{
this.collapseInterpolator = collapseInterpolator;
}
/**
* Returns the current {@link TimeInterpolator} for collapsing.
* @return the current interpolator, null by default.
*/
public TimeInterpolator getCollapseInterpolator()
{
return this.collapseInterpolator;
}
/**
* Is this {@link ExpandableTextView} expanded or not?
* @return true if expanded, false if collapsed.
*/
public boolean isExpanded()
{
return this.expanded;
}
//endregion
/**
* This method will notify the listener about this view being expanded.
*/
private void notifyOnCollapse()
{
for (final OnExpandListener onExpandListener : this.onExpandListeners)
{
onExpandListener.onCollapse(this);
}
}
/**
* This method will notify the listener about this view being collapsed.
*/
private void notifyOnExpand()
{
for (final OnExpandListener onExpandListener : this.onExpandListeners)
{
onExpandListener.onExpand(this);
}
}
//region public interfaces
/**
* Interface definition for a callback to be invoked when
* a {@link ExpandableTextView} is expanded or collapsed.
*/
public interface OnExpandListener
{
/**
* The {@link ExpandableTextView} is being expanded.
* @param view the textview
*/
void onExpand(@NonNull ExpandableTextView view);
/**
* The {@link ExpandableTextView} is being collapsed.
* @param view the textview
*/
void onCollapse(@NonNull ExpandableTextView view);
}
/**
* Simple implementation of the {@link OnExpandListener} interface with stub
* implementations of each method. Extend this if you do not intend to override
* every method of {@link OnExpandListener}.
*/
public static class SimpleOnExpandListener implements OnExpandListener
{
@Override
public void onExpand(@NonNull final ExpandableTextView view)
{
// empty implementation
}
@Override
public void onCollapse(@NonNull final ExpandableTextView view)
{
// empty implementation
}
}
//endregion
@Override
public Parcelable onSaveInstanceState() {
// Boilerplate code that allows parent classes to save state
Parcelable superState = super.onSaveInstanceState();
SavedState ss = new SavedState(superState);
//end
ss.isExpanded = this.isExpanded();
return ss;
}
@Override
public void onRestoreInstanceState(Parcelable state) {
//begin boilerplate code so parent classes can restore state
if(!(state instanceof SavedState)) {
super.onRestoreInstanceState(state);
return;
}
SavedState ss = (SavedState)state;
super.onRestoreInstanceState(ss.getSuperState());
//end
if (ss.isExpanded) {
// Temporarily setting animation duration to 0 to make allow for a smoother UI experience.
setAnimationDuration(0);
expand();
setAnimationDuration(animationDuration);
}
}
/**
* User interface state stored by {@link ExpandableTextView} for saving View state on
* {@code configChanges}.
*
* @see #onSaveInstanceState()
* @see #onRestoreInstanceState(Parcelable)
* */
public static class SavedState extends BaseSavedState {
boolean isExpanded;
SavedState(Parcelable superState) {
super(superState);
}
private SavedState(Parcel in) {
super(in);
this.isExpanded = in.readInt() == 1;
}
@Override
public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags);
out.writeInt(this.isExpanded ? 1 : 0);
}
// required field that makes Parcelables from a Parcel
public static final Parcelable.Creator<SavedState> CREATOR =
new Parcelable.Creator<SavedState>() {
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}
}