Fix ExpandableTextView recursively call setText (#2252)
This commit is contained in:
parent
fd1c51e027
commit
f5aee25427
|
|
@ -19,6 +19,7 @@
|
|||
|
||||
package org.lsposed.manager.ui.widget;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.graphics.Typeface;
|
||||
import android.os.Bundle;
|
||||
|
|
@ -49,6 +50,7 @@ public class ExpandableTextView extends MaterialTextView {
|
|||
private final SpannableString collapse;
|
||||
private final SpannableString expand;
|
||||
private final SpannableStringBuilder sb = new SpannableStringBuilder();
|
||||
private int lineCount = 0;
|
||||
|
||||
public ExpandableTextView(Context context) {
|
||||
this(context, null);
|
||||
|
|
@ -89,29 +91,44 @@ public class ExpandableTextView extends MaterialTextView {
|
|||
|
||||
@Override
|
||||
public boolean onPreDraw() {
|
||||
var lineCount = getLayout().getLineCount();
|
||||
this.getViewTreeObserver().removeOnPreDrawListener(this);
|
||||
if (lineCount == 0) {
|
||||
lineCount = getLayout().getLineCount();
|
||||
}
|
||||
if (lineCount > maxLines) {
|
||||
SpannableString s;
|
||||
int end;
|
||||
int hintTextOffsetEnd;
|
||||
if (maxLines == getMaxLines()) {
|
||||
nextLines = lineCount + 1;
|
||||
end = getLayout().getLineStart(getMaxLines() - 1);
|
||||
s = expand;
|
||||
} else {
|
||||
hintTextOffsetEnd = getLayout().getLineStart(getMaxLines() - 1);
|
||||
setTextWithSpan(text, hintTextOffsetEnd - 1, expand);
|
||||
} else if (nextLines == getMaxLines()) {
|
||||
nextLines = maxLines;
|
||||
end = text.length() + 1;
|
||||
s = collapse;
|
||||
hintTextOffsetEnd = getLayout().getLineStart(getMaxLines() - 1);
|
||||
setTextWithSpan(text, hintTextOffsetEnd, collapse);
|
||||
}
|
||||
sb.clearSpans();
|
||||
sb.clear();
|
||||
sb.append(text, 0, end - 1);
|
||||
sb.append("\n");
|
||||
sb.append(s);
|
||||
super.setText(sb, BufferType.NORMAL);
|
||||
}
|
||||
return super.onPreDraw();
|
||||
}
|
||||
|
||||
private void setTextWithSpan(CharSequence text, int textOffsetEnd,
|
||||
SpannableString sbStr) {
|
||||
sb.clearSpans();
|
||||
sb.clear();
|
||||
sb.append(text, 0, textOffsetEnd);
|
||||
sb.append("\n");
|
||||
sb.append(sbStr);
|
||||
super.setText(sb, BufferType.NORMAL);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
|
||||
super.onLayout(changed, left, top, right, bottom);
|
||||
if (getLayout() != null) {
|
||||
lineCount = getLayout().getLineCount();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressLint("ClickableViewAccessibility")
|
||||
@Override
|
||||
public boolean onTouchEvent(@NonNull MotionEvent event) {
|
||||
Layout layout = this.getLayout();
|
||||
|
|
@ -127,7 +144,6 @@ public class ExpandableTextView extends MaterialTextView {
|
|||
if (links.length == 0) {
|
||||
return false;
|
||||
} else {
|
||||
performClick();
|
||||
return super.onTouchEvent(event);
|
||||
}
|
||||
}
|
||||
|
|
@ -136,11 +152,6 @@ public class ExpandableTextView extends MaterialTextView {
|
|||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean performClick() {
|
||||
return super.performClick();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Parcelable onSaveInstanceState() {
|
||||
Bundle bundle = new Bundle();
|
||||
|
|
|
|||
Loading…
Reference in New Issue