我有文本视图和展开/折叠显示更多/更少按钮。现在我想添加平滑动画。我用objectanimator尝试了很多东西,但应用程序仍然很强大。我的代码是这样的。
public void onBindViewHolder(@NonNull final MyHolderText myHolder, int i) {
myHolder.mTitle.setText(models.get(i).getTitle());
myHolder.mImageView.setImageResource(models.get(i).getImage());
myHolder.mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (myHolder.mButton.getText().toString().equalsIgnoreCase(c.getString(R.string.more)))
{
myHolder.mDescription.setMaxLines(Integer.MAX_VALUE);
myHolder.mButton.setText(R.string.less);
}
else
{
myHolder.mDescription.setMaxLines(5);
myHolder.mButton.setText(R.string.more);
}
}
});
myHolder.mDescription.setText(models.get(i).getDesc());
}
此方法仅适用于展开文本。当我尝试折叠时,它只显示5行,下面有空格,就像视图没有调整大小一样。也许有什么办法可以改变它的崩溃?
@SuppressLint("Range")
public static void expandCollapsedByMaxLines(@NonNull final TextView text) {
int height = text.getMeasuredHeight();
text.setHeight(height);
text.setMaxLines(Integer.MAX_VALUE); //expand fully
text.measure(View.MeasureSpec.makeMeasureSpec(text.getMeasuredWidth(), View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.WRAP_CONTENT, View.MeasureSpec.UNSPECIFIED));
int newHeight = text.getMeasuredHeight();
ObjectAnimator animation = ObjectAnimator.ofInt(text, "height", height, newHeight);
animation.setDuration(250).start();
}
暂无答案!
目前还没有任何答案,快来回答吧!