关闭

TextView的内部链接支持长按

383人阅读 评论(0) 收藏 举报
分类:

先创建一个支持处理长按事件的span

public class LongClickableSpan extends ClickableSpan View.OnClickListener l;{
    View.OnClickListener l;
    OnLongClickListener mOnLongClickListener = null;

    public LongClickableSpan(String text, OnClickListener l, OnLongClickListener ll, Context mContext) {
        super(text, l, mContext);
        mOnLongClickListener = ll;
    }

    @Override
    public void onClick(View v) {
        if (l != null) {
            l.onClick(v);
        }
    }

    public void onLongClick(View view) {
        if (mOnLongClickListener != null) {
            mOnLongClickListener.onLongClick(view);
        }
    }
}

再重写一个LinkMovementMethod

public class LongClickLinkMovementMethod extends LinkMovementMethod {
    private static LongClickLinkMovementMethod sInstance;
    private static int LONG_CLICK_TIME = 1000;
    private Long lastClickTime = 0l;
    private int lastX = 0;
    private int lastY = 0;
    // 本次点击是否已相应长按事件
    private boolean LongClickCalled = false;

    public static LinkMovementMethod getInstance() {
        if (sInstance == null) {
            sInstance = new LongClickLinkMovementMethod();
        }
        return sInstance;
    }

    @Override
    public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
        int action = event.getAction();
        if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_MOVE) {
            int x = (int) event.getX();
            int y = (int) event.getY();
            lastX = x;
            lastY = y;
            int deltaX = Math.abs(x - lastX);
            int deltaY = Math.abs(y - lastY);

            x -= widget.getTotalPaddingLeft();
            y -= widget.getTotalPaddingTop();

            x += widget.getScrollX();
            y += widget.getScrollY();

            Layout layout = widget.getLayout();
            int line = layout.getLineForVertical(y);
            int off = layout.getOffsetForHorizontal(line, x);

            LongClickableSpan[] link = buffer.getSpans(off, off, LongClickableSpan.class);

            if (link.length != 0) {
                if (action == MotionEvent.ACTION_UP) {
                    // 相应长按事件后不再相应点击事件
                    if (!LongClickCalled) {
                        link[0].onClick(widget);
                    }
                    LongClickCalled = false;
                } else if (action == MotionEvent.ACTION_DOWN) {
                    Selection.setSelection(buffer, buffer.getSpanStart(link[0]), buffer.getSpanEnd(link[0]));
                    lastClickTime = System.currentTimeMillis();
                } else if (action == MotionEvent.ACTION_MOVE) {
                    long delta = System.currentTimeMillis() - lastClickTime;
                    if (delta > LONG_CLICK_TIME && !LongClickCalled && deltaX < 10 && deltaY < 10) {
                        LongClickCalled = true;
                        link[0].onLongClick(widget);
                    }
                }
                return true;
            }
        } else if (action == MotionEvent.ACTION_CANCEL) {
            LongClickCalled = false;
        }

        return super.onTouchEvent(widget, buffer, event);
    }

}

使用代码:

TextView textView;
textView.setText(text);//此处text内加入LongClickableSpan的配置
textView.setMovementMethod(LongClickLinkMovementMethod.getInstance());


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:13245次
    • 积分:178
    • 等级:
    • 排名:千里之外
    • 原创:4篇
    • 转载:2篇
    • 译文:0篇
    • 评论:1条
    文章分类
    最新评论