Closed rubengees closed 5 years ago
Oh yea this is really annoying. I'm doing something less gross in my app:
class SomeViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val bodyTextView: TextView
@SuppressLint("ClickableViewAccessibility")
fun forwardTouchEventsToBackground(linkMovementMethod: BetterLinkMovementMethod) {
// Bug workaround: TextView with clickable spans consume all touch events. Manually
// transfer them to the parent so that the background touch indicator shows up +
// click listener works.
bodyTextView!!.setOnTouchListener { o, event ->
val handledByMovementMethod = linkMovementMethod.onTouchEvent(bodyTextView, bodyTextView.text as Spannable, event)
handledByMovementMethod || itemView.onTouchEvent(event)
}
}
}
Maybe we can generalize this solution without explicitly knowing about the existence of BetterLinkMovementMethod
?
Hey @saket !
Maybe BetterLinkMovementMethod
can capture click event and call TextView#performClick
?
Oh hey @noties. Sorry i just saw your message. I was busy attending droidcon Berlin and roaming around Europe. Capturing TextView's click events sounds like an interesting idea. I will explore this, thanks!
Thank you so much! The problem of how to pass up textview clicks to the parent has been troubling people for years. @saket your solution works great :)
Closing this issue. Please feel free to re-open it for more questions.
Was there a solution to passing touch events to parent views when the user taps somewhere on the textview that isn't a link?
@chankruse does the solution in this comment not work? https://github.com/saket/Better-Link-Movement-Method/issues/14#issuecomment-388698953
@saket solution works great. It would be better to write in a TextView.
public class LinkifyTextView extends TextView {
public LinkifyTextView(Context context) {
super(context);
}
public LinkifyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public LinkifyTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public LinkifyTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
final MovementMethod movementMethod = getMovementMethod();
final CharSequence text = getText();
if (movementMethod instanceof BetterLinkMovementMethod && text instanceof Spannable) {
return movementMethod.onTouchEvent(this, (Spannable) text, event) || super.onTouchEvent(event);
}
return super.dispatchTouchEvent(event);
}
One of my apps requires a click listener on the
TextView
and/or parent views (e.g. in aRecyclerView
) and also has linkified text in it.After the
MovementMethod
is applied, the click listeners do not work anymore. This is an age-old bug, but there are workarounds.A similar library solves this by having a custom
TextView
that interceptsonTouch
events and handles them differently when a touch on a link is in progress or not.Maybe a similar approach can be introduced to this library? Currently i am using this gross hack:
This could be improved by having a first class api if a click is in progress and/or by having a
TextView
doing this in the library.Thanks for looking into this!