Search code examples
androidandroid-viewonclicklistenerandroid-4.0-ice-cream-sandwichclickablespan

ClickableSpan avoid touch propagation to parent view


I have a SpannableString set as the text of my TextView which is inside another view that has a click handler. I've got ClickableSpans inside my text which are, you are right, clickable.

My problem is that I need touch events to be captured in case of a click inside the clickable span and not to propagate to parent view (as the parent has another click handler).

The container view is simply a feed of posts in my app, and those posts can contain hashtags/mentions/links etc. If a user taps a hashtag, they should go to the hashtag which is handled by the clickable span, otherwise, they should go to the post, which is handled by the container itself.

Is there any straightforward mechanism to implement this functionality?


Solution

  • I've came up with a terrible, anti-pattern solution that works well.

    In my app class, I've defined:

    public static boolean shouldIgnoreNextTouchEvent = false;
    

    In my ClickableSpans click handler, I've set a global flag to avoid next touch event to true:

    ClickableSpan clickableSpan = new ClickableSpan() {
        @Override
        public void onClick(View widget) {
            App.shouldIgnoreNextTouchEvent = true;
            ...
        }
     }
    

    Then in my parent view's handler:

    @Override
    public void onClick() {
        if(App.shouldIgnoreNextTouchEvent){
            App.shouldIgnoreNextTouchEvent = false;
            return;
        }
        ...
    }
    

    I know it's not a good solution but it works like a charm.