8

我有一个带有 TextViews 的 RecyclerView,它可能包含应该可以点击的自定义主题标签。所以我创建了 TextView 的子类,在其中使用 Pattern 我创建了 ClickableSpan。为了使 ClickableSpan 处于活动状态,我添加了

setMovementMethod(LinkMovementMethod.getInstance())

此方法更改 TextView 的属性:

setFocusable(true);
setClickable(true);
setLongClickable(true);

单击链接有效,但它会阻止在列表项上显示可绘制的波纹,并且单击标签之外的 TextView 将被忽略。

所以我很感兴趣如何将 TextView 上的触摸(除了主题标签)重定向到它的父级?

4

2 回答 2

5

感谢维克托——

我使用它来允许不在可点击范围内的触摸事件传递给父级

override fun dispatchTouchEvent(event: MotionEvent?): Boolean {

        if (hasOnClickListeners() == false && linksClickable == false) {
            // Avoid all touches
            return false
        }

        // Handle avoiding touch events, if not on a clickable span
        if (event != null && hasOnClickListeners() == false) {
            if (layout != null) {
                val line = layout.getLineForVertical(event.y.toInt())
                val offset = layout.getOffsetForHorizontal(line, event.x)

                if (text != null && text is Spanned) {
                    val spanned = text as Spanned

                    val spansAtPoint = spanned.getSpans(offset, offset, ClickableSpan::class.java)
                    if (spansAtPoint.size == 0) {
                        // No clickable span at event -- abort touch
                        return false
                    }
                }
            }
        }
        return super.dispatchTouchEvent(event)
    }
于 2019-06-04T15:16:19.680 回答
4

所以我没有在互联网上找到解决方案,所以我最终自己实现了它。

因此,我没有使用带有 MovementMethod 的简单 TextView,而是创建了一个使用 onTouchEvent 扩展文本视图的新类。

public class MyTextView extends TextView {
@Override
  public boolean onTouchEvent(@NonNull MotionEvent event) {
    if (linksActive) {
      Layout layout = this.getLayout();
      if (layout != null) {
        int line = layout.getLineForVertical((int) event.getY());
        int offset = layout.getOffsetForHorizontal(line, event.getX());

        if (getText() != null && getText() instanceof Spanned) {
          Spanned spanned = (Spanned) getText();

          ClickableSpan[] links = spanned.getSpans(offset, offset, ClickableSpan.class);

          if (links.length > 0) {

            if (event.getAction() == MotionEvent.ACTION_DOWN) {
              return true;
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
              links[0].onClick(this);
            } else {
              return super.onTouchEvent(event);
            }
          }
        }
      }
    }

    return super.onTouchEvent(event);
  }
}

我还创建了一个扩展 ClickableSpan 处理点击的类:

public class URLSpan extends ClickableSpan {
  private String url;
  private int spanType;

  public URLSpan(String url, int spanType) {
    this.url = url;
    this.spanType = spanType;
  }

  public int getType() {
    return spanType;
  }

  @Override
  public void onClick(@NonNull View widget) {
    if (widget instanceof OnSpanClickListener) {
      ((OnSpanClickListener) widget).onSpanClicked(url, type);
    } else {
      // ignored
    }
  }

}

在这种情况下,我必须自己管理跨度(在setText()方法中)

public static void formatPhoneNumbers(@NonNull Spannable spannable, @ColorInt int color) {
    Matcher matcher = MY_Patterns.PHONE_NO.matcher(spannable);
    while (matcher.find()) {
      if (!Linkify.sPhoneNumberMatchFilter.acceptMatch(spannable, matcher.start(), matcher.end())) {
        continue;
      }

      removeSpans(spannable, SpanType.PHONE_NO, matcher.start(), matcher.end());

      if (hasClickableSpans(spannable, matcher.start(), matcher.end())) {
        // ignore different clickable spans overlap
        continue;
      }

      final ForegroundColorSpan span = new ForegroundColorSpan(color);
      final ClickableSpan urlSpan = new URLSpan(matcher.group(), SpanType.PHONE_NO);

      spannable.setSpan(urlSpan, matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
      spannable.setSpan(span, matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
  }

  private static void removeSpans(@NonNull Spannable spannable, int type, int start, int end) {
    URLSpan[] arr = spannable.getSpans(start, end, URLSpan.class);
    for (URLSpan span : arr) {
      if (span.getType() == type) {
        spannable.removeSpan(span);
      }
    }
  }

  private static boolean hasClickableSpans(@NonNull Spannable spannable, int start, int end) {
    ClickableSpan[] arr = spannable.getSpans(start, end, ClickableSpan.class);
    return arr.length > 0;
  }
于 2015-12-27T11:12:49.420 回答