AndroidTextView实现多文本折叠、展开效果

AndroidTextView实现多文本折叠、展开效果

2023年7月13日发(作者:)

AndroidTextView实现多⽂本折叠、展开效果背景在开发过程中,当我们的需求中包含说说或者评论等内容的展⽰时,我们都会考虑当内容太多时该如何显⽰。当内容的字数太多,如果全部展⽰出来可能会影响体验效果,但是⼜不能只截取⼀部分内容进⾏展⽰,此时就需要考虑使⽤多⾏显⽰折叠的效果来实现。效果图:使⽤1.布局⽂件调⽤ ⽂件调⽤private void initData() { t(t1); t(t2); t(t3); t(t4);}MoreLineTextView使⽤1.在中定义属性 2.是否显⽰折叠效果@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { ure(widthMeasureSpec, heightMeasureSpec); // 如果没有变化,测量并返回 if (!mRelayout || getVisibility() == ) { ure(widthMeasureSpec, heightMeasureSpec); return; } mRelayout = false; ure(widthMeasureSpec, heightMeasureSpec); // 内容区域初始显⽰⾏⾼ ght(eHeight() * (mMaxLine > eCount() ? eCount() : mMaxLine)); (new Runnable() { @Override public void run() { // 是否显⽰折叠效果 ibility(eCount() > mMaxLine ? E : ); } });}3.设置显⽰内容/** * @Description 设置显⽰内容 */public void setText(String str) { mRelayout = true; t(str); setVisibility(y(str) ? : E);}4.展开/收起动画@Overridepublic void onClick(View v) { if (eCount() <= mMaxLine) { return; } isExpand = !isExpand; nimation(); final int deltaValue; final int startValue = ght(); if (isExpand) { deltaValue = eHeight() * eCount() - startValue;//计算要展开⾼度 RotateAnimation animation = new RotateAnimation(0, 180, VE_TO_SELF, 0.5f, VE_TO_SELF, 0.5f); ation(mDurationMillis); lAfter(true); nimation(animation); t(getContext().getString(se)); } else { deltaValue = eHeight() * mMaxLine - startValue;//为负值,收缩的⾼度 RotateAnimation animation = new RotateAnimation(180, 0, VE_TO_SELF, 0.5f, VE_TO_SELF, 0.5f); ation(mDurationMillis); lAfter(true); nimation(animation); t(getContext().getString()); } Animation animation = new Animation() { protected void applyTransformation(float interpolatedTime, Transformation t) { //interpolatedTime:为当前动画帧对应的相对时间,值总在0-1之间,原始长度+⾼度差*(从0到1的渐变)即表现为动画效果 ght((int) (startValue + deltaValue * interpolatedTime)); } }; ation(mDurationMillis); nimation(animation);}ExpandableTextView使⽤1.在中定义属性 2.是否显⽰折叠效果@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // 如果没有变化,测量并返回 if (!mRelayout || getVisibility() == ) { ure(widthMeasureSpec, heightMeasureSpec); return; } mRelayout = false; // Setup with optimistic case // i.e. Everything fits. No button needed ibility(); Lines(_VALUE); // Measure ure(widthMeasureSpec, heightMeasureSpec); //如果内容真实⾏数⼩于等于最⼤⾏数,不处理 if (eCount() <= mMaxCollapsedLines) { return; } // 获取内容tv真实⾼度(含padding) mTextHeightWithMaxLines = getRealTextViewHeight(mTvContent); // 如果是收起状态,重新设置最⼤⾏数 if (mCollapsed) { Lines(mMaxCollapsedLines); } ibility(E); // Re-measure with new setup ure(widthMeasureSpec, heightMeasureSpec); if (mCollapsed) { // Gets the margin between the TextView's bottom and the ViewGroup's bottom (new Runnable() { @Override public void run() { mMarginBetweenTxtAndBottom = getHeight() - ght(); } }); // 保存这个容器的测量⾼度 mCollapsedHeight = getMeasuredHeight(); }}3.设置显⽰内容/** * @Description 设置显⽰内容 */public void setText(CharSequence text) { mRelayout = true; t(text); setVisibility(y(text) ? : E);}4.展开/收起动画@Overridepublic void onClick(View view) { if (ibility() != E) { return; } mCollapsed = !mCollapsed; // 修改收起/展开图标、⽂字 setDrawbleAndText(); // 保存位置状态 if (mCollapsedStatus != null) { (mPosition, mCollapsed); } // 执⾏展开/收起动画 mAnimating = true; ValueAnimator valueAnimator; if (mCollapsed) { valueAnimator = new ValueAnimator().ofInt(getHeight(), mCollapsedHeight); } else { mCollapsedHeight = getHeight(); valueAnimator = new ValueAnimator().ofInt(getHeight(), getHeight() + mTextHeightWithMaxLines - ght()); } ateListener(new orUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator valueAnimator) { int animatedValue = (int) matedValue(); Height(animatedValue - mMarginBetweenTxtAndBottom); getLayoutParams().height = animatedValue; requestLayout(); } }); tener(new orListener() { @Override public void onAnimationStart(Animator animator) { } @Override public void onAnimationEnd(Animator animator) { // 动画结束后发送结束的信号,清除动画标志 mAnimating = false; // 通知监听 if (mListener != null) { ndStateChanged(mTvContent, !mCollapsed); } } @Override public void onAnimationCancel(Animator animator) { } @Override public void onAnimationRepeat(Animator animator) { } }); ation(mAnimationDuration); ();}项⽬地址 ☞以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

发布者:admin,转转请注明出处:http://www.yc00.com/web/1689215892a222260.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信