Last active
August 29, 2015 14:25
-
-
Save EminYahyayev/6f50d0d09b5510ea9f6c to your computer and use it in GitHub Desktop.
Simple {@link RecyclerView.OnScrollListener} implementation which pauses/resumes Picasso's tagged requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ewintory.udacity.popularmovies.utils; | |
import android.content.Context; | |
import android.support.v7.widget.RecyclerView; | |
import com.squareup.picasso.Picasso; | |
import com.squareup.picasso.RequestCreator; | |
/** | |
* Simple {@link RecyclerView.OnScrollListener} implementation which | |
* pauses/resumes Picasso's tagged requests | |
* | |
* @author Emin Yahyayev | |
* @see {@link RequestCreator#tag(Object)} | |
*/ | |
public final class PicassoScrollListener extends RecyclerView.OnScrollListener { | |
private static final int DEFAULT_SENSITIVITY = 120; | |
private Picasso mPicasso; | |
private int mPauseSensitivity; // scroll sensitivity | |
private Object mTag; | |
public PicassoScrollListener(Context context, Object tag) { | |
this(Picasso.with(context), tag, DEFAULT_SENSITIVITY); | |
} | |
public PicassoScrollListener(Picasso picasso, Object tag) { | |
this(picasso, tag, DEFAULT_SENSITIVITY); | |
} | |
public PicassoScrollListener(Context context, Object tag, int pauseSensitivity) { | |
this(Picasso.with(context), tag, pauseSensitivity); | |
} | |
public PicassoScrollListener(Picasso picasso, Object tag, int pauseSensitivity) { | |
mPicasso = picasso; | |
mPauseSensitivity = pauseSensitivity; | |
mTag = tag; | |
} | |
@Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { | |
if (Math.abs(dy) > mPauseSensitivity) | |
mPicasso.pauseTag(mTag); | |
else | |
mPicasso.resumeTag(mTag); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment