14

I want to use a bounce effect on a RecyclerView. A bounce effect whenever I overscroll the content...

Does there exist a library or example for it?

prom85
  • 16,896
  • 17
  • 122
  • 242

4 Answers4

15

I also couldn't find any library that supports the bounce effect for RecyclerView. Eventually I ended up implementing a new library by myself. Check out my library overscroll-bouncy-android. . It currently supports RecyclerView with LinearLayoutManager. I'm also working on ListView and ScrollView.

To use my library:

Step 1:

dependencies {
    compile 'com.chauthai.overscroll:overscroll-bouncy:0.1.0'
}

Step 2:

<com.chauthai.overscroll.RecyclerViewBouncy
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
Chau Thai
  • 704
  • 6
  • 10
  • 2
    I have RecyclerViewBouncy within SwipeRefreshLayout, and it looks like RecyclerViewBouncy swallows pull down event and refresh is not triggered – Delorean Feb 27 '18 at 19:29
  • Unfortunately it does not work properly with CollapsingToolbarLayout – Seven Jul 10 '18 at 23:05
6

This can be easily done with dynamic animations without using any third-party library.

I wrote an article about this here.

Another advantage is that it'll work with any type of layout manager and since we are using dynamic animations which are physics-based so animation feels more natural.

Ashu Tyagi
  • 1,400
  • 13
  • 25
4

You can use this library https://github.com/EverythingMe/overscroll-decor So, you need to create your own ScrollDecorAdapter like this

public class CustomRecyclerViewOverScrollDecorAdapter extends RecyclerViewOverScrollDecorAdapter {
RecyclerView mRecyclerView;

public CustomRecyclerViewOverScrollDecorAdapter(RecyclerView recyclerView) {
    super(recyclerView);
    mRecyclerView = recyclerView;
}

@Override
public boolean isInAbsoluteEnd() {
    LinearLayoutManager linearLayoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager();
    if (linearLayoutManager.getOrientation() == LinearLayoutManager.HORIZONTAL) {
        return !mRecyclerView.canScrollHorizontally(1);
    } else {
        return !mRecyclerView.canScrollVertically(1);
    }
  }
}

And now in your fragment/activity use

 CustomVerticalOverScrollDecorator overScrollDecorator =
            new CustomVerticalOverScrollDecorator(new CustomRecyclerViewOverScrollDecorAdapter(yourRecyclerView));

Where CustomVerticalOverScrollDecorator smth like this

public class CustomVerticalOverScrollDecorator extends VerticalOverScrollBounceEffectDecorator {

public CustomVerticalOverScrollDecorator(IOverScrollDecoratorAdapter viewAdapter) {
    this(viewAdapter, DEFAULT_TOUCH_DRAG_MOVE_RATIO_FWD, DEFAULT_TOUCH_DRAG_MOVE_RATIO_BCK, DEFAULT_DECELERATE_FACTOR);
}

public CustomVerticalOverScrollDecorator(IOverScrollDecoratorAdapter viewAdapter, float touchDragRatioFwd, float touchDragRatioBck, float decelerateFactor) {
    super(viewAdapter, touchDragRatioFwd, touchDragRatioBck, decelerateFactor);

    // Some setup on the view itself.
   }
  }
Mikhail Valuyskiy
  • 1,238
  • 2
  • 16
  • 31
  • 1
    Tried both libraries(this and Chau Thai's). 1. Chau's library is great but it only supports LinearLayoutManager and sometimes is a bit buggy when updating/adding new items. 2. overscroll-decor is great. The only drawback is that it does not support auto-over-scroll effect (when you fling and it reaches top/bottom). Chau's library supports it. (https://github.com/EverythingMe/overscroll-decor/issues/1) – vgulkevic Feb 10 '19 at 20:15
2

You can try my library https://github.com/Valkriaine/Bouncy.

It supports both recyclerview and nestedscrollview.

Add it in your root build.gradle at the end of repositories:

allprojects {
      repositories {
          ...
          maven { url 'https://jitpack.io' }
      }
  }

In your app module build.gradle:

     dependencies {
        implementation 'androidx.recyclerview:recyclerview:1.2.1'
        implementation 'com.github.valkriaine:Bouncy:2.2'
   }

And use it as a normal recyclerview.

<com.factor.bouncy.BouncyRecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:recyclerview_fling_animation_size=".7"
        app:recyclerview_overscroll_animation_size=".7"
        app:recyclerview_damping_ratio="DAMPING_RATIO_LOW_BOUNCY"
        app:recyclerview_stiffness="STIFFNESS_MEDIUM"
        app:allow_drag_reorder="true"
        app:allow_item_swipe="false"/>

Setup adapter and layoutmanager. Technically supports any layoutmanager:

   recycler_view.setAdapter(myAdapter);
   recycler_view.setLayoutManager(new LinearLayoutManager(context));
   //recycler_view.setLayoutManager(new GridLayoutManager(context, 3));

Horizontal overscroll too:

recycler_view.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));

If the bounce animation is in the wrong direction (eg bouncing horizontally while the layout is vertical) you can also manually set the animation orientation:

 recycler_view.setOrientation(LinearLayoutManager.VERTICAL);