ViewTreeObserver vto = tv1.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
int lineCount = tv1.getLineCount();
//LogUtils.i("当前文本lineCount:" + lineCount+";isExpandText:"+dataList.isExpandText);
if (lineCount > 3) {
// holder.item_latest_expand_imageview.setImageDrawable(UIUtils.tintDrawable(UIUtils.getDrawable(R.mipmap.expand_textview), R.attr.colorTheme));
holder.item_latest_expand_ll.setVisibility(View.VISIBLE);
if (dataList.isExpandText) {//展开状态
//翻转icon的180度旋转动画
holder.item_latest_expand_textview.setText("收起全部");
holder.item_latest_expand_imageview.setImageDrawable(UIUtils.tintDrawable(UIUtils.getDrawable(R.mipmap.no_expand_textview), R.attr.colorTheme));
int height = tv1.getLineHeight() * tv1.getLineCount();
// if (tv1.getHeight() != height) {
tv1.setHeight(height);
//}
} else {//折叠
holder.item_latest_expand_imageview.setImageDrawable(UIUtils.tintDrawable(UIUtils.getDrawable(R.mipmap.expand_textview), R.attr.colorTheme));
holder.item_latest_expand_textview.setText("展开更多");
int height = tv1.getLineHeight() * 3;
// if (tv1.getHeight() != height) {
tv1.setHeight(height);
// }
}
} else {
holder.item_latest_expand_ll.setVisibility(View.GONE);
int height = tv1.getLineHeight() * tv1.getLineCount();
if (tv1.getHeight() != height) {
tv1.setHeight(height);
}
}
return true;
}
});
holder.item_latest_expand_ll.setOnClickListener(new View.OnClickListener() {//点击展开/折叠
@Override
public void onClick(View v) {
dataList.isExpandText = !dataList.isExpandText;
tv1.clearAnimation(); //清除动画
holder.item_latest_expand_imageview.clearAnimation();
final int tempHight;
final int startHight = tv1.getHeight(); //起始高度
int durationMillis = 500;
if (!dataList.isExpandText) {
/**
* 折叠效果,从长文折叠成短文
*/
tempHight = tv1.getLineHeight() * 3 - startHight;
//翻转icon的180度旋转动画
RotateAnimation animation = new RotateAnimation(180, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(durationMillis);
animation.setFillAfter(false);
holder.item_latest_expand_imageview.startAnimation(animation);
holder.item_latest_expand_textview.setText("展开更多");
} else {
/**
* 展开效果,从短文展开成长文
*/
tempHight = tv1.getLineHeight() * tv1.getLineCount() - startHight;
//翻转icon的180度旋转动画
RotateAnimation animation = new RotateAnimation(180, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(durationMillis);
animation.setFillAfter(false);
holder.item_latest_expand_imageview.startAnimation(animation);
holder.item_latest_expand_textview.setText("收起全部");
}
Animation animation = new Animation() {
//interpolatedTime 为当前动画帧对应的相对时间,值总在0-1之间
protected void applyTransformation(float interpolatedTime, Transformation t) { //根据ImageView旋转动画的百分比来显示textview高度,达到动画效果
tv1.setHeight((int) (startHight + tempHight * interpolatedTime));//原始长度+高度差*(从0到1的渐变)即表现为动画效果
}
};
animation.setDuration(durationMillis);
tv1.startAnimation(animation);
}
});