Skip to content

Instantly share code, notes, and snippets.

@yuriyskulskiy
Created August 17, 2020 19:39
Show Gist options
  • Save yuriyskulskiy/2175ae4bd0b4fed3d652a76c2ef5b7d5 to your computer and use it in GitHub Desktop.
Save yuriyskulskiy/2175ae4bd0b4fed3d652a76c2ef5b7d5 to your computer and use it in GitHub Desktop.
AnimatedLayout part 4: fix incorrect text width bug
public class AnimatedLayout extends ConstraintLayout {
...
private void applySummerTitleTV() {
mTitleTV.setBackgroundResource(R.drawable.summer_text_background);
mTitleTV.setTextColor(Color.BLACK);
mTitleTV.setText("WINTER IS COMING...");
// fix bug: text view with wrap_content layout param
// does not resize its width and
// last word of new text will not displayed if new string is longer
mTitleTV.post(new Runnable() {
@Override
public void run() {
AnimatedLayout.this.getParent().requestLayout(); // fix textwidth bug
}
});
}
private void applyWinterTitleTV() {
mTitleTV.setBackgroundResource(R.drawable.winter_text_background);
mTitleTV.setTextColor(Color.WHITE);
mTitleTV.setText("WINTER IS HERE...");
mTitleTV.post(new Runnable() {
@Override
public void run() {
AnimatedLayout.this.getParent().requestLayout(); //fix textwidth bug
}
});
}
...
public void invalidateText() {
if (mIdleState == WINTER_STATE) {
applyWinterTitleTV();
} else {
applySummerTitleTV();
}
}
}
public class CustomLinearLayoutManager extends LinearLayoutManager {
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (getItemViewType(position) == REGULAR_VIEW_TYPE) {
RegularViewHolder viewHolder = (RegularViewHolder) holder;
RegularData currentDataItem;
if (position > INSERT_POSITION) {
currentDataItem = dataSet.get(--position);
} else {
currentDataItem = dataSet.get(position);
}
viewHolder.textView.setText(currentDataItem.getText());
} else {
AnimationViewHolder viewHolder = (AnimationViewHolder) holder;
viewHolder.animatedLayout.invalidateText(); //fix text width bug
}
}
public class AnimationViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public AnimatedLayout animatedLayout;
public AnimationViewHolder(View itemView) {
super(itemView);
animatedLayout = itemView.findViewById(R.id.transformItem);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment