本文介绍了Android的TextView的Linkify拦截与父视图的手势的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在的问题是,如果我Linkify TextView的的underliyng滚动型不听扫手势我setted.Is有没有办法让Linkify不与underliyng视图的手势搞乱?我试图重写的onTouchEvent并返回false来ACTION_MOVE但滚动型的手势需要ACTION_DOWN和ACTION_UP事件的功能。有没有一种方法来实现这一目标?

The problem is that if i Linkify the textView the underliyng ScrollView don't listen the sweep Gestures I've setted.Is there a way to have Linkify without messing with the underliyng view's gestures?I tried to override ontouchEvent and return false to ACTION_MOVE but the scrollview's gesture needs the ACTION_DOWN and ACTION_UP event to function. Is there a way to achieve that?

推荐答案

Linkify 适用于movementMethod到TextView的 LinkMovementMethod 。该运动的方法以为它实现它覆盖其他滚动方法的家长有一个垂直滚动的方法。虽然的TouchEvent 可dispached到父,具体父滚动型所需的整个序列 ACTION_DOWN ACTION_MOVE ACTION_UP 来执行(扫描检测)。

Linkify applies to a movementMethod to the textView LinkMovementMethod. That movement method thought it implements a scrolling vertically method it overrides any other scrolling method the parent has. Although touchEvent can be dispached to the parent, the specific parent ScrollView needed the whole sequence ACTION_DOWN , ACTION_MOVE, ACTION_UP to perform (sweep detection).

因此​​,解决我的问题是后Linkify删除TextView中的滚动方法和处理在的onTouchEvent LinkMovementMethod 链路检测行动$ c中的TextView中的>。

So the solution to my problem is after Linkify to remove the textView's scrolling method and handle the LinkMovementMethod link detection action in onTouchEvent of the textView.

@override
public boolean onTouchEvent(MotionEvent event) {
        TextView widget = (TextView) this;
        Object text = widget.getText();
        if (text instanceof Spanned) {
            Spannable buffer = (Spannable) text;

            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]));
                    }
                    return true;
                }
            }

        }

        return false;
    }

这种方式,我有Link_Click检测(仅与用户进行接触的环节,而不是整个TextView的),我没有整LinkMovementMethod。

This way i have the Link_Click detection (performed only with the user touches the link and not the whole textview) and i don't have the whole LinkMovementMethod.

这篇关于Android的TextView的Linkify拦截与父视图的手势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 21:46