简介:Scrollable list of items, where current item is centered and can be changed using swipes
更多:作者   提 Bug   

The library is a RecyclerView-based implementation of a scrollable list, where current item is centered and can be changed using swipes. It is similar to a ViewPager, but you can quickly and painlessly create layout, where views adjacent to the currently selected view are partially or fully visible on the screen.



Add this into your dependencies block.

compile 'com.yarolegovich:discrete-scrollview:1.2.0'


Get it on Google Play

Please see the sample app for examples of library usage.




The library uses a custom LayoutManager to adjust items' positions on the screen and handle scroll, however it is not exposed to the client code. All public API is accessible through DiscreteScrollView class, which is a simple descendant of RecyclerView.

If you have ever used RecyclerView - you already know how to use this library. One thing to note - you should NOT set LayoutManager.


  1. Add DiscreteScrollView to your layout either using xml or code:
  2. Create your implementation of RecyclerView.Adapter. Refer to the sample for an example, if you don't know how to do it.
  3. Set the adapter.
  4. You are done!
    app:dsv_orientation="horizontal|vertical" />  <!-- orientation is optional, default is horizontal -->
    DiscreteScrollView scrollView = findViewById(R.id.picker);
    scrollView.setAdapter(new YourAdapterImplementation());



scrollView.setOrientation(Orientation o); //Sets an orientation of the view
scrollView.setOffscreenItems(count); //Reserve extra space equal to (childSize * count) on each side of the view
scrollView.getCurrentItem(); //returns adapter position of the currently selected item or -1 if adapter is empty.
scrollView.scrollToPosition(int position); //position becomes selected
scrollView.smoothScrollToPosition(int position); //position becomes selected with animated scroll
scrollView.setItemTransitionTimeMillis(int millis); //determines how much time it takes to change the item on fling, settle or smoothScroll


One useful feature of ViewPager is page transformations. It allows you, for example, to create carousel effect. DiscreteScrollView also supports page transformations.


public interface DiscreteScrollItemTransformer {
     * In this method you apply any transform you can imagine (perfomance is not guaranteed).
     * @param position is a value inside the interval [-1f..1f]. In idle state:
     * |view1|  |currentlySelectedView|  |view2|
     * -view1 and everything to the left is on position -1;
     * -currentlySelectedView is on position 0;
     * -view2 and everything to the right is on position 1. */
    void transformItem(View item, float position); 

Because scale transformation is the most common, I included a helper class - ScaleTransformer, here is how to use it:

cityPicker.setItemTransformer(new ScaleTransformer.Builder()
  .setPivotX(Pivot.X.CENTER) // CENTER is a default one
  .setPivotY(Pivot.Y.BOTTOM) // CENTER is a default one

You may see how it works on GIFs.

Infinite scroll

Infinite scroll is implemented on the adapter level:

InfiniteScrollAdapter wrapper = InfiniteScrollAdapter.wrap(yourAdapter);

An instance of InfiniteScrollAdapter has the following useful methods:

int getRealItemCount();

int getRealCurrentPosition();

int getRealPosition(int position);

 * You will probably want this method in the following use case:
 * int targetAdapterPosition = wrapper.getClosestPosition(targetPosition);
 * scrollView.smoothScrollTo(targetAdapterPosition);
 * To scroll the data set for the least required amount to reach targetPosition.
int getClosestPosition(int position);

Currently InfiniteScrollAdapter handles data set changes inefficiently, so your contributions are welcome.


  • Scroll state changes: ```java scrollView.addScrollStateChangeListener(listener); scrollView.removeScrollStateChangeListener(listener);

public interface ScrollStateChangeListener {

void onScrollStart(T currentItemHolder, int adapterPosition); //called when scroll is started, including programatically initiated scroll

void onScrollEnd(T currentItemHolder, int adapterPosition); //called when scroll ends /**

  • Called when scroll is in progress.
  • @param scrollPosition is a value inside the interval [-1f..1f], it corresponds to the position of currentlySelectedView.
  • In idle state:
  • |view1| |currentlySelectedView| |view2|
  • -view1 is on position -1;
  • -currentlySelectedView is on position 0;
  • -view2 is on position 1.
  • @param currentHolder - ViewHolder of a current view
  • @param newCurrent - ViewHolder of a view that moved closer to the center */ void onScroll(float scrollPosition, @NonNull T currentHolder, @NonNull T newCurrentHolder); } ```
    • Scroll: ```java scrollView.addScrollListener(listener); scrollView.removeScrollListener(listener);

public interface ScrollListener { //The same as ScrollStateChangeListener, but for the cases when you are interested only in onScroll() void onScroll(float scrollPosition, @NonNull T currentHolder, @NonNull T newCurrentHolder); }

* Current selection changes:

public interface OnItemChangedListener<T extends ViewHolder> {
   * Called when new item is selected. It is similar to the onScrollEnd of ScrollStateChangeListener, except that it is 
   * also called when currently selected item appears on the screen for the first time.
   * viewHolder will be null, if data set becomes empty 
  void onCurrentItemChanged(@Nullable T viewHolder, int adapterPosition); 

Special thanks

Thanks to Tayisiya Yurkiv for sample app design and beautiful GIFs.


Copyright 2017 Yaroslav Shevchuk

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at


Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
See the License for the specific language governing permissions and
limitations under the License.
Android 开发经验分享
Android 开发经验分享