简体   繁体   English

为什么即使没有滚动,调用Android ListView的setOnScrollListener也会立即调用侦听器?

[英]Why does calling Android ListView's setOnScrollListener immediately call the listener even though there was no scrolling?

Setting a scroll listener on an Android AbsListView causes that listener to be invoked immediately. 在Android AbsListView上设置滚动侦听器会导致该侦听器立即被调用。 This is clearly intentional from the source of AbsListView.java : 显然,这是来自AbsListView.java的源AbsListView.java

public void setOnScrollListener(OnScrollListener l) {
    mOnScrollListener = l;
    invokeOnItemScrollListener();
}

Why would anyone want this? 为什么有人要这个?

Setting a click/touch listener doesn't cause the listener to be called immediately, why are scroll listeners different? 设置单击/触摸侦听器不会导致立即调用该侦听器,为什么滚动侦听器有所不同? Is there some reasonable rationale for this that I'm missing? 我是否缺少一些合理的理由?

Related question in which someone's bitten by this weird behavior: onScroll gets called when I set listView.onScrollListener(this), but without any touch 有人被这种奇怪的行为咬伤的相关问题: 当我设置listView.onScrollListener(this)时,onScroll被调用,但是没有任何接触

So that the scroll listener is informed of the initial position of the list. 因此,滚动侦听器会被告知列表的初始位置。 Because a list is always scrolled to somewhere (even if just 0) while clicks and touches aren't always occurring. 因为列表总是滚动到某个位置(即使只有0),但单击和触摸并不总是发生。

In the end it was just a design decision where they could have gone either way. 最终,这只是设计决定,他们可能会选择哪种方式。 Its not an uncommon one- lots of GUI frameworks will immediately call listeners on registration if there is data available. 如果有可用数据,很多GUI框架会在注册时立即调用侦听器,这并不罕见。 If that screws you up, code your listener to ignore the first call. 如果您不满意,请对您的监听器进行编码以忽略第一个呼叫。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM