AndroidFastScroll

repository·master·Indexed 20 days ago

https://github.com/zhanghai/androidfastscroll

A customizable fast-scrolling library for Android views including RecyclerView, ScrollView, NestedScrollView, and WebView. It supports Material Design styles, custom animations, and custom layouts. Key features include the FastScrollerBuilder for configuration, PopupTextProvider for custom scroll labels, and specialized components like FixItemDecorationRecyclerView and FixOnItemTouchListenerRecyclerView to resolve drawing and touch event conflicts.

Tokens
1.3K
Snippets
4
Records
8
Agent score
23%

What's inside AndroidFastScroll

  1. Handle variable item heights in RecyclerView

    master
    The default ViewHelper for RecyclerView assumes all items have the same height. If your items have variable heights, the scroll calculation will be inaccurate. To fix this, you must provide your own implementation of ViewHelper via FastScrollerBuilder.setViewHelper() that correctly measures or calculates positions for variable heights.
  2. Install AndroidFastScroll

    master

    Add the library dependency to your build.gradle file. Note that this library requires Java 8 bytecode support, so you must enable it in your android block for both Java and Kotlin projects.

    // Dependency
    implementation 'me.zhanghai.android.fastscroll:library:1.3.0'
    
    // Java 8 support configuration
    android {
        ...
        // For Java projects
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
        // For Kotlin projects
        kotlinOptions {
            jvmTarget = JavaVersion.VERSION_1_8.toString()
        }
    }
  3. Customize FastScroller using FastScrollerBuilder

    master

    Use FastScrollerBuilder to customize the appearance and behavior of the scrollbar. Key customization methods include:

    • setViewHelper(ViewHelper): Provide a custom ViewHelper to support views other than the standard ones or to handle variable item heights.
    • setPopupTextProvider(PopupTextProvider): Provide a custom provider for the popup text (useful if your Adapter doesn't implement PopupTextProvider).
    • setPadding(int): Set a custom padding for the scrollbar.
    • setTrackDrawable(Drawable): Set a custom track drawable (must have an intrinsic width).
    • setThumbDrawable(Drawable): Set a custom thumb drawable (must have an intrinsic size).
    • setPopupStyle(Consumer<View>): Customize the popup view using a lambda.
    • setAnimationHelper(AnimationHelper): Provide a custom animation for the scrollbar.
    • disableScrollbarAutoHide(): Disable the auto-hide animation (requires using DefaultAnimationHelper).
    • useDefaultStyle(): Use the default predefined style.
    • useMd2Style(): Use the Material Design 2 predefined style.
    new FastScrollerBuilder(recyclerView)
        .setTrackDrawable(myTrack)
        .setThumbDrawable(myThumb)
        .useMd2Style()
        .build();
  4. Fix touch event conflicts with FixOnItemTouchListenerRecyclerView

    master
    If you use other libraries that rely on RecyclerView.OnItemTouchListener (such as recyclerview-selection), use FixOnItemTouchListenerRecyclerView to ensure touch events are correctly handled and cancellations are dispatched. It is recommended to configure this library before others to ensure it takes precedence in touch event handling.
  5. Implement PopupTextProvider for custom scroll labels

    master

    To display custom text (like section headers) in the fast scroll popup, implement the PopupTextProvider interface. You can either pass this provider directly to the RecyclerViewHelper constructor or implement it within your RecyclerView.Adapter.

    When implemented, getPopupText(RecyclerView view, int position) will be called to retrieve the label for the item at the current scroll position.

    // Option 1: Implement in your Adapter
    public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> implements PopupTextProvider {
        @Override
        public CharSequence getPopupText(@NonNull RecyclerView view, int position) {
            return "Section " + position;
        }
    }
    
    // Option 2: Pass a separate provider to RecyclerViewHelper
    PopupTextProvider myProvider = new PopupTextProvider() {
        @Override
        public CharSequence getPopupText(@NonNull RecyclerView view, int position) {
            return "Label " + position;
        }
    };
    RecyclerViewHelper helper = new RecyclerViewHelper(recyclerView, myProvider);