Last active
September 1, 2015 17:35
-
-
Save niftynei/5ec8cb27d945c8b656ce to your computer and use it in GitHub Desktop.
Making it rain expandable textz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.electricobjects.client.collections; | |
import android.animation.Animator; | |
import android.animation.AnimatorListenerAdapter; | |
import android.animation.ObjectAnimator; | |
import android.app.Activity; | |
import android.support.v7.widget.RecyclerView; | |
import android.text.Editable; | |
import android.text.Layout; | |
import android.text.Spannable; | |
import android.text.SpannableString; | |
import android.text.method.LinkMovementMethod; | |
import android.text.style.URLSpan; | |
import android.text.style.UnderlineSpan; | |
import android.view.View; | |
import android.view.ViewTreeObserver; | |
import android.view.animation.AccelerateDecelerateInterpolator; | |
import android.widget.ImageView; | |
import android.widget.TextView; | |
import com.electricobjects.client.R; | |
import com.electricobjects.client.models.Collection; | |
import com.electricobjects.client.util.NavigationUtil; | |
import com.electricobjects.client.util.image.ImageUtil; | |
/** | |
* Created by koral on 20.05.15. | |
* Modified by lisa on 25.08.15. | |
*/ | |
class CollectionDetailsViewHolder extends RecyclerView.ViewHolder { | |
private final TextView mCollectionBody; | |
private final TextView mReadMoreButton; | |
public CollectionDetailsViewHolder(View itemView) { | |
super(itemView); | |
mCollectionBody = (TextView) itemView.findViewById(R.id.collection_body); | |
mReadMoreButton = (TextView) itemView.findViewById(R.id.button_read_more); | |
int blueColor = itemView.getResources().getColor(R.color.friendly_blue); | |
mCollectionBody.setLinkTextColor(blueColor); | |
mCollectionBody.setMovementMethod(LinkMovementMethod.getInstance()); | |
// Make the Read More button into a blue link thing | |
Spannable text = new SpannableString(mReadMoreButton.getText()); | |
text.setSpan(new UnderlineSpan(), 0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); | |
mReadMoreButton.setText(text); | |
mReadMoreButton.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
ObjectAnimator animation = ObjectAnimator.ofInt(mCollectionBody, "maxLines", 50); | |
animation.setInterpolator(new AccelerateDecelerateInterpolator()); | |
animation.addListener(new AnimatorListenerAdapter() { | |
@Override | |
public void onAnimationStart(Animator animation) { | |
mReadMoreButton.animate() | |
.alpha(0.0f) | |
.setInterpolator(new AccelerateDecelerateInterpolator()) | |
.setDuration(300) | |
.setListener(new AnimatorListenerAdapter() { | |
@Override | |
public void onAnimationEnd(Animator animation) { | |
mReadMoreButton.setVisibility(View.GONE); | |
} | |
}) | |
.start(); | |
} | |
}); | |
animation.setDuration(300).start(); | |
} | |
}); | |
// Only show the Read More button if need to (set up below) | |
mReadMoreButton.setVisibility(View.GONE); | |
} | |
// Called in the RecyclerView adapter onBind method | |
public void bind(Collection collection) { | |
setupBodyText(collection.getFormattedBody()); | |
ViewTreeObserver vto = mCollectionBody.getViewTreeObserver(); | |
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { | |
@Override | |
public void onGlobalLayout() { | |
mCollectionBody.getViewTreeObserver().removeOnGlobalLayoutListener(this); | |
Layout layout = mCollectionBody.getLayout(); | |
if (layout != null && layout.getLineCount() > 0) { | |
int lines = layout.getLineCount(); | |
boolean isEllipsized = (mCollectionBody.getMaxLines() < layout.getLineCount()); | |
if (isEllipsized) { | |
// Show the Read More button | |
mReadMoreButton.setVisibility(View.VISIBLE); | |
// TODO: insert ... at the end of the line | |
} | |
} | |
} | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void bind(Collection collection) { | |
// other set up stuff... | |
ViewTreeObserver vto = mCollectionBody.getViewTreeObserver(); | |
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { | |
@Override | |
public void onGlobalLayout() { | |
mCollectionBody.getViewTreeObserver().removeOnGlobalLayoutListener(this); | |
final Layout layout = mCollectionBody.getLayout(); | |
if (layout != null && layout.getLineCount() > 0) { | |
boolean isEllipsized = (mCollectionBody.getMaxLines() < layout.getLineCount()); | |
if (isEllipsized) { | |
mReadMoreButton.setVisibility(View.VISIBLE); | |
mReadMoreButton.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
ObjectAnimator animation = ObjectAnimator.ofInt(mCollectionBody, "maxLines", layout.getLineCount()); | |
animation.setInterpolator(new AccelerateDecelerateInterpolator()); | |
animation.addListener(new AnimatorListenerAdapter() { | |
@Override | |
public void onAnimationStart(Animator animation) { | |
mReadMoreButton.animate() | |
.alpha(0.0f) | |
.setInterpolator(new AccelerateDecelerateInterpolator()) | |
.setDuration(300) | |
.setListener(new AnimatorListenerAdapter() { | |
@Override | |
public void onAnimationEnd(Animator animation) { | |
mReadMoreButton.setVisibility(View.GONE); | |
} | |
}) | |
.start(); | |
} | |
}); | |
animation.setDuration(300).start(); | |
} | |
}); | |
// TODO: insert ... at the end of the line | |
} | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment