Install DiscreteScrollView via Gradle
masterAdd the following dependency to your Gradle dependencies block to use DiscreteScrollView in your Android project.
compile 'com.yarolegovich:discrete-scrollview:1.5.1'repository·master·Indexed 26 days ago
https://github.com/yarolegovich/discretescrollviewA 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.
Add the following dependency to your Gradle dependencies block to use DiscreteScrollView in your Android project.
compile 'com.yarolegovich:discrete-scrollview:1.5.1'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());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());setScrollConfig(config), where config is an instance of the DSVScrollConfig enum.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.The library provides three types of listeners to track interaction:
onScrollStart, onScrollEnd, onScroll).onScroll callback.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.
To allow the user to slide through multiple items in a single fling, use setSlideOnFling(true).
You can adjust the sensitivity of the fling by changing the threshold with setSlideOnFlingThreshold(value). The default threshold is 2100.
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.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).