eoe 移动开发者论坛

 找回密码
 加入eoe

QQ登录

只需一步,快速开始

查看: 159|回复: 0
收起左侧

一个 TextView 完成显示全文与隐藏功能

[复制链接]

签到天数: 114 天

连续签到: 1 天

[LV.6]常住居民II

46

主题

230

帖子

1350

e币
发表于 2017-3-3 09:44:18 | 显示全部楼层 |阅读模式

没有eoe的账号,级别还太低,出门如何吹牛逼?

您需要 登录 才可以下载或查看,没有帐号?加入eoe

x
public class CollapsibleTextView extends TextView {
  private int mSuffixColor = 0xff0000ff;
  private int mCollapsedLines = 1;
  private boolean mSuffixTrigger = false;
  private String mText;
  private OnClickListener mCustomClickListener;
  private boolean mShouldInitLayout = true;
  private boolean mExpanded = false;
  private String
      mCollapsedText = " Show All",
      mExpandedText = " Hide";
  public CollapsibleTextView(Context context) {
    this(context, null);
  }
  public CollapsibleTextView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }
  public CollapsibleTextView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    TypedArray attributes = context.getTheme()
        .obtainStyledAttributes(attrs, R.styleable.CollapsibleTextView, defStyleAttr, 0);
    mSuffixColor = attributes.getColor(R.styleable.CollapsibleTextView_suffixColor, 0xff0000ff);
    mCollapsedLines = attributes.getInt(R.styleable.CollapsibleTextView_collapsedLines, 1);
    mCollapsedText = attributes.getString(R.styleable.CollapsibleTextView_collapsedText);
    if (TextUtils.isEmpty(mCollapsedText)) mCollapsedText = " Show All";
    mExpandedText = attributes.getString(R.styleable.CollapsibleTextView_expandedText);
    if (TextUtils.isEmpty(mExpandedText)) mExpandedText = " Hide";
    mSuffixTrigger = attributes.getBoolean(R.styleable.CollapsibleTextView_suffixTrigger, false);
    this.mText = getText() == null ? null : getText().toString();
    setMovementMethod(LinkMovementMethod.getInstance());
    super.setOnClickListener(mClickListener);
  }
  private OnClickListener mClickListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
      if (!mSuffixTrigger) {
        mExpanded = !mExpanded;
        applyState(mExpanded);
      }
      if (mCustomClickListener != null) {
        mCustomClickListener.onClick(v);
      }
    }
  };
  private ClickableSpan mClickSpanListener
      = new ClickableSpan() {
    @Override
    public void onClick(View widget) {
      if (mSuffixTrigger) {
        mExpanded = !mExpanded;
        applyState(mExpanded);
      }
    }
    @Override
    public void updateDrawState(TextPaint ds) {
      super.updateDrawState(ds);
      ds.setUnderlineText(false);
    }
  };
  private void applyState(boolean expanded) {
    if (TextUtils.isEmpty(mText)) return;
    String note = mText, suffix;
    if (expanded) {
      suffix = mExpandedText;
    } else {
      if (mCollapsedLines - 1 < 0) {
        throw new RuntimeException("CollapsedLines must equal or greater than 1");
      }
      int lineEnd = getLayout().getLineEnd(mCollapsedLines - 1);
      suffix = mCollapsedText;
      int newEnd = lineEnd - suffix.length() - 1;
      int end = newEnd > 0 ? newEnd : lineEnd;
      TextPaint paint = getPaint();
      int maxWidth = mCollapsedLines * (getMeasuredWidth() - getPaddingLeft() - getPaddingRight());
      while (paint.measureText(note.substring(0, end) + suffix) > maxWidth)
        end--;
      note = note.substring(0, end);
    }
    final SpannableString str = new SpannableString(note + suffix);
    if (mSuffixTrigger) {
      str.setSpan(mClickSpanListener,
          note.length(),
          note.length() + suffix.length(),
          SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    str.setSpan(new ForegroundColorSpan(mSuffixColor),
        note.length(),
        note.length() + suffix.length(),
        SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
    post(new Runnable() {
      @Override
      public void run() {
        setText(str);
      }
    });
  }
  @Override
  protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);
    if (mShouldInitLayout && getLineCount() > mCollapsedLines) {
      mShouldInitLayout = false;
      applyState(mExpanded);
    }
  }
  public void setFullString(String str) {
    this.mText = str;
    mShouldInitLayout = true;
    setText(mText);
  }
  @Override
  public void setOnClickListener(OnClickListener l) {
    mCustomClickListener = l;
  }
  public boolean isExpanded() {
    return mExpanded;
  }
  public void setExpanded(boolean mExpanded) {
    if (this.mExpanded != mExpanded) {
      this.mExpanded = mExpanded;
      applyState(mExpanded);
    }
  }
  public int getSuffixColor() {
    return mSuffixColor;
  }
  public void setSuffixColor(int mSuffixColor) {
    this.mSuffixColor = mSuffixColor;
    applyState(mExpanded);
  }
  public int getCollapsedLines() {
    return mCollapsedLines;
  }
                                public void setCollapsedLines(int mCollapsedLines) {
    this.mCollapsedLines = mCollapsedLines;
    mShouldInitLayout = true;
    setText(mText);
  }
  public boolean isSuffixTrigger() {
    return mSuffixTrigger;
  }
  public void setSuffixTrigger(boolean mSuffixTrigger) {
    this.mSuffixTrigger = mSuffixTrigger;
    applyState(mExpanded);
  }
  public String getCollapsedText() {
    return mCollapsedText;
  }
  public void setCollapsedText(String mCollapsedText) {
    this.mCollapsedText = mCollapsedText;
    applyState(mExpanded);
  }
  public String getExpandedText() {
    return mExpandedText;
  }
  public void setExpandedText(String mExpandedText) {
    this.mExpandedText = mExpandedText;
    applyState(mExpanded);
  }
}

*滑动验证:
您需要登录后才可以回帖 登录 | 加入eoe

本版积分规则

推荐阅读
赞助商们

QQ|联系我们|小黑屋|手机版|eoe 移动开发者论坛 ( 京ICP备11018032 京公网安11010802020210  

GMT+8, 2017-9-25 19:42 , Processed in 0.748844 second(s), 41 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表
关闭

扫一扫 关注eoe官方微信