This commit is contained in:
@ -25,7 +25,7 @@ public class ExpandTextView extends AppCompatTextView {
|
||||
private String mEndText = "...";
|
||||
private String mExpandText = mEndText + "全文";
|
||||
private boolean mUseGradientAlphaEndText = false;
|
||||
private boolean mShrinkOnAnchor = false;
|
||||
private boolean mShrinkOnAnchor = false; // 以锚点所在的位置进行收起 (即 maxLines = 锚点所在的行)
|
||||
|
||||
private int mMaxLines = 3; // 由于sdk版本限制(getMaxLines) 这里设置默认值
|
||||
|
||||
@ -33,6 +33,7 @@ public class ExpandTextView extends AppCompatTextView {
|
||||
private boolean mIsExpanded = false; // 位于 recyclerView 时需要自行在外层管理是否已展开
|
||||
|
||||
private ExpandCallback mExpandCallback;
|
||||
private SelfCalculateMaxLinesCallback mMaxLinesCalculatedCallback;
|
||||
|
||||
private Rect mLastVisibleLineRect;
|
||||
private Rect mLastActualLineRect;
|
||||
@ -65,25 +66,23 @@ public class ExpandTextView extends AppCompatTextView {
|
||||
@Override
|
||||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||||
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
||||
if (mShrinkOnAnchor) {
|
||||
if (mShrinkOnAnchor && getText().toString().contains(SHRINK_ANCHOR)) {
|
||||
updateMaxLinesByShrinkAnchor();
|
||||
}
|
||||
setMeasuredDimension(getMeasuredWidth(), getMeasuredHeight() - getExtraBottomPadding());
|
||||
}
|
||||
|
||||
private void updateMaxLinesByShrinkAnchor() {
|
||||
int count = getLineCount();
|
||||
for (int line = 0; line < count; line++) {
|
||||
int start = getLayout().getLineStart(line);
|
||||
int end = getLayout().getLineEnd(line);
|
||||
CharSequence substring = getText().subSequence(start, end);
|
||||
int lineCount = getLineCount();
|
||||
for (int line = 0; line < lineCount; line++) {
|
||||
CharSequence substring = getText().subSequence(getLayout().getLineStart(line), getLayout().getLineEnd(line));
|
||||
if (substring.toString().contains(SHRINK_ANCHOR)) {
|
||||
if (mMaxLines != line + 1) {
|
||||
setExpandMaxLines(line + 1);
|
||||
int anchorIndex = getText().toString().indexOf(SHRINK_ANCHOR);
|
||||
SpannableStringBuilder sb = new SpannableStringBuilder(getText());
|
||||
sb.delete(anchorIndex, anchorIndex + 1);
|
||||
setText(sb);
|
||||
if (!mIsExpanded) {
|
||||
setExpandMaxLines(line + 1);
|
||||
mMaxLinesCalculatedCallback.onMaxLinesCalculated(line + 1);
|
||||
}
|
||||
setText(findAndDeleteAnchor(getText()));
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -94,13 +93,13 @@ public class ExpandTextView extends AppCompatTextView {
|
||||
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
|
||||
super.onLayout(changed, left, top, right, bottom);
|
||||
if (mInitLayout && !mIsExpanded && getLineCount() > mMaxLines) {
|
||||
mSnapshotText = getText();
|
||||
mSnapshotText = findAndDeleteAnchor(getText());
|
||||
mInitLayout = false;
|
||||
showExpandButton();
|
||||
}
|
||||
}
|
||||
|
||||
public void setExpendText(String text) {
|
||||
public void setExpandText(String text) {
|
||||
this.mExpandText = text;
|
||||
}
|
||||
|
||||
@ -241,6 +240,18 @@ public class ExpandTextView extends AppCompatTextView {
|
||||
return result;
|
||||
}
|
||||
|
||||
// 去掉锚点
|
||||
private CharSequence findAndDeleteAnchor(CharSequence charSequence) {
|
||||
int anchorIndex = charSequence.toString().indexOf(SHRINK_ANCHOR);
|
||||
if (anchorIndex >= 0) {
|
||||
SpannableStringBuilder sb = new SpannableStringBuilder(getText());
|
||||
sb.delete(anchorIndex, anchorIndex + 1);
|
||||
return sb;
|
||||
} else {
|
||||
return charSequence;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 此方法仅更改标记,不做实际展开的操作
|
||||
*/
|
||||
@ -253,8 +264,16 @@ public class ExpandTextView extends AppCompatTextView {
|
||||
setMaxLines(maxLines);
|
||||
}
|
||||
|
||||
public void setSelfCalculateMaxLinesCallback(SelfCalculateMaxLinesCallback callback) {
|
||||
mMaxLinesCalculatedCallback = callback;
|
||||
}
|
||||
|
||||
public interface ExpandCallback {
|
||||
void onExpand();
|
||||
}
|
||||
|
||||
public interface SelfCalculateMaxLinesCallback {
|
||||
void onMaxLinesCalculated(int maxLines);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user