gpt4 book ai didi

android - LinkMovementMethod 僵硬滚动

转载 作者:行者123 更新时间:2023-11-29 16:00:11 30 4
gpt4 key购买 nike

我有一个只有 TextView 的 Activity 。 TextView 很长(需要垂直滚动)并且有一些链接。

为了滚动和拦截链接,我有以下代码:

TextView textView = (TextView) getView().findViewById(R.id.help_text_view);
textView.setMovementMethod(LinkMovementMethod.getInstance());

问题是滚动是“僵硬的”,也就是说,您不能像在大多数 View 中所期望的那样上下移动文本。只要抬起手指/光标,文本就会停止移动。

如何恢复正常的 Fling 功能?

最佳答案

我的解决方案是创建自己的 MovementMethod 并自己实现点击

private MovementMethod createMovementMethod ( Context context ) {
final GestureDetector detector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp ( MotionEvent e ) {
return true;
}

@Override
public boolean onSingleTapConfirmed ( MotionEvent e ) {
return true;
}
});
return new ScrollingMovementMethod() {
@Override
public boolean onTouchEvent ( @NotNull TextView widget, @NotNull Spannable buffer, @NotNull MotionEvent event ) {
// check if event is a single tab
boolean isClickEvent = detector.onTouchEvent(event);

// detect span that was clicked
if (isClickEvent) {
int x = (int) event.getX();
int y = (int) event.getY();

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);

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

if (link.length != 0) {
// execute click only for first clickable span
// can be a for each loop to execute every one
link[0].onClick(widget);
return true;
}
}

// let scroll movement handle the touch
return super.onTouchEvent(widget, buffer, event);
}
};
}

关于android - LinkMovementMethod 僵硬滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25145898/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com