Android TextView實(shí)現(xiàn)多文本折疊、展開效果
背景
在開發(fā)過程中,當(dāng)我們的需求中包含說說或者評(píng)論等內(nèi)容的展示時(shí),我們都會(huì)考慮當(dāng)內(nèi)容太多時(shí)該如何顯示。當(dāng)內(nèi)容的字?jǐn)?shù)太多,如果全部展示出來可能會(huì)影響體驗(yàn)效果,但是又不能只截取一部分內(nèi)容進(jìn)行展示,此時(shí)就需要考慮使用多行顯示折疊的效果來實(shí)現(xiàn)。
效果圖:
使用
1.布局文件調(diào)用
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.wiggins.expandable.widget.MoreLineTextView android:id="@+id/tv_more_line_short" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white" android:padding="@dimen/padding_small" app:clickAll="true" app:textColor="@color/red" /> <View style="@style/spaceLine" /> <com.wiggins.expandable.widget.expandable.ExpandableTextView android:id="@+id/tv_expandable_short" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white" android:ellipsize="end" android:padding="@dimen/padding_small" android:textColor="@color/blue" app:allClickable="false" app:contentTextColor="@color/blue" app:isDisplayIcon="false" app:maxCollapsedLines="4" /> <View style="@style/spaceLine" /> <com.wiggins.expandable.widget.MoreLineTextView android:id="@+id/tv_more_line_long" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white" android:padding="@dimen/padding_small" app:clickAll="true" app:textColor="@color/red" /> <View style="@style/spaceLine" /> <com.wiggins.expandable.widget.expandable.ExpandableTextView android:id="@+id/tv_expandable_long" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white" android:ellipsize="end" android:padding="@dimen/padding_small" android:textColor="@color/blue" app:allClickable="false" app:contentTextColor="@color/blue" app:isDisplayIcon="false" app:maxCollapsedLines="4" /> </LinearLayout>
2.Java文件調(diào)用
private void initData() { mTvMoreLineShort.setText(Constant.content1); mTvExpandableShort.setText(Constant.content2); mTvMoreLineLong.setText(Constant.content3); mTvExpandableLong.setText(Constant.content4); }
MoreLineTextView使用
1.在attr.xml中定義屬性
<declare-styleable name="MoreTextStyle"> <!--內(nèi)容大小--> <attr name="textSize" format="dimension" /> <!--內(nèi)容顏色--> <attr name="textColor" format="color" /> <!--內(nèi)容默認(rèn)最大行數(shù)--> <attr name="maxLine" format="integer" /> <!--展開/收起圖標(biāo)--> <attr name="expandIcon" format="reference" /> <!--展開/收起動(dòng)畫執(zhí)行時(shí)間--> <attr name="durationMillis" format="integer" /> <!--可點(diǎn)擊區(qū)域,默認(rèn)展開/收起區(qū)域可點(diǎn)擊--> <attr name="clickAll" format="boolean" /> </declare-styleable>
2.是否顯示折疊效果
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); // 如果沒有變化,測(cè)量并返回 if (!mRelayout || getVisibility() == View.GONE) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); return; } mRelayout = false; super.onMeasure(widthMeasureSpec, heightMeasureSpec); // 內(nèi)容區(qū)域初始顯示行高 mTvContent.setHeight(mTvContent.getLineHeight() * (mMaxLine > mTvContent.getLineCount() ? mTvContent.getLineCount() : mMaxLine)); mLlExpand.post(new Runnable() { @Override public void run() { // 是否顯示折疊效果 mLlExpand.setVisibility(mTvContent.getLineCount() > mMaxLine ? View.VISIBLE : View.GONE); } }); }
3.設(shè)置顯示內(nèi)容
/** * @Description 設(shè)置顯示內(nèi)容 */ public void setText(String str) { mRelayout = true; mTvContent.setText(str); setVisibility(TextUtils.isEmpty(str) ? View.GONE : View.VISIBLE); }
4.展開/收起動(dòng)畫
@Override public void onClick(View v) { if (mTvContent.getLineCount() <= mMaxLine) { return; } isExpand = !isExpand; mTvContent.clearAnimation(); final int deltaValue; final int startValue = mTvContent.getHeight(); if (isExpand) { deltaValue = mTvContent.getLineHeight() * mTvContent.getLineCount() - startValue;//計(jì)算要展開高度 RotateAnimation animation = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); animation.setDuration(mDurationMillis); animation.setFillAfter(true); mIvExpand.startAnimation(animation); mTvExpand.setText(getContext().getString(R.string.collapse)); } else { deltaValue = mTvContent.getLineHeight() * mMaxLine - startValue;//為負(fù)值,收縮的高度 RotateAnimation animation = new RotateAnimation(180, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); animation.setDuration(mDurationMillis); animation.setFillAfter(true); mIvExpand.startAnimation(animation); mTvExpand.setText(getContext().getString(R.string.expand)); } Animation animation = new Animation() { protected void applyTransformation(float interpolatedTime, Transformation t) { //interpolatedTime:為當(dāng)前動(dòng)畫幀對(duì)應(yīng)的相對(duì)時(shí)間,值總在0-1之間,原始長(zhǎng)度+高度差*(從0到1的漸變)即表現(xiàn)為動(dòng)畫效果 mTvContent.setHeight((int) (startValue + deltaValue * interpolatedTime)); } }; animation.setDuration(mDurationMillis); mTvContent.startAnimation(animation); }
ExpandableTextView使用
1.在attr.xml中定義屬性
<declare-styleable name="ExpandableTextView"> <!--內(nèi)容默認(rèn)最大行數(shù),超過隱藏--> <attr name="maxCollapsedLines" format="integer" /> <!--展開/收起動(dòng)畫執(zhí)行時(shí)間--> <attr name="animDuration" format="integer" /> <!--展開圖片--> <attr name="expandDrawable" format="reference" /> <!--收起圖片--> <attr name="collapseDrawable" format="reference" /> <!--內(nèi)容顏色--> <attr name="contentTextColor" format="color" /> <!--內(nèi)容大小--> <attr name="contentTextSize" format="dimension" /> <!--收起/展開顏色--> <attr name="collapseExpandTextColor" format="color" /> <!--收起/展開大小--> <attr name="collapseExpandTextSize" format="dimension" /> <!--收起文字--> <attr name="textCollapse" format="string" /> <!--展開文字--> <attr name="textExpand" format="string" /> <!--可點(diǎn)擊區(qū)域,默認(rèn)展開/收起區(qū)域可點(diǎn)擊--> <attr name="allClickable" format="boolean" /> <!--是否顯示展開/收起圖標(biāo),默認(rèn)顯示--> <attr name="isDisplayIcon" format="boolean" /> <!--收起/展開位置,默認(rèn)左邊--> <attr name="collapseExpandGrarity"> <flag name="left" value="3" /> <flag name="right" value="5" /> </attr> <!--收起/展開圖標(biāo)位置,默認(rèn)右邊--> <attr name="drawableGrarity"> <flag name="left" value="3" /> <flag name="right" value="5" /> </attr> </declare-styleable>
2.是否顯示折疊效果
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // 如果沒有變化,測(cè)量并返回 if (!mRelayout || getVisibility() == View.GONE) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); return; } mRelayout = false; // Setup with optimistic case // i.e. Everything fits. No button needed mLlExpand.setVisibility(View.GONE); mTvContent.setMaxLines(Integer.MAX_VALUE); // Measure super.onMeasure(widthMeasureSpec, heightMeasureSpec); //如果內(nèi)容真實(shí)行數(shù)小于等于最大行數(shù),不處理 if (mTvContent.getLineCount() <= mMaxCollapsedLines) { return; } // 獲取內(nèi)容tv真實(shí)高度(含padding) mTextHeightWithMaxLines = getRealTextViewHeight(mTvContent); // 如果是收起狀態(tài),重新設(shè)置最大行數(shù) if (mCollapsed) { mTvContent.setMaxLines(mMaxCollapsedLines); } mLlExpand.setVisibility(View.VISIBLE); // Re-measure with new setup super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (mCollapsed) { // Gets the margin between the TextView's bottom and the ViewGroup's bottom mTvContent.post(new Runnable() { @Override public void run() { mMarginBetweenTxtAndBottom = getHeight() - mTvContent.getHeight(); } }); // 保存這個(gè)容器的測(cè)量高度 mCollapsedHeight = getMeasuredHeight(); } }
3.設(shè)置顯示內(nèi)容
/** * @Description 設(shè)置顯示內(nèi)容 */ public void setText(CharSequence text) { mRelayout = true; mTvContent.setText(text); setVisibility(TextUtils.isEmpty(text) ? View.GONE : View.VISIBLE); }
4.展開/收起動(dòng)畫
@Override public void onClick(View view) { if (mLlExpand.getVisibility() != View.VISIBLE) { return; } mCollapsed = !mCollapsed; // 修改收起/展開圖標(biāo)、文字 setDrawbleAndText(); // 保存位置狀態(tài) if (mCollapsedStatus != null) { mCollapsedStatus.put(mPosition, mCollapsed); } // 執(zhí)行展開/收起動(dòng)畫 mAnimating = true; ValueAnimator valueAnimator; if (mCollapsed) { valueAnimator = new ValueAnimator().ofInt(getHeight(), mCollapsedHeight); } else { mCollapsedHeight = getHeight(); valueAnimator = new ValueAnimator().ofInt(getHeight(), getHeight() + mTextHeightWithMaxLines - mTvContent.getHeight()); } valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator valueAnimator) { int animatedValue = (int) valueAnimator.getAnimatedValue(); mTvContent.setMaxHeight(animatedValue - mMarginBetweenTxtAndBottom); getLayoutParams().height = animatedValue; requestLayout(); } }); valueAnimator.addListener(new Animator.AnimatorListener() { @Override public void onAnimationStart(Animator animator) { } @Override public void onAnimationEnd(Animator animator) { // 動(dòng)畫結(jié)束后發(fā)送結(jié)束的信號(hào),清除動(dòng)畫標(biāo)志 mAnimating = false; // 通知監(jiān)聽 if (mListener != null) { mListener.onExpandStateChanged(mTvContent, !mCollapsed); } } @Override public void onAnimationCancel(Animator animator) { } @Override public void onAnimationRepeat(Animator animator) { } }); valueAnimator.setDuration(mAnimationDuration); valueAnimator.start(); }
項(xiàng)目地址 ☞ 傳送門
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android UI實(shí)現(xiàn)多行文本折疊展開效果
- Android TextView多文本折疊展開效果
- Android中RecyclerView實(shí)現(xiàn)多級(jí)折疊列表效果(二)
- Android中RecyclerView實(shí)現(xiàn)多級(jí)折疊列表效果(TreeRecyclerView)
- Android折疊式Toolbar使用完全解析(CollapsingToolbarLayout)
- android給RecyclerView加上折疊的效果示例
- Android中FoldingLayout折疊布局的用法及實(shí)戰(zhàn)全攻略
- Android TextView仿微信可折疊效果
- Android顯示全文折疊控件使用方法詳解
- Android開發(fā)實(shí)現(xiàn)的文本折疊點(diǎn)擊展開功能示例
相關(guān)文章
Android 獲取手機(jī)聯(lián)系人實(shí)例代碼詳解
最近做了個(gè)項(xiàng)目,其中有項(xiàng)目需求是這樣的,需要獲取手機(jī)聯(lián)系人,下面小編把代碼分享給大家,供大家參考2015-12-12android Launcher3設(shè)置默認(rèn)桌面應(yīng)用
這篇文章主要為大家詳細(xì)介紹了android Launcher3設(shè)置默認(rèn)桌面應(yīng)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07Android仿微博首頁(yè)Tab加號(hào)彈窗功能
這篇文章主要為大家詳細(xì)介紹了Android仿微博首頁(yè)Tab加號(hào)彈窗功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Android用Fragment創(chuàng)建選項(xiàng)卡
這篇文章主要為大家詳細(xì)介紹了Android用Fragment創(chuàng)建選項(xiàng)卡的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10Android自定義相機(jī)、預(yù)覽區(qū)域裁剪
這篇文章主要為大家詳細(xì)介紹了Android自定義相機(jī)、預(yù)覽區(qū)域裁剪,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05