2018-01-04 19:52:25 +01:00
|
|
|
package jp.juggler.subwaytooter.view
|
|
|
|
|
|
|
|
import android.text.Spannable
|
|
|
|
import android.text.method.LinkMovementMethod
|
|
|
|
import android.text.method.Touch
|
|
|
|
import android.text.style.ClickableSpan
|
|
|
|
import android.view.MotionEvent
|
|
|
|
import android.widget.TextView
|
|
|
|
|
2018-01-10 16:47:35 +01:00
|
|
|
object MyLinkMovementMethod : LinkMovementMethod() {
|
2018-01-04 19:52:25 +01:00
|
|
|
|
2018-01-13 16:24:51 +01:00
|
|
|
// 改行より右をタッチしても反応しないようにする
|
2018-01-04 19:52:25 +01:00
|
|
|
override fun onTouchEvent(widget : TextView, buffer : Spannable, event : MotionEvent) : Boolean {
|
|
|
|
|
|
|
|
val action = event.action
|
|
|
|
|
|
|
|
if(action != MotionEvent.ACTION_UP && action != MotionEvent.ACTION_DOWN) {
|
|
|
|
return Touch.onTouchEvent(widget, buffer, event)
|
|
|
|
}
|
|
|
|
|
|
|
|
var x = event.x.toInt()
|
|
|
|
var y = event.y.toInt()
|
|
|
|
|
|
|
|
x -= widget.totalPaddingLeft
|
|
|
|
y -= widget.totalPaddingTop
|
|
|
|
|
|
|
|
x += widget.scrollX
|
|
|
|
y += widget.scrollY
|
|
|
|
|
|
|
|
val layout = widget.layout
|
|
|
|
|
|
|
|
val line = layout.getLineForVertical(y)
|
|
|
|
if(0 <= line && line < layout.lineCount) {
|
|
|
|
|
|
|
|
val line_left = layout.getLineLeft(line)
|
|
|
|
val line_right = layout.getLineRight(line)
|
|
|
|
if(line_left <= x && x <= line_right) {
|
|
|
|
|
|
|
|
val offset = layout.getOffsetForHorizontal(line, x.toFloat())
|
|
|
|
|
|
|
|
val link = buffer.getSpans(offset, offset, ClickableSpan::class.java)
|
|
|
|
if(link != null && link.isNotEmpty()) {
|
|
|
|
|
|
|
|
if(action == MotionEvent.ACTION_UP) {
|
|
|
|
link[0].onClick(widget)
|
|
|
|
}
|
|
|
|
if(widget is MyTextView) {
|
|
|
|
widget.linkHit = true
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Touch.onTouchEvent(widget, buffer, event)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|