AbsListView: Improve scrolling cache

Scrolling cache helps make short scrolls/flings smooth but will
cause stutter when long flings are made. This patch disables
scrolling cache when long flings are made.

This patch also fixes a related bug where scrolling cache will
not be enabled properly when transitioning from flinging to scrolling.

Patch Set 2: Calculate threshold based on maximum velocity (Sang Tae Park)

Change-Id: Iad52a35120212c871ffd35df6184aeb678ee44aa
Signed-off-by: Alex Naidis <alex.naidis@linux.com>
Signed-off-by: MOVZX <movzx@yahoo.com>
This commit is contained in:
2026-01-02 23:08:41 +07:00
parent 7a118feec0
commit 7ae2fee6e1

View File

@@ -697,6 +697,7 @@ public abstract class AbsListView extends AdapterView<ListAdapter> implements Te
private int mMinimumVelocity;
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 124051740)
private int mMaximumVelocity;
private int mDecacheThreshold;
private float mVelocityScale = 1.0f;
final boolean[] mIsScrap = new boolean[1];
@@ -1011,6 +1012,7 @@ public abstract class AbsListView extends AdapterView<ListAdapter> implements Te
mVerticalScrollFactor = configuration.getScaledVerticalScrollFactor();
mMinimumVelocity = configuration.getScaledMinimumFlingVelocity();
mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
mDecacheThreshold = mMaximumVelocity / 2;
mOverscrollDistance = configuration.getScaledOverscrollDistance();
mOverflingDistance = configuration.getScaledOverflingDistance();
@@ -4965,7 +4967,7 @@ public abstract class AbsListView extends AdapterView<ListAdapter> implements Te
// Keep the fling alive a little longer
postDelayed(this, FLYWHEEL_TIMEOUT);
} else {
endFling();
endFling(false); // Don't disable the scrolling cache right after it was enabled
mTouchMode = TOUCH_MODE_SCROLL;
reportScrollStateChange(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL);
}
@@ -4985,6 +4987,10 @@ public abstract class AbsListView extends AdapterView<ListAdapter> implements Te
// Use AbsListView#fling(int) instead
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
void start(int initialVelocity) {
if (Math.abs(initialVelocity) > mDecacheThreshold) {
// For long flings, scrolling cache causes stutter, so don't use it
clearScrollingCache();
}
int initialY = initialVelocity < 0 ? Integer.MAX_VALUE : 0;
mLastFlingY = initialY;
mScroller.setInterpolator(null);
@@ -5065,6 +5071,10 @@ public abstract class AbsListView extends AdapterView<ListAdapter> implements Te
// To interrupt a fling early you should use smoothScrollBy(0,0) instead
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
void endFling() {
endFling(true);
}
void endFling(boolean clearCache) {
mTouchMode = TOUCH_MODE_REST;
removeCallbacks(this);
@@ -5073,7 +5083,8 @@ public abstract class AbsListView extends AdapterView<ListAdapter> implements Te
if (!mSuppressIdleStateChangeCall) {
reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
}
clearScrollingCache();
if (clearCache)
clearScrollingCache();
mScroller.abortAnimation();
if (mFlingStrictSpan != null) {