【问题标题】:How to determine when ListView fling is slowing down如何确定 ListView fling 何时变慢
【发布时间】:2023-03-30 02:02:01
【问题描述】:

为了提高列表滚动的性能,我有implemented this suggestion 它肯定会提高性能。

我的就是这样实现的

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
    switch (scrollState) {
        case OnScrollListener.SCROLL_STATE_IDLE:
            adapter.busy = false;
            adapter.notifyDataSetChanged();
            break;
        case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
            adapter.busy = true;
            break;
        case OnScrollListener.SCROLL_STATE_FLING:
            adapter.busy = true;
            break;
    }
}

但是,我想通过在列表滚动速度超过某个阈值时将 adapter.busy 设置为 false 来使其在视觉上更具吸引力。但是,我想不出在滚动时确定滚动速度的好方法。任何帮助将不胜感激。

【问题讨论】:

  • 很遗憾并非如此,从那时起我们进行了很多更改,以使滚动性能不再是一个问题。

标签: android performance android-listview baseadapter


【解决方案1】:

您可以随时获取滚动位置。使用 CountdownTimer 定期检查最新的滚动位置,并与之前的滚动位置进行比较以确定方向和速度。如果它的移动太快根据更新。实施后,请按上述方式发布结果。 (可能还有滚动位置更改事件,或者您可能会利用焦点事件更改来确定这一点)。

【讨论】:

    【解决方案2】:

    有类似的要求,以下对我有用。 只需要尝试最适合MIN_VELOCITY

    @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                    int totalItemCount) {
    
                checkFlingVelocitySlowDown(firstVisibleItem, totalItemCount);
            }
    
    int mLastScrollInTopItemPosition = 0;
    long mLastTimeTopPositionChanged = System.currentTimeMillis();
    final double MIN_VELOCITY = 0.01;
    final int TOP_VIEW_ITEMS_RANGE = 3;
    final int BOTTOM_VIEW_RANGE = 6;
    
    void checkFlingVelocitySlowDown(int scrollInTopItemPosition, int totalItemCount) {
        try {
            if (mLastScrollInTopItemPosition != scrollInTopItemPosition) {
                long now = System.currentTimeMillis();
                long timeSpan = now - mLastTimeTopPositionChanged;
                double velocity = (timeSpan > 0) ? (1.0 / timeSpan) : 1000000;
                mLastScrollInTopItemPosition = scrollInTopItemPosition;
                mLastTimeTopPositionChanged = now;
    
                if (velocity <= MIN_VELOCITY ||
                        scrollInTopItemPosition <= TOP_VIEW_ITEMS_RANGE ||
                        (Math.abs(totalItemCount - scrollInTopItemPosition) < BOTTOM_VIEW_RANGE)) {
                    // to what ever it should do when scroll closer to top or bottom, or fling is slowing down
                }
            }
        } catch (Exception e) {}
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-12
      • 2012-10-14
      • 2018-01-25
      • 1970-01-01
      相关资源
      最近更新 更多