欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

AndroidTextView實現(xiàn)多文本折疊、展開效果

背景

創(chuàng)新互聯(lián)2013年至今,先為盤錦等服務(wù)建站,盤錦等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為盤錦企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

在開發(fā)過程中,當(dāng)我們的需求中包含說說或者評論等內(nèi)容的展示時,我們都會考慮當(dāng)內(nèi)容太多時該如何顯示。當(dāng)內(nèi)容的字?jǐn)?shù)太多,如果全部展示出來可能會影響體驗效果,但是又不能只截取一部分內(nèi)容進(jìn)行展示,此時就需要考慮使用多行顯示折疊的效果來實現(xiàn)。

效果圖:

Android TextView實現(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  />

 <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  />

 <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  />

 <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" />
 <!--展開/收起動畫執(zhí)行時間-->
 <attr name="durationMillis" format="integer" />
 <!--可點擊區(qū)域,默認(rèn)展開/收起區(qū)域可點擊-->
 <attr name="clickAll" format="boolean" />
</declare-styleable>

2.是否顯示折疊效果

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
 // 如果沒有變化,測量并返回
 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.展開/收起動畫

@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;//計算要展開高度
  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)前動畫幀對應(yīng)的相對時間,值總在0-1之間,原始長度+高度差*(從0到1的漸變)即表現(xiàn)為動畫效果
   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" />
 <!--展開/收起動畫執(zhí)行時間-->
 <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" />
 <!--可點擊區(qū)域,默認(rèn)展開/收起區(qū)域可點擊-->
 <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) {
 // 如果沒有變化,測量并返回
 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ù),不處理
 if (mTvContent.getLineCount() <= mMaxCollapsedLines) {
  return;
 }
 // 獲取內(nèi)容tv真實高度(含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();
   }
  });
  // 保存這個容器的測量高度
  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.展開/收起動畫

@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í)行展開/收起動畫
 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) {
   // 動畫結(jié)束后發(fā)送結(jié)束的信號,清除動畫標(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();
}

項目地址 ☞ 傳送門

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

本文標(biāo)題:AndroidTextView實現(xiàn)多文本折疊、展開效果
本文來源:http://chinadenli.net/article16/iegddg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、虛擬主機、動態(tài)網(wǎng)站軟件開發(fā)、企業(yè)網(wǎng)站制作、網(wǎng)站營銷

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)
日韩人妻一区中文字幕| 日本精品啪啪一区二区三区| 中文字幕日韩无套内射| 久热香蕉精品视频在线播放| 亚洲视频一区自拍偷拍另类 | 九九热精彩视频在线免费| 成年女人午夜在线视频| 老鸭窝老鸭窝一区二区| 成人精品一区二区三区综合 | 国产精品免费无遮挡不卡视频| 成人亚洲国产精品一区不卡| 国产亚洲系列91精品| 色综合久久六月婷婷中文字幕| 欧美自拍系列精品在线| 亚洲高清中文字幕一区二区三区| 自拍偷拍福利视频在线观看| 国产精品一区二区三区欧美| 久久99爱爱视频视频| 亚洲国产精品一区二区| 午夜久久精品福利视频| 免费在线播放不卡视频| 黑丝国产精品一区二区| 好吊视频有精品永久免费| 欧美成人欧美一级乱黄| 国产高清三级视频在线观看| 欧美亚洲美女资源国产| 台湾综合熟女一区二区| 在线观看那种视频你懂的| 四十女人口红哪个色好看| 日韩国产亚洲欧美激情| 一区二区日韩欧美精品| 亚洲性日韩精品一区二区| 青青操在线视频精品视频| 国产午夜在线精品视频| 国产伦精品一一区二区三区高清版 | 亚洲欧美精品伊人久久| 中文字幕91在线观看| 亚洲天堂国产精品久久精品| 暴力三级a特黄在线观看| 99久久成人精品国产免费| 色涩一区二区三区四区|