如何动画展开/折叠 TextView

问题描述

我有带有展开/折叠显示更多/更少按钮的 TextView。现在我想添加平滑的动画。我用 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();
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)