Skip to content

Commit

Permalink
Merge branch 'release/1.0.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
Cliffus committed Jun 19, 2016
2 parents fb45036 + fb04b56 commit 0de20df
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 21 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ Initial version of the ExpandableTextView
1.0.1
-----
Added support for Interpolators + update demo Activity

1.0.2
-----
Fixed some issues related to rotating a device from portrait to landscape (and the other way around)
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Demo
----
This repository also contains a demo project.

![Demo](https://raw.githubusercontent.com/Blogcat/Android-ExpandableTextView/release/1.0.1/demo.gif)
![Demo](https://raw.githubusercontent.com/Blogcat/Android-ExpandableTextView/release/1.0.2/demo.gif)

Add dependency
--------------
Expand All @@ -32,7 +32,7 @@ library dependency

```groovy
dependencies {
compile ('at.blogc:expandabletextview:1.0.1@aar')
compile ('at.blogc:expandabletextview:1.0.2@aar')
}
```

Expand Down Expand Up @@ -134,6 +134,7 @@ expandableTextView.setOnExpandListener(new ExpandableTextView.OnExpandListener()
Roadmap
=======

* add method to know if the TextView is expandable or not
* optional fading edge at the bottom of the TextView
* update demo project with more examples

Expand Down
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
android:theme="@style/AppTheme"
tools:ignore="AllowBackup">

<activity android:name="at.blogc.android.activities.MainActivity">
<activity
android:name="at.blogc.android.activities.MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize">

<intent-filter>
<action android:name="android.intent.action.MAIN"/>
Expand Down
2 changes: 1 addition & 1 deletion expandabletextview/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'

def fullVersion = '1.0.1'
def fullVersion = '1.0.2'

group = 'at.blogc'
version = fullVersion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class ExpandableTextView extends TextView
private long animationDuration;
private boolean animating;
private boolean expanded;
private int originalHeight;
private int collapsedHeight;

public ExpandableTextView(final Context context)
{
Expand Down Expand Up @@ -108,12 +108,9 @@ public int getMaxLines()
*/
public boolean toggle()
{
if (this.expanded)
{
return this.collapse();
}

return this.expand();
return this.expanded
? this.collapse()
: this.expand();
}

/**
Expand All @@ -132,28 +129,29 @@ public boolean expand()
this.onExpandListener.onExpand(this);
}

// get original height
// get collapsed height
this.measure
(
MeasureSpec.makeMeasureSpec(this.getMeasuredWidth(), MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
);

this.originalHeight = this.getMeasuredHeight();
this.collapsedHeight = this.getMeasuredHeight();

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

// get new height
// get expanded height
this.measure
(
MeasureSpec.makeMeasureSpec(this.getMeasuredWidth(), MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
);

final int fullHeight = this.getMeasuredHeight();
final int expandedHeight = this.getMeasuredHeight();

final ValueAnimator valueAnimator = ValueAnimator.ofInt(this.originalHeight, fullHeight);
// animate from collapsed height to expanded height
final ValueAnimator valueAnimator = ValueAnimator.ofInt(this.collapsedHeight, expandedHeight);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
{
@Override
Expand All @@ -164,11 +162,19 @@ public void onAnimationUpdate(final ValueAnimator animation)
ExpandableTextView.this.setLayoutParams(layoutParams);
}
});

valueAnimator.addListener(new AnimatorListenerAdapter()
{
@Override
public void onAnimationEnd(final Animator animation)
{
// 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;
}
Expand Down Expand Up @@ -204,10 +210,11 @@ public boolean collapse()
this.onExpandListener.onCollapse(this);
}

// get new height
final int fullHeight = this.getMeasuredHeight();
// get expanded height
final int expandedHeight = this.getMeasuredHeight();

final ValueAnimator valueAnimator = ValueAnimator.ofInt(fullHeight, this.originalHeight);
// animate from expanded height to collapsed height
final ValueAnimator valueAnimator = ValueAnimator.ofInt(expandedHeight, this.collapsedHeight);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
{
@Override
Expand All @@ -218,6 +225,7 @@ public void onAnimationUpdate(final ValueAnimator animation)
ExpandableTextView.this.setLayoutParams(layoutParams);
}
});

valueAnimator.addListener(new AnimatorListenerAdapter()
{
@Override
Expand All @@ -226,6 +234,13 @@ public void onAnimationEnd(final Animator animation)
// set maxLines to original value
ExpandableTextView.this.setMaxLines(ExpandableTextView.this.maxLines);

// if fully collapsed, 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 = false;
ExpandableTextView.this.animating = false;
}
Expand Down Expand Up @@ -269,7 +284,7 @@ public void setOnExpandListener(final OnExpandListener onExpandListener)
*/
public OnExpandListener getOnExpandListener()
{
return onExpandListener;
return this.onExpandListener;
}

/**
Expand Down Expand Up @@ -327,9 +342,22 @@ public boolean isExpanded()
return this.expanded;
}

/**
* 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(ExpandableTextView view);

/**
* The {@link ExpandableTextView} is being collapsed.
* @param view the textview
*/
void onCollapse(ExpandableTextView view);
}
}

0 comments on commit 0de20df

Please sign in to comment.