From 99f4ca907de94a59711ac19b993546fcd8d07c5f Mon Sep 17 00:00:00 2001 From: Cliff Ophalvens Date: Tue, 31 May 2016 15:22:00 +0200 Subject: [PATCH 1/6] simplify if statement --- .../java/at/blogc/android/views/ExpandableTextView.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/expandabletextview/src/main/java/at/blogc/android/views/ExpandableTextView.java b/expandabletextview/src/main/java/at/blogc/android/views/ExpandableTextView.java index 067c9e3..b048e94 100644 --- a/expandabletextview/src/main/java/at/blogc/android/views/ExpandableTextView.java +++ b/expandabletextview/src/main/java/at/blogc/android/views/ExpandableTextView.java @@ -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(); } /** From ca48f665d36860734e93dff022a5b1f7d17f7307 Mon Sep 17 00:00:00 2001 From: Cliff Ophalvens Date: Tue, 31 May 2016 15:55:20 +0200 Subject: [PATCH 2/6] 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 --- .../android/views/ExpandableTextView.java | 56 +++++++++++++++---- 1 file changed, 45 insertions(+), 11 deletions(-) diff --git a/expandabletextview/src/main/java/at/blogc/android/views/ExpandableTextView.java b/expandabletextview/src/main/java/at/blogc/android/views/ExpandableTextView.java index b048e94..46ace0f 100644 --- a/expandabletextview/src/main/java/at/blogc/android/views/ExpandableTextView.java +++ b/expandabletextview/src/main/java/at/blogc/android/views/ExpandableTextView.java @@ -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) { @@ -129,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 @@ -161,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; } @@ -201,10 +210,14 @@ public boolean collapse() this.onExpandListener.onCollapse(this); } - // get new height - final int fullHeight = this.getMeasuredHeight(); + // get expanded height + final int expandedHeight = this.getMeasuredHeight(); + + // get collapsed height + //TODO - 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 @@ -215,6 +228,7 @@ public void onAnimationUpdate(final ValueAnimator animation) ExpandableTextView.this.setLayoutParams(layoutParams); } }); + valueAnimator.addListener(new AnimatorListenerAdapter() { @Override @@ -223,6 +237,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; } @@ -266,7 +287,7 @@ public void setOnExpandListener(final OnExpandListener onExpandListener) */ public OnExpandListener getOnExpandListener() { - return onExpandListener; + return this.onExpandListener; } /** @@ -324,9 +345,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); } } From c5fb734d79c0d97be99daa94ad08d5ac2d5701fd Mon Sep 17 00:00:00 2001 From: Cliff Ophalvens Date: Tue, 14 Jun 2016 13:18:49 +0200 Subject: [PATCH 3/6] remove todo --- .../main/java/at/blogc/android/views/ExpandableTextView.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/expandabletextview/src/main/java/at/blogc/android/views/ExpandableTextView.java b/expandabletextview/src/main/java/at/blogc/android/views/ExpandableTextView.java index 46ace0f..f53d230 100644 --- a/expandabletextview/src/main/java/at/blogc/android/views/ExpandableTextView.java +++ b/expandabletextview/src/main/java/at/blogc/android/views/ExpandableTextView.java @@ -213,9 +213,6 @@ public boolean collapse() // get expanded height final int expandedHeight = this.getMeasuredHeight(); - // get collapsed height - //TODO - // animate from expanded height to collapsed height final ValueAnimator valueAnimator = ValueAnimator.ofInt(expandedHeight, this.collapsedHeight); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() From aef07916ad49652045e8ebbe4b2a51759b10ac3f Mon Sep 17 00:00:00 2001 From: Cliff Ophalvens Date: Tue, 14 Jun 2016 13:19:17 +0200 Subject: [PATCH 4/6] alter manifest, so MainActivity handles configChanges by itself --- app/src/main/AndroidManifest.xml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index c232d2d..13ee363 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -26,7 +26,9 @@ android:theme="@style/AppTheme" tools:ignore="AllowBackup"> - + From 4b44aee0a0f67f7f122ce8187c07b861287c8b37 Mon Sep 17 00:00:00 2001 From: Cliff Ophalvens Date: Tue, 14 Jun 2016 13:27:23 +0200 Subject: [PATCH 5/6] prepare release 1.0.2 --- CHANGELOG.md | 4 ++++ expandabletextview/build.gradle | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 626725a..1c3c61b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/expandabletextview/build.gradle b/expandabletextview/build.gradle index 9380d39..3bdd501 100644 --- a/expandabletextview/build.gradle +++ b/expandabletextview/build.gradle @@ -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 From fb04b56fe5cad9654e50440f68443a60bd771b2b Mon Sep 17 00:00:00 2001 From: Cliff Ophalvens Date: Wed, 15 Jun 2016 13:14:28 +0200 Subject: [PATCH 6/6] update README --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index fc77d22..5dc2188 100644 --- a/README.md +++ b/README.md @@ -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 -------------- @@ -32,7 +32,7 @@ library dependency ```groovy dependencies { - compile ('at.blogc:expandabletextview:1.0.1@aar') + compile ('at.blogc:expandabletextview:1.0.2@aar') } ``` @@ -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