DiscreteScrollView Documentation

repository·master·Indexed 26 days ago

https://github.com/yarolegovich/discretescrollview

A RecyclerView-based Android library for creating scrollable lists where the current item is centered, ideal for carousels and pickers. It features custom layout management, page transformations via DiscreteScrollItemTransformer, infinite scrolling through InfiniteScrollAdapter, and configurable fling and slide behaviors.

Tokens
1.1K
Snippets
3
Records
9
Agent score
41%

What's inside DiscreteScrollView

  1. Implement DiscreteScrollView in your layout

    master

    DiscreteScrollView is a descendant of RecyclerView. You can add it to your layout using XML or programmatically.

    Important: Do NOT set a LayoutManager manually; the library uses a custom internal LayoutManager to handle positioning and scrolling.

    <com.yarolegovich.discretescrollview.DiscreteScrollView
      android:id="@+id/picker"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      app:dsv_orientation="horizontal|vertical" />  <!-- orientation is optional, default is horizontal -->
    DiscreteScrollView scrollView = findViewById(R.id.picker);
    scrollView.setAdapter(new YourAdapterImplementation());
  2. Apply Page Transformations

    master

    You can create effects like carousels using DiscreteScrollItemTransformer. The transformItem method provides a position value in the interval [-1f..1f], where 0 is the currently selected view, -1 is the view to the left, and 1 is the view to the right.

    To adjust how many items are affected by the transformation, use setClampTransformProgressAfter(n).

    Example using the built-in ScaleTransformer:

    cityPicker.setItemTransformer(new ScaleTransformer.Builder()
      .setMaxScale(1.05f) 
      .setMinScale(0.8f) 
      .setPivotX(Pivot.X.CENTER) // CENTER is a default one
      .setPivotY(Pivot.Y.BOTTOM) // CENTER is a default one
      .build());
  3. Control current item selection and scrolling

    master

    Methods for interacting with the currently selected item:

    • getCurrentItem(): Returns the adapter position of the currently selected item, or -1 if the adapter is empty.
    • scrollToPosition(int position): Scrolls to the specified position, making it the selected item.
    • smoothScrollToPosition(int position): Performs an animated scroll to the specified position, making it the selected item.
    • setItemTransitionTimeMillis(int millis): Determines the duration of transitions during fling, settle, or smooth scroll.
  4. Register Scroll and Item Change Listeners

    master

    The library provides three types of listeners to track interaction:

    1. ScrollStateChangeListener: Tracks when scrolling starts, ends, or is in progress (onScrollStart, onScrollEnd, onScroll).
    2. ScrollListener: A simplified listener that only provides the onScroll callback.
    3. OnItemChangedListener: Called when a new item is selected (onCurrentItemChanged). This is similar to onScrollEnd but also triggers when the currently selected item first appears on screen.

    All listeners use generics to provide the ViewHolder type T.

  5. Implement Infinite Scroll

    master

    Infinite scroll is implemented by wrapping your existing adapter with InfiniteScrollAdapter.

    InfiniteScrollAdapter wrapper = InfiniteScrollAdapter.wrap(yourAdapter);
    scrollView.setAdapter(wrapper);

    InfiniteScrollAdapter provides these methods:

    • getRealItemCount()
    • getRealCurrentPosition()
    • getRealPosition(int position)
    • getClosestPosition(int position): Useful for finding the target adapter position to call smoothScrollTo for the minimum required movement.
  6. Configure DiscreteScrollView basic settings

    master

    Use these methods to control the basic behavior of the scroll view:

    • setOrientation(DSVOrientation o): Sets the orientation (horizontal or vertical).
    • setOffscreenItems(count): Reserves extra space equal to (childSize * count) on each side of the view.
    • setOverScrollEnabled(enabled): Enables or disables overscroll (can also be set via android:overScrollMode in XML).