saket / Better-Link-Movement-Method

Attempts to improve how clickable links are detected, highlighted and handled in TextView
Apache License 2.0
782 stars 77 forks source link

Other click view not clickable #30

Open nAkhmedov opened 4 years ago

nAkhmedov commented 4 years ago

When i use this lib in adapter, onClick/onLongClick events are working for only links. If i type simple text and tring to click or long click no action, even parent view does not handle these actions.

viewHolder.itemView.messageTv?.let {
it.movementMethod = BetterLinkMovementMethod.getInstance()
Linkify.addLinks(it, Linkify.WEB_URLS)
BetterLinkMovementMethod
    .linkify(Linkify.WEB_URLS, it)
    .setOnLinkClickListener { _: TextView?, url: String? ->
        val uri = Uri.parse(url)
        val intent = Intent(Intent.ACTION_VIEW, uri)
        intent.putExtra(Browser.EXTRA_APPLICATION_ID, context.packageName)
        if (intent.resolveActivity(context.packageManager) != null) {
            context.startActivity(intent)
        }
        true
    }
    .setOnLinkLongClickListener { _: TextView?, _: String? ->
        context.onItemLongClick(viewHolder.adapterPosition)
        true
    }
}   
saket commented 4 years ago

Does it work if you replace BetterLinkMovementMethod with the framework LinkMovementMethod?

nAkhmedov commented 4 years ago

Thank you for response. Nope, click and long click events are working when i set to TextView android:autolink="web" in xml(without above java codes).

saket commented 4 years ago

Does it work if you replace BetterLinkMovementMethod with the framework LinkMovementMethod?

I meant:

textview.movementMethod = LinkMovementMethod.getInstance()
nAkhmedov commented 4 years ago

I understand you, no, it is not working with LinkMovementMethod class.

saket commented 4 years ago

Whoops then it's unfortunately outside the control of BetterLinkMovementMethod

nAkhmedov commented 4 years ago

Do you have any suggestions on this issue?

saket commented 4 years ago

Try overriding its touch listener?

Shunaylov commented 4 years ago

So? How to handle clicks over a TextView outside links?

Shunaylov commented 4 years ago

@nAkhmedov did u got any solution?

skrugly commented 3 years ago

Here's a snippet of possible solution:

var wasLinkClicked = false
val detector = GestureDetectorCompat(context, object : GestureDetector.SimpleOnGestureListener() {
                override fun onSingleTapUp(e: MotionEvent): Boolean {
                    if (!wasLinkClicked) {
                        // handle click here
                    }
                    return true
                }
            })

            val myMovementMethod = object : BetterLinkMovementMethod() {
                override fun onTouchEvent(textView: TextView, text: Spannable, event: MotionEvent): Boolean {
                    val touchHandled = super.onTouchEvent(textView, text, event)

                    if (event.action == MotionEvent.ACTION_DOWN) {
                        wasLinkClicked = touchHandled
                    }
                    detector.onTouchEvent(event)
                    return true
                }
            }
            textView.movementMethod = myMovementMethod