更改游戏详情的自定义栏目富文本正文的展开实现
This commit is contained in:
@ -8,6 +8,7 @@ import android.text.Layout;
|
||||
import android.text.SpannableStringBuilder;
|
||||
import android.text.Spanned;
|
||||
import android.text.TextPaint;
|
||||
import android.text.TextUtils;
|
||||
import android.text.style.ClickableSpan;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.View;
|
||||
@ -23,9 +24,11 @@ public class ExpandTextView extends AppCompatTextView {
|
||||
private CharSequence mSnapshotText;
|
||||
|
||||
private String mEndText = "...";
|
||||
private CharSequence mShrankText = "";
|
||||
private String mExpandText = mEndText + "全文";
|
||||
private CharSequence mExpandedText = "";
|
||||
private boolean mUseGradientAlphaEndText = false;
|
||||
private boolean mShrinkOnAnchor = false; // 以锚点所在的位置进行收起 (即 maxLines = 锚点所在的行)
|
||||
private boolean mShowExpandTextRegardlessOfMaxLines = false;
|
||||
|
||||
private int mMaxLines = 3; // 由于sdk版本限制(getMaxLines) 这里设置默认值
|
||||
|
||||
@ -39,7 +42,6 @@ public class ExpandTextView extends AppCompatTextView {
|
||||
private Rect mLastActualLineRect;
|
||||
|
||||
private static int DEFAULT_ADDITIONAL_END_TEXT_COUNT = 2;
|
||||
public static final String SHRINK_ANCHOR = "☼";
|
||||
|
||||
public ExpandTextView(Context context) {
|
||||
super(context);
|
||||
@ -55,45 +57,33 @@ public class ExpandTextView extends AppCompatTextView {
|
||||
mLastActualLineRect = new Rect();
|
||||
|
||||
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ExpandTextView);
|
||||
mShrinkOnAnchor = ta.getBoolean(R.styleable.ExpandTextView_shrinkOnAnchor, false);
|
||||
mUseGradientAlphaEndText = ta.getBoolean(R.styleable.ExpandTextView_useGradientAlphaEndText, false);
|
||||
mEndText = ta.getString(R.styleable.ExpandTextView_endText) == null ? mEndText : ta.getString(R.styleable.ExpandTextView_endText);
|
||||
mExpandText = ta.getString(R.styleable.ExpandTextView_expandText) == null ? mExpandText : ta
|
||||
.getString(R.styleable.ExpandTextView_expandText);
|
||||
mExpandText = ta.getString(R.styleable.ExpandTextView_expandText) == null ? mExpandText : ta.getString(R.styleable.ExpandTextView_expandText);
|
||||
ta.recycle();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||||
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
||||
if (mShrinkOnAnchor && getText().toString().contains(SHRINK_ANCHOR)) {
|
||||
updateMaxLinesByShrinkAnchor();
|
||||
// TODO 复用有问题
|
||||
if (mShowExpandTextRegardlessOfMaxLines && !mIsExpanded) {
|
||||
updateMaxLines();
|
||||
}
|
||||
setMeasuredDimension(getMeasuredWidth(), getMeasuredHeight() - getExtraBottomPadding());
|
||||
}
|
||||
|
||||
private void updateMaxLinesByShrinkAnchor() {
|
||||
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) {
|
||||
if (!mIsExpanded) {
|
||||
setExpandMaxLines(line + 1);
|
||||
mMaxLinesCalculatedCallback.onMaxLinesCalculated(line + 1);
|
||||
}
|
||||
setText(findAndDeleteAnchor(getText()));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
private void updateMaxLines() {
|
||||
mMaxLines = getLineCount() - 1;
|
||||
mMaxLinesCalculatedCallback.onMaxLinesCalculated(getLineCount() - 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
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 && mMaxLines > 0) {
|
||||
mSnapshotText = findAndDeleteAnchor(getText());
|
||||
if ((mShowExpandTextRegardlessOfMaxLines && !mIsExpanded)
|
||||
|| (mInitLayout && !mIsExpanded && getLineCount() > mMaxLines && mMaxLines > 0)) {
|
||||
mSnapshotText = getText();
|
||||
mInitLayout = false;
|
||||
showExpandButton();
|
||||
}
|
||||
@ -107,6 +97,18 @@ public class ExpandTextView extends AppCompatTextView {
|
||||
this.mExpandCallback = callback;
|
||||
}
|
||||
|
||||
public void setShrankTextAndExpandedText(CharSequence shrankText, CharSequence expandedText) {
|
||||
mShrankText = shrankText;
|
||||
mExpandedText = expandedText;
|
||||
mShowExpandTextRegardlessOfMaxLines = !TextUtils.isEmpty(shrankText);
|
||||
|
||||
if (!mIsExpanded && mShowExpandTextRegardlessOfMaxLines) {
|
||||
setText(mShrankText);
|
||||
} else {
|
||||
setText(mExpandedText);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setText(CharSequence text, BufferType type) {
|
||||
mInitLayout = true;
|
||||
@ -139,7 +141,7 @@ public class ExpandTextView extends AppCompatTextView {
|
||||
if (viewWidth - lastLineRight > expandTextWidth) {
|
||||
if (mUseGradientAlphaEndText) {
|
||||
finalEndText = content.toString()
|
||||
.substring(content.length() - additionalEndTextCount, content.length()) + mEndText;
|
||||
.substring(content.length() - additionalEndTextCount, content.length()) + mEndText;
|
||||
finalEndText = finalEndText.replace("\n", "");
|
||||
|
||||
content = content.subSequence(0, content.length() - additionalEndTextCount) + finalEndText + mExpandText;
|
||||
@ -198,7 +200,11 @@ public class ExpandTextView extends AppCompatTextView {
|
||||
public void onClick(@NonNull View widget) {
|
||||
mIsExpanded = true;
|
||||
setMaxLines(Integer.MAX_VALUE);
|
||||
setText(mSnapshotText);
|
||||
if (mShowExpandTextRegardlessOfMaxLines) {
|
||||
setText(mExpandedText);
|
||||
} else {
|
||||
setText(mSnapshotText);
|
||||
}
|
||||
|
||||
if (mExpandCallback != null) {
|
||||
mExpandCallback.onExpand();
|
||||
@ -229,7 +235,7 @@ public class ExpandTextView extends AppCompatTextView {
|
||||
|
||||
if (getMeasuredHeight() == getLayout().getHeight() - heightBetweenLastVisibleLineRectAndLastActualLineRect) {
|
||||
result = mLastVisibleLineRect.bottom - (lastVisibleLineBaseline + layout.getPaint()
|
||||
.getFontMetricsInt().descent + getPaddingBottom());
|
||||
.getFontMetricsInt().descent + getPaddingBottom());
|
||||
if (getLineSpacingExtra() > result) {
|
||||
result = 0;
|
||||
} else {
|
||||
@ -240,18 +246,6 @@ 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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 此方法仅更改标记,不做实际展开的操作
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user