Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Android.App;
- using Android.Content;
- using Android.OS;
- using Android.Runtime;
- using Android.Views;
- using Android.Widget;
- using Android.Util;
- using Android.Text;
- using Android.Text.Method;
- using Android.Text.Style;
- namespace Android.TweetBox.Views
- {
- public class TextViewWithUrls : TextView
- {
- public bool DontConsumeNonUrlClicks = true;
- public bool LinkHit;
- public TextViewWithUrls (Context context) : base(context)
- {
- }
- public TextViewWithUrls (Context context, IAttributeSet attrs) : base(context, attrs)
- {
- }
- public TextViewWithUrls (Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
- {
- }
- public override bool OnTouchEvent (MotionEvent e)
- {
- LinkHit = false;
- bool res = base.OnTouchEvent (e);
- if (DontConsumeNonUrlClicks)
- return LinkHit;
- return res;
- }
- }
- public class LocalLinkMovementMethod : LinkMovementMethod
- {
- public static LocalLinkMovementMethod sInstance;
- public static LocalLinkMovementMethod GetInstance ()
- {
- if (sInstance == null)
- sInstance = new LocalLinkMovementMethod ();
- return sInstance;
- }
- public override bool OnTouchEvent (TextView widget, Android.Text.ISpannable buffer, MotionEvent e)
- {
- MotionEventActions action = e.Action;
- if (action == MotionEventActions.Up || action == MotionEventActions.Down) {
- int x = (int)e.GetX ();
- int y = (int)e.GetY ();
- x -= widget.PaddingLeft;
- y -= widget.PaddingTop;
- x += widget.ScrollX;
- y += widget.ScrollY;
- Layout layout = widget.Layout;
- int line = layout.GetLineForVertical (y);
- int off = layout.GetOffsetForHorizontal (line, x);
- Object[] link = buffer.GetSpans (off, off, Java.Lang.Class.FromType (typeof(ClickableSpan)));
- if (link.Length > 0) {
- Console.WriteLine ("ClickableSpan");
- if (action == MotionEventActions.Up) {
- ((ClickableSpan)link [0]).OnClick (widget);
- } else if (action == MotionEventActions.Down) {
- Selection.SetSelection (buffer, buffer.GetSpanStart ((ClickableSpan)link [0]), buffer.GetSpanEnd ((ClickableSpan)link [0]));
- }
- if (widget.GetType ().Equals (typeof(TextViewWithUrls))) {
- ((TextViewWithUrls)widget).LinkHit = true;
- }
- return true;
- } else {
- Selection.RemoveSelection (buffer);
- base.OnTouchEvent (widget, buffer, e);
- return false;
- }
- }
- return base.OnTouchEvent (widget, buffer, e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement