diff options
author | Maurice Lam <yukl@google.com> | 2017-04-21 00:01:43 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2017-04-21 00:01:43 +0000 |
commit | 31d308a933f9472cf5f56eb0c10febb77e793b8f (patch) | |
tree | 78f541fc1c2f6cdbc97522a44b1a7c65561c4307 /library/main/src | |
parent | 1a8259d4fb0a29ca334ac8728ef9ac9e4241f056 (diff) | |
parent | 51f4609c813c2336bd12eaf7d6a719fd7f2fc5f2 (diff) | |
download | setupwizard-31d308a933f9472cf5f56eb0c10febb77e793b8f.tar.gz |
Merge "Allow set LinkSpan click listener on RichTextView"
Diffstat (limited to 'library/main/src')
-rw-r--r-- | library/main/src/com/android/setupwizardlib/span/LinkSpan.java | 48 |
1 files changed, 42 insertions, 6 deletions
diff --git a/library/main/src/com/android/setupwizardlib/span/LinkSpan.java b/library/main/src/com/android/setupwizardlib/span/LinkSpan.java index 89ffff7..a5f0424 100644 --- a/library/main/src/com/android/setupwizardlib/span/LinkSpan.java +++ b/library/main/src/com/android/setupwizardlib/span/LinkSpan.java @@ -28,8 +28,8 @@ import android.view.View; /** * A clickable span that will listen for click events and send it back to the context. To use this - * class, implement {@link com.android.setupwizardlib.span.LinkSpan.OnClickListener} in your - * context (typically your Activity). + * class, implement {@link OnLinkClickListener} in your TextView, or use + * {@link com.android.setupwizardlib.view.RichTextView#setOnClickListener(View.OnClickListener)}. * * <p />Note on accessibility: For TalkBack to be able to traverse and interact with the links, you * should use {@code LinkAccessibilityHelper} in your {@code TextView} subclass. Optionally you can @@ -51,10 +51,29 @@ public class LinkSpan extends ClickableSpan { private static final Typeface TYPEFACE_MEDIUM = Typeface.create("sans-serif-medium", Typeface.NORMAL); + /** + * @deprecated Use {@link OnLinkClickListener} + */ + @Deprecated public interface OnClickListener { void onClick(LinkSpan span); } + /** + * Listener that is invoked when a link span is clicked. If the containing view of this span + * implements this interface, this will be invoked when the link is clicked. + */ + public interface OnLinkClickListener { + + /** + * Called when a link has been clicked. + * + * @param span The span that was clicked. + * @return True if the click was handled, stopping further propagation of the click event. + */ + boolean onLinkClick(LinkSpan span); + } + /* non-static section */ private final String mId; @@ -65,9 +84,7 @@ public class LinkSpan extends ClickableSpan { @Override public void onClick(View view) { - final OnClickListener listener = getListenerFromContext(view.getContext()); - if (listener != null) { - listener.onClick(this); + if (dispatchClick(view)) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { view.cancelPendingInputEvents(); } @@ -76,8 +93,27 @@ public class LinkSpan extends ClickableSpan { } } + private boolean dispatchClick(View view) { + boolean handled = false; + if (view instanceof OnLinkClickListener) { + handled = ((OnLinkClickListener) view).onLinkClick(this); + } + if (!handled) { + final OnClickListener listener = getLegacyListenerFromContext(view.getContext()); + if (listener != null) { + listener.onClick(this); + handled = true; + } + } + return handled; + } + + /** + * @deprecated Deprecated together with {@link OnClickListener} + */ @Nullable - private OnClickListener getListenerFromContext(@Nullable Context context) { + @Deprecated + private OnClickListener getLegacyListenerFromContext(@Nullable Context context) { while (true) { if (context instanceof OnClickListener) { return (OnClickListener) context; |