Skip to content

Conversation

@7se7en72025
Copy link
Contributor

Purpose / Description

Fix scrollbar thumb size changing during scroll when RecyclerView items have different heights.

The scrollbar thumb was resizing as items with varying heights scrolled in/out of view. This occurred because computeVerticalScrollRange() returns different values during scroll when items have different heights, causing the thumb height calculation to fluctuate.

Before: Scrollbar thumb size would change while scrolling through items with different heights (e.g., tags with varying lengths, cards with different amounts of text).

After: Scrollbar thumb maintains a consistent size throughout scrolling, providing a stable and professional user experience.

Fixes

Approach

  • Added cachedMaxScrollRange variable to cache the maximum scroll range encountered
  • Update cache when a larger scroll range is detected (as user scrolls to see more content)
  • Use cached maximum for thumb height calculation to keep size stable
  • Use current range for position calculation to maintain accurate thumb position
  • Reset cache when adapter data changes to handle dynamic content updates

This ensures the thumb size is based on the true maximum content height we've seen, not the fluctuating current value that changes as items with different heights scroll in/out of view.

How Has This Been Tested?

  • Built and installed fullDebug variant using .\gradlew.bat installFullDebug
  • Tested in Card Browser with cards having tags of varying lengths (short vs long tag names)
  • Tested in Tags Dialog with mix of short and long tag names
  • Verified scrollbar thumb maintains constant size during scroll
  • Confirmed thumb position remains accurate and responsive
  • Tested with items that take up different numbers of lines (as described in the bug report)

Test Configuration:

  • Android SDK 33
  • AnkiDroid Version: 2.23.0alpha8
  • Physical device / Emulator
  • Device: Xiaomi POCO (as mentioned in bug report)

Reproduction Steps:

  1. Open Card Browser or Tags Dialog
  2. Scroll through items with varying heights (tags/cards with different text lengths)
  3. Observe the scrollbar thumb on the right side
  4. Expected: Thumb size remains constant while scrolling
  5. Before fix: Thumb size would change as items scrolled in/out of view

Learning (optional, can help others)

The issue occurs because RecyclerView's computeVerticalScrollRange() can return varying values as items with different heights scroll in and out of view. This is a known limitation when dealing with dynamic item heights in RecyclerView.

Research:

  • RecyclerView's computeVerticalScrollRange() calculates based on currently measured items
  • When items have varying heights, the measured range can fluctuate during scroll
  • Solution: Cache the maximum range seen and use it for thumb size calculation
  • This approach maintains stability while preserving accurate position tracking

Related:

  • Similar issues can occur in any RecyclerView with variable item heights
  • The fix ensures consistent scrollbar behavior across all RecyclerViews using RecyclerFastScroller

Checklist

Please, go through these checks before submitting the PR.

  • You have a descriptive commit message with a short title (first line, max 50 chars).
  • You have commented your code, particularly in hard-to-understand areas
  • You have performed a self-review of your own code
  • UI changes: include screenshots of all affected screens (in particular showing any new or changed strings)
  • UI Changes: You have tested your change using the Google Accessibility Scanner

Cache maximum scroll range to prevent thumb resizing when items
with different heights scroll in/out of view. This ensures the
thumb maintains a consistent size throughout scrolling.
@welcome
Copy link

welcome bot commented Nov 14, 2025

First PR! 🚀 We sincerely appreciate that you have taken the time to propose a change to AnkiDroid! Please have patience with us as we are all volunteers - we will get to this as soon as possible.

Copy link
Member

@mikehardy mikehardy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

neat idea! one quick question

Comment on lines 405 to 408
// Initialize cache on first layout if not set yet
if (cachedMaxScrollRange == 0 && currentScrollRange > 0) {
cachedMaxScrollRange = currentScrollRange
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this necessary, or is this always just a special case if the conditional above that sets cached if current is > ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Removed the redundant initialization :) the condition above already handles it.

Cache maximum scroll range to prevent thumb resizing when items
with different heights scroll in/out of view. This ensures the
thumb maintains a consistent size throughout scrolling.
Copy link
Member

@mikehardy mikehardy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait :-), now you removed both cache storage conditionals, so you won't cache the current range if it's greater than the cached range...

Add logic to update cached maximum scroll range to maintain thumb size consistency.
@7se7en72025
Copy link
Contributor Author

@mikehardy pls check now, Have made the correct changes !!

@7se7en72025
Copy link
Contributor Author

7se7en72025 commented Nov 15, 2025

@BrayanDSO @mikehardy pls check now

@BrayanDSO
Copy link
Member

Don't ping people that fast. Be patient. Everyone here has other things to do.

Copy link
Member

@mikehardy mikehardy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 seems good now, and does seem like this trick should work
Thanks!

@mikehardy mikehardy added the Needs Second Approval Has one approval, one more approval to merge label Nov 15, 2025
Copy link
Member

@david-allison david-allison left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Open Card Browser or Tags Dialog

This doesn't appear to change anything on the Tags Dialog

Just tested and the scrollbar now jitters

AnkiDroid Version = 2.23.0alpha8-debug (dcf793c)

Screen_recording_20251115_214207.webm

@mikehardy mikehardy added the Needs Author Reply Waiting for a reply from the original author label Nov 15, 2025
Fix the scrollbar thumb height calculation to prevent jittering during scroll.

Changes:
1. Cache the maximum scroll range to prevent thumb size changes when items
   with different heights scroll in/out of view
2. Fix handle height calculation to use computeVerticalScrollExtent() instead
   of barHeight in the numerator. The correct formula is:
   (visibleExtent / totalRange) * trackHeight

Previously used (barHeight / range) * barHeight which was mathematically
incorrect and didn't properly represent the proportion of visible content.

Fixes ankidroid#19224
Fix the scrollbar thumb height calculation to prevent jittering during scroll.

Changes:
1. Cache the maximum scroll range to prevent thumb size changes when items
   with different heights scroll in/out of view
2. Fix handle height calculation to use computeVerticalScrollExtent() instead
   of barHeight in the numerator. The correct formula is:
   (visibleExtent / totalRange) * trackHeight

Previously used (barHeight / range) * barHeight which was mathematically
incorrect and didn't properly represent the proportion of visible content.

Fixes ankidroid#19224
Cache computeVerticalScrollExtent() and only update when RecyclerView
is resized. The visible extent can vary during scroll, causing the
thumb height to change and create jitter.

Changes:
- Add cachedVisibleExtent and lastRecyclerViewHeight variables
- Only recalculate visible extent when RecyclerView height changes
- Use cached visible extent for handle height calculation
- Reset cache when adapter changes

This prevents the thumb size from changing during scroll, eliminating
the jitter issue.
Cache the calculated handle height and only update when there's a
significant change (> 2px difference). This prevents small calculation
fluctuations from causing jitter during scroll.

Changes:
- Add cachedHandleHeight and cachedBarHeight variables
- Only update handle height if barHeight changed or difference > 2px
- Use cached handle height to prevent jitter
- Reset cache when adapter changes

This provides additional stability on top of the visible extent caching.
@7se7en72025 7se7en72025 force-pushed the fix/scrollbar-thumb-size-stable branch 2 times, most recently from 11ab287 to 53c89c0 Compare November 16, 2025 02:27
@7se7en72025
Copy link
Contributor Author

7se7en72025 commented Nov 16, 2025

I wasn’t aware of your recent commits. If they fixed the issue, can I remove my commits i made after yours ? Please guide me. Thanks!

- Set fixed row height (56dp) in card browser to prevent scrollbar jitter
- Add ellipsize/line limits and improved styling to browser columns
- Add ellipsize to note type field & tag list layouts
- Add long-press toast + tooltip to view full text for truncated items

Adds UX hints for truncated text and preserves smooth scrolling across lists.
@7se7en72025 7se7en72025 force-pushed the fix/scrollbar-thumb-size-stable branch from 9a70fc9 to 8e4164d Compare November 16, 2025 08:18
@david-allison
Copy link
Member

This doesn't compile

I haven't made relevant commits to this issue on main

@7se7en72025
Copy link
Contributor Author

WhatsApp.Video.2025-11-17.at.02.28.04_bd1a91ab.mp4

can something like this work ?

@david-allison
Copy link
Member

That's already done by 'Options => Truncate content'

@7se7en72025
Copy link
Contributor Author

then i will look for a way to fix this without truncating then,

@david-allison david-allison marked this pull request as draft November 18, 2025 15:18
@david-allison
Copy link
Member

Converted to draft to remove from my queue, please mark it as ready when reviewable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Needs Author Reply Waiting for a reply from the original author Needs Second Approval Has one approval, one more approval to merge New contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Scrollbar thumb changes length when scolling

4 participants