如何区分Android ScrollView的最终用户滚动与编程滚动?

如何区分Android ScrollView的最终用户滚动与编程滚动?,第1张

概述背景:我有一个ScrollView / TextView对,它接收来自外部源的间歇性文本流.它会在每次更新时自动滚动到底部. 我希望用户能够通过手动滚动到某个地方来突破自动向下滚动模式,但是我不清楚如何将手动滚动与我自己编写的程序滚动区分开来. 我的UI更新在计时器上运行以缓冲重绘: private Handler outputUpdater = new Handler ();private s 背景:我有一个ScrollVIEw / TextVIEw对,它接收来自外部源的间歇性文本流.它会在每次更新时自动滚动到底部.

我希望用户能够通过手动滚动到某个地方来突破自动向下滚动模式,但是我不清楚如何将手动滚动与我自己编写的程序滚动区分开来.

我的UI更新在计时器上运行以缓冲重绘:

private Handler outputUpdater = new Handler ();private static String outputBuffer = "";private static boolean outputHasChanged = false;private static final Object lock = new Object ();private Runnable outputUpdaterTask = new Runnable () {    public voID run () {        synchronized (lock) {            // if the output has changed,update the TextVIEw            if (outputHasChanged) {                TextVIEw tv = (TextVIEw) findVIEwByID (R.ID.textVIEw);                tv.setText (outputBuffer);            }            // if the output has changed,or the scroll hasn't reached the bottom yet            // then keep scrolling down            if (outputHasChanged || !scrollAtBottom ()) {                ScrollVIEw sv = (ScrollVIEw) findVIEwByID (R.ID.scrollVIEw);                sv.fullScroll (VIEw.FOCUS_DOWN);            }            outputHasChanged = false;        }        outputUpdater.postDelayed (this,100);    }};

scrollAtBottom从onScrollChanged处理程序获取它的值.

一切正常.即使没有文本更新也需要调用fullScroll,因为如果有TextVIEw更新,或者虚拟键盘可见性发生变化等,对fullScroll的单次调用并不总是到底.

我希望如果用户手动滚动,我可以知道决定停止调用fullScroll.

不幸的是,仅仅将“从底部,自动模式”到“不在底部”的转换视为切换到手动模式的提示似乎还不够,因为各种UI更改似乎也将滚动视图从底部移开(例如虚拟键盘显示).

问题重述:
如何区分用户启动的滚动与编程滚动?

解决方法 您是否尝试过使用ontouchEvent的布尔值,类似于:
boolean userIntercept = false;@OverrIDepublic boolean ontouchEvent(MotionEvent me) {    int action = me.getAction();    if (action == MotionEvent.ACTION_MOVE) {            userIntercept = true;    }    return super.ontouchEvent(me);}

然后在你的outputUpdaterTask中:

// if the output has changed,or the scroll hasn't reached the bottom yet// then keep scrolling downif (outputHasChanged || !scrollAtBottom () && !userIntercept) {    ScrollVIEw sv = (ScrollVIEw) findVIEwByID (R.ID.scrollVIEw);    sv.fullScroll (VIEw.FOCUS_DOWN);}

您只需要确定一种方法,即将userIntercept返回到false,以最适合您的应用程序的方式.

总结

以上是内存溢出为你收集整理的如何区分Android ScrollView的最终用户滚动与编程滚动?全部内容,希望文章能够帮你解决如何区分Android ScrollView的最终用户滚动与编程滚动?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1131120.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存