Android 疑难杂症-网页链接显示问题
疑难点
- TextView网页可点击链接的时候,发生了设置setMovementMethod,内容显示不出来的问题
- TextView 网页链接取消下划线的问题
解决方案
1.布局文件里TextView不能出现gravity,否则就显示不出来。
2.要设置自定义的MovementMethod方法
mTvShortContent.setMovementMethod( ExpandableTextView.LocalLinkMovementMethod.getInstance());
public static class LocalLinkMovementMethod extends LinkMovementMethod {
static LocalLinkMovementMethod sInstance;
public static LocalLinkMovementMethod getInstance() {
if (sInstance == null) {
sInstance = new LocalLinkMovementMethod();
}
return sInstance;
}
@Override
public boolean onTouchEvent(TextView widget,
Spannable buffer, MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_UP ||
action == MotionEvent.ACTION_DOWN) {
int x = (int) event.getX();
int y = (int) event.getY();
x -= widget.getTotalPaddingLeft();
y -= widget.getTotalPaddingTop();
x += widget.getScrollX();
y += widget.getScrollY();
Layout layout = widget.getLayout();
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);
//详情界面
// ClickableSpan[] link = buffer.getSpans(
// off, off, ClickableSpan.class);
//
// if (link.length != 0) {
// if (action == MotionEvent.ACTION_UP) {
// link[0].onClick(widget);
// } else if (action == MotionEvent.ACTION_DOWN) {
// Selection.setSelection(buffer,
// buffer.getSpanStart(link[0]),
// buffer.getSpanEnd(link[0]));
// }
//
// if (widget instanceof ExpandableTextView) {
// ((ExpandableTextView) widget).linkHit = true;
// }
// return true;
// } else {
// Selection.removeSelection(buffer);
// Touch.onTouchEvent(widget, buffer, event);
// return false;
// }
//feed 流使用 APPSQ-19608
ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);
if (link.length != 0) {
if (action == MotionEvent.ACTION_UP) {
//除了点击事件,我们不要其他东西
link[0].onClick(widget);
}
return true;
}
}
return Touch.onTouchEvent(widget, buffer, event);
}
}
3.把底部下划线去掉
builder.setSpan(new ClickableSpan() {
@Override
public void onClick(@NonNull View widget) {
LoggerUtils.d("视频详情 超链接点击11111111111111111");
ExpandableLinkHelper.clickLinkTag(data.getUrl(), urlRule, context);
}
@Override
public void updateDrawState(TextPaint ds) {
ds.setColor(SdkUtil.getColor(context, R.color.order_blue));
ds.setUnderlineText(false);
}
}, data.getStart() + 1, data.getEnd(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);