Skip to content

Commit ca48f66

Browse files
committed
if fully expanded, set height to WRAP_CONTENT, because when rotating the device the height calculated with ValueAnimator isn't correct anymore (same for collapsing) + added some more documentation
1 parent 99f4ca9 commit ca48f66

1 file changed

Lines changed: 45 additions & 11 deletions

File tree

expandabletextview/src/main/java/at/blogc/android/views/ExpandableTextView.java

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class ExpandableTextView extends TextView
4747
private long animationDuration;
4848
private boolean animating;
4949
private boolean expanded;
50-
private int originalHeight;
50+
private int collapsedHeight;
5151

5252
public ExpandableTextView(final Context context)
5353
{
@@ -129,28 +129,29 @@ public boolean expand()
129129
this.onExpandListener.onExpand(this);
130130
}
131131

132-
// get original height
132+
// get collapsed height
133133
this.measure
134134
(
135135
MeasureSpec.makeMeasureSpec(this.getMeasuredWidth(), MeasureSpec.EXACTLY),
136136
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
137137
);
138138

139-
this.originalHeight = this.getMeasuredHeight();
139+
this.collapsedHeight = this.getMeasuredHeight();
140140

141-
// set maxLines to MAX Integer
141+
// set maxLines to MAX Integer, so we can calculate the expanded height
142142
this.setMaxLines(Integer.MAX_VALUE);
143143

144-
// get new height
144+
// get expanded height
145145
this.measure
146146
(
147147
MeasureSpec.makeMeasureSpec(this.getMeasuredWidth(), MeasureSpec.EXACTLY),
148148
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
149149
);
150150

151-
final int fullHeight = this.getMeasuredHeight();
151+
final int expandedHeight = this.getMeasuredHeight();
152152

153-
final ValueAnimator valueAnimator = ValueAnimator.ofInt(this.originalHeight, fullHeight);
153+
// animate from collapsed height to expanded height
154+
final ValueAnimator valueAnimator = ValueAnimator.ofInt(this.collapsedHeight, expandedHeight);
154155
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
155156
{
156157
@Override
@@ -161,11 +162,19 @@ public void onAnimationUpdate(final ValueAnimator animation)
161162
ExpandableTextView.this.setLayoutParams(layoutParams);
162163
}
163164
});
165+
164166
valueAnimator.addListener(new AnimatorListenerAdapter()
165167
{
166168
@Override
167169
public void onAnimationEnd(final Animator animation)
168170
{
171+
// if fully expanded, set height to WRAP_CONTENT, because when rotating the device
172+
// the height calculated with this ValueAnimator isn't correct anymore
173+
final ViewGroup.LayoutParams layoutParams = ExpandableTextView.this.getLayoutParams();
174+
layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
175+
ExpandableTextView.this.setLayoutParams(layoutParams);
176+
177+
// keep track of current status
169178
ExpandableTextView.this.expanded = true;
170179
ExpandableTextView.this.animating = false;
171180
}
@@ -201,10 +210,14 @@ public boolean collapse()
201210
this.onExpandListener.onCollapse(this);
202211
}
203212

204-
// get new height
205-
final int fullHeight = this.getMeasuredHeight();
213+
// get expanded height
214+
final int expandedHeight = this.getMeasuredHeight();
215+
216+
// get collapsed height
217+
//TODO
206218

207-
final ValueAnimator valueAnimator = ValueAnimator.ofInt(fullHeight, this.originalHeight);
219+
// animate from expanded height to collapsed height
220+
final ValueAnimator valueAnimator = ValueAnimator.ofInt(expandedHeight, this.collapsedHeight);
208221
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
209222
{
210223
@Override
@@ -215,6 +228,7 @@ public void onAnimationUpdate(final ValueAnimator animation)
215228
ExpandableTextView.this.setLayoutParams(layoutParams);
216229
}
217230
});
231+
218232
valueAnimator.addListener(new AnimatorListenerAdapter()
219233
{
220234
@Override
@@ -223,6 +237,13 @@ public void onAnimationEnd(final Animator animation)
223237
// set maxLines to original value
224238
ExpandableTextView.this.setMaxLines(ExpandableTextView.this.maxLines);
225239

240+
// if fully collapsed, set height to WRAP_CONTENT, because when rotating the device
241+
// the height calculated with this ValueAnimator isn't correct anymore
242+
final ViewGroup.LayoutParams layoutParams = ExpandableTextView.this.getLayoutParams();
243+
layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
244+
ExpandableTextView.this.setLayoutParams(layoutParams);
245+
246+
// keep track of current status
226247
ExpandableTextView.this.expanded = false;
227248
ExpandableTextView.this.animating = false;
228249
}
@@ -266,7 +287,7 @@ public void setOnExpandListener(final OnExpandListener onExpandListener)
266287
*/
267288
public OnExpandListener getOnExpandListener()
268289
{
269-
return onExpandListener;
290+
return this.onExpandListener;
270291
}
271292

272293
/**
@@ -324,9 +345,22 @@ public boolean isExpanded()
324345
return this.expanded;
325346
}
326347

348+
/**
349+
* Interface definition for a callback to be invoked when
350+
* a {@link ExpandableTextView} is expanded or collapsed.
351+
*/
327352
public interface OnExpandListener
328353
{
354+
/**
355+
* The {@link ExpandableTextView} is being expanded.
356+
* @param view the textview
357+
*/
329358
void onExpand(ExpandableTextView view);
359+
360+
/**
361+
* The {@link ExpandableTextView} is being collapsed.
362+
* @param view the textview
363+
*/
330364
void onCollapse(ExpandableTextView view);
331365
}
332366
}

0 commit comments

Comments
 (0)