2011-08-29 12 views
7

El problema es que si Enlace el texto vea la imagen subyacente ScrollView no escuche los gestos de barrido que he establecido. ¿Hay alguna forma de vincular sin interferir con los gestos de la vista subyacente? Intenté anular ontouchEvent y devolver falso a ACTION_MOVE pero el gesto scrollview necesita el evento ACTION_DOWN y ACTION_UP para funcionar. ¿Hay alguna manera de lograr eso?Android TextView Enlace las interceptaciones con gestos de vista padre

Respuesta

44

Linkify se aplica a un movimientoMétodo del textoView LinkMovementMethod. Ese método de movimiento pensó que implementa un método de desplazamiento vertical que anula cualquier otro método de desplazamiento que tenga el padre. Aunque touchEvent se puede enviar a la matriz, el elemento primario específico ScrollView necesitaba toda la secuencia ACTION_DOWN, ACTION_MOVE, ACTION_UP para realizar (detección de barrido).

Así que la solución a mi problema es después de vincular para eliminar el método de desplazamiento de textView y manejar la acción de detección de enlace LinkMovementMethod en onTouchEvent de 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; 
    } 

esta manera tengo la detección Link_Click (realizado sólo con el usuario toca el enlace y no toda la TextView) y no tener toda la LinkMovementMethod.

+1

Un millón de agradecimientos para esta respuesta. –

+0

me alegro de que finalmente ayudó a alguien: D – weakwire

+0

gran respuesta compañero! – Maurycy

0

hay una pequeña cosa mala que el método Vista de Texto :: setText (...) la utilización de la bandera autoLink,

if (mAutoLinkMask != 0) { 
     Spannable s2; 

     if (type == BufferType.EDITABLE || text instanceof Spannable) { 
      s2 = (Spannable) text; 
     } else { 
      s2 = mSpannableFactory.newSpannable(text); 
     } 

     if (Linkify.addLinks(s2, mAutoLinkMask)) { 
      text = s2; 
      type = (type == BufferType.EDITABLE) ? BufferType.EDITABLE : BufferType.SPANNABLE; 

      /* 
      * We must go ahead and set the text before changing the 
      * movement method, because setMovementMethod() may call 
      * setText() again to try to upgrade the buffer type. 
      */ 
      mText = text; 

      // Do not change the movement method for text that support text selection as it 
      // would prevent an arbitrary cursor displacement. 
      if (mLinksClickable && !textCanBeSelected()) { 
       setMovementMethod(LinkMovementMethod.getInstance()); 
      } 
     } 
    } 

así que pasé un tiempo para entender , por qué estoy deshabilitando enlaces en ListView artículo, pero a veces obtiene un enlace!

Tiene que establecer que la bandera en el valor necesario y luego llamar a un setText (...)

+0

No está nada claro qué estás diciendo aquí. ¿Es esta una respuesta a la pregunta o estás tratando de hacer una nueva pregunta? – SuperBiasedMan

+0

Es principalmente una nota, porque encontré este tema cuando busqué el problema "Vincular vista de texto en ListView" – Mike