package com.xty.common

import android.text.Spannable
import android.text.method.LinkMovementMethod
import android.view.MotionEvent
import android.view.ViewGroup
import android.view.ViewParent
import android.widget.TextView

class MyLinkedMovementMethod: LinkMovementMethod() {
    private var sInstance: MyLinkedMovementMethod? = null
    fun getInstance(): MyLinkedMovementMethod? {
        if (sInstance == null) sInstance = MyLinkedMovementMethod()
        return sInstance
    }

    override fun onTouchEvent(widget: TextView, buffer: Spannable?, event: MotionEvent): Boolean {
        // 因为TextView没有点击事件，所以点击TextView的非富文本时，super.onTouchEvent()返回false；
        // 此时可以让TextView的父容器执行点击事件；
        val isConsume = super.onTouchEvent(widget, buffer, event)
        if (!isConsume && event.getAction() == MotionEvent.ACTION_UP) {
            val parent: ViewParent = widget.getParent()
            if (parent is ViewGroup) {
                // 获取被点击控件的父容器，让父容器执行点击；
                (parent as ViewGroup).performClick()
            }
        }
        return isConsume
    }
}