Skip to content

Instantly share code, notes, and snippets.

@fdoyle
Last active March 9, 2016 23:37
Show Gist options
  • Save fdoyle/3a05ea521b9fa15b48d2 to your computer and use it in GitHub Desktop.
Save fdoyle/3a05ea521b9fa15b48d2 to your computer and use it in GitHub Desktop.
Some Recyclerview Stuff
package com.lacronicus;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.animation.Interpolator;
import java.util.Random;
/**
* Created by fdoyle on 3/8/16.
*/
public class InterpolatorSmoothScroller extends RecyclerView.SmoothScroller {
Interpolator interpolator;
boolean shouldStart = true;
RecyclerView recyclerView;
public InterpolatorSmoothScroller(Interpolator interpolator, RecyclerView recyclerView) {
this.interpolator = interpolator;
this.recyclerView = recyclerView;
}
@Override
protected void onStart() {
shouldStart = true;
}
@Override
protected void onStop() {
}
@Override
protected void onSeekTargetStep(int dx, int dy, RecyclerView.State state, Action action) {
scroll(state, action, 3000);
}
@Override
protected void onTargetFound(View targetView, RecyclerView.State state, Action action) {
scroll(state, action, 1000);
}
private void scroll(RecyclerView.State state, Action action, int duration) {
if(state.didStructureChange() || shouldStart) {
int targetIndex = state.getTargetScrollPosition();
View currentView = recyclerView.getLayoutManager().getChildAt(0);
int currentIndex = recyclerView.getLayoutManager().getPosition(recyclerView.getChildAt(0));
int yDelta = currentView.getHeight() * (targetIndex - currentIndex) + (int) currentView.getY();
int xDelta = currentView.getWidth() * (targetIndex - currentIndex) + (int) currentView.getX();
action.setDy(yDelta);
action.setDx(xDelta);
if(Math.abs(xDelta) < 10 && Math.abs(yDelta) < 10) {
action.setDuration(1);
} else {
action.setDuration(duration);
action.setInterpolator(interpolator);
}
shouldStart = false;
stop();
}
}
public int getAbsYDelta(int target) {
View currentView = recyclerView.getLayoutManager().getChildAt(0);
int currentIndex = recyclerView.getLayoutManager().getPosition(recyclerView.getChildAt(0));
return Math.abs(currentView.getHeight() * (target - currentIndex) + (int) currentView.getY());
}
public int getAbsXDelta(int target) {
View currentView = recyclerView.getLayoutManager().getChildAt(0);
int currentIndex = recyclerView.getLayoutManager().getPosition(recyclerView.getChildAt(0));
return Math.abs(currentView.getWidth() * (target - currentIndex) + (int) currentView.getX());
}
public boolean wouldMoveSignificantly() {
return getAbsXDelta(getTargetPosition()) > 3 || getAbsYDelta(getTargetPosition()) > 3;
}
}
package com.lacronicus;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
/**
* Created by fdoyle on 3/8/16.
*/
public class SnapScrollListener extends RecyclerView.OnScrollListener {
RecyclerView listView;
public SnapScrollListener(RecyclerView listView) {
this.listView = listView;
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
if (recyclerView.getChildCount() > 2)
return;
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
Interpolator interpolator = new DecelerateInterpolator();
InterpolatorSmoothScroller scroller = new InterpolatorSmoothScroller(interpolator, listView);
if (listView.getLayoutManager() instanceof LinearLayoutManager) {
LinearLayoutManager layoutManager = (LinearLayoutManager) listView.getLayoutManager();
if (layoutManager.getOrientation() == LinearLayoutManager.VERTICAL) {
boolean pastHalfWayVertical = Math.abs(listView.getChildAt(0).getY()) < listView.getChildAt(0).getHeight() / 2;
scroller.setTargetPosition(listView.getLayoutManager().getPosition(listView.getChildAt(pastHalfWayVertical ? 0 : 1)));
} else {
boolean pastHalfWayHorizontal = Math.abs(listView.getChildAt(0).getX()) < listView.getChildAt(0).getWidth() / 2;
scroller.setTargetPosition(listView.getLayoutManager().getPosition(listView.getChildAt(pastHalfWayHorizontal ? 0 : 1)));
}
if (scroller.wouldMoveSignificantly()) { // don't animate if you're already on your target (this counts as "settling" to the recyclerview, so if you animate when you don't need to, you'll start another animation when it goes back to idle, looping forever. Android has OCD)
layoutManager.startSmoothScroll(scroller);
}
} else {
throw new IllegalStateException("This only works with LinearLayoutManagers");
}
}
}
}
@fdoyle
Copy link
Author

fdoyle commented Mar 8, 2016

The first class is a SmoothScroller that takes a LinearLayoutManager based Recyclerview where all items are equal height and allows you to smooth scroll using any interpolator. if your interpolator tries to scroll past bounds, the whole thing breaks, so don't use any of those.

The second class uses the first to provide a "snapping" effect, where the recyclerview aligns the top edge of the first item to the top of the recycler. Also works horizontally. If your items are full height or full width, it'll work kinda like a viewpager.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment