question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Smarter position provided by getPopupText(position)

See original GitHub issue

Actual Behavior

I implemented in my app a fullscreen RecyclerView with getPopupText support. It looks like this: The problem is, the position provided by getPopupText is the first visible item in the Adapter. Though as you can see here, this item (framed in red) is actually barely visible. So while the user only sees items starting with “L”, the popup still shows “K”, because the very last “K” item still hasn’t been recycled.

I was thinking maybe there is a smarter way than getFirstItemAdapterPosition() to get this position. As I’m a bit curious, I investigated how other fast scrollers handle this issue. For instance, Nova Launcher’s widget list uses a fast scroll which applies some kind of padding to this position, proportional to how low you are in the list (the more you scroll, the lower the item targeted by getPopupText is on the screen). Which enables their popup to use the very first item’s position when you’re at the top, the very last item’s position when you’re at the bottom, and the position of an item which is always visible when you’re anywhere in between.

It’s kinda hard to explain with words. Actually experimenting with it helps to understand. I am also aware that the Google Contacts app works in a similar way (using the first visible item), though it’s not a problem because their list is not full screen, and the list snaps when fast scrolling, to make sure the first item is always fully visible. Not all lists can mimic this behavior though.

Expected Behavior

The position provided by getPopupText should target an item which is always (clearly) visible to the user.

Steps to Reproduce the Problem

  1. Create a RecyclerView and simply bind a FastScroller with FastScrollerBuilder(recyclerView).build() (the issue is more visible with a full screen RV).
  2. Implement PopupTextProvider in the Adapter.
  3. Scroll.

Specifications

  • Version: v1.1.0
  • Platform: Android 10, OnePlus 6 OxygenOS 10.3.1, no root

Don’t hesitate to ask for more details if it wasn’t clear. 😃 Thanks!

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
h6ah4icommented, Sep 19, 2021

Hi. I am using the following code to improve the accuracy of obtaining the popup text.

public class BetterRecyclerViewHelper  implements FastScroller.ViewHelper {
    ...

    @Nullable
    @Override
    public String getPopupText() {
        PopupTextProvider popupTextProvider = mPopupTextProvider;
        if (popupTextProvider == null) {
            RecyclerView.Adapter<?> adapter = mView.getAdapter();
            if (adapter instanceof PopupTextProvider) {
                popupTextProvider = (PopupTextProvider) adapter;
            }
        }
        if (popupTextProvider == null) {
            return null;
        }
        int position = getPopupTextPosition();
        if (position == RecyclerView.NO_POSITION) {
            return null;
        }
        return popupTextProvider.getPopupText(position);
    }

    private int getPopupTextPosition() {
        int position = getFirstItemAdapterPosition();
        if (position == RecyclerView.NO_POSITION) return RecyclerView.NO_POSITION;

        LinearLayoutManager linearLayoutManager = getVerticalLinearLayoutManager();
        if (linearLayoutManager == null) return position;

        int viewportHeight = mView.getHeight();
        int range = Math.max(getScrollRange() - viewportHeight, 1);
        int offset = Math.min(getScrollOffset(), range);

        int firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
        int lastVisibleItemPosition = linearLayoutManager.findLastVisibleItemPosition();

        if (firstVisibleItemPosition == RecyclerView.NO_POSITION || lastVisibleItemPosition == RecyclerView.NO_POSITION) return position;

        int positionOffset = (int)((lastVisibleItemPosition - firstVisibleItemPosition + 1) * 1.0 * offset / range);
        int correctedPosition = Math.min((position + positionOffset), Objects.requireNonNull(mView.getAdapter()).getItemCount() - 1);

        return correctedPosition;
    }

    ...
}
1reaction
RobbWatershedcommented, May 5, 2020

@zhanghai Wouldn’t it be better to replace the implementation of RecyclerViewHelper.getFirstItemAdapterPosition by something that uses LinearLayoutManager’s FindXXXVisibleItemPosition instead of fetching the first visible view of the layout and then computing its position ?

This way it would be possible to give the user/developer control over which method to use between :

  • findFirstVisibleItemPosition (= more or less what the current implementation does)
  • findFirstCompletelyVisibleItemPosition (= what the OP seems to want)
  • findLastVisibleItemPosition
  • findLastCompletelyVisibleItemPosition
Read more comments on GitHub >

github_iconTop Results From Across the Web

3 Simple Job Tweaks That Will Make Your Employees ...
3 Simple Job Tweaks That Will Make Your Employees Instantly Smarter · 1. Challenging tasks · 2. Autonomy · 3. The right kind...
Read more >
How Your Job Can Make You Smarter - WSJ
How Your Job Can Make You Smarter. Different benefits for managers, paramedics, programmers, teachers.
Read more >
Smarter Security: Entrance and Access Control Solutions
Smarter Security markets the world's most intelligent Entrance and Access Control solutions. Fastlane turnstiles, Door Detectives and SmarterAccess.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found