Skip to content

Instantly share code, notes, and snippets.

@VladSumtsov
Last active April 20, 2017 13:43
Show Gist options
  • Save VladSumtsov/5a0778076a173a28caff0318f1d66ca7 to your computer and use it in GitHub Desktop.
Save VladSumtsov/5a0778076a173a28caff0318f1d66ca7 to your computer and use it in GitHub Desktop.
ViewPager listener to change colors on swiping
package com.corewillsoft.loansdeposits.ui.utils;
import android.animation.ArgbEvaluator;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import static android.support.v4.view.ViewPager.SCROLL_STATE_IDLE;
import static com.corewillsoft.loansdeposits.ui.utils.SwipeDirectionDetector.Direction.LEFT;
import static com.corewillsoft.loansdeposits.ui.utils.SwipeDirectionDetector.Direction.RIGHT;
public abstract class ColorChangeEvaluatorListener implements ViewPager.OnPageChangeListener {
private final SwipeDirectionDetector directionDetector;
private final ArgbEvaluator evaluator;
private final int[] colors;
private int currentItem;
private ViewPager viewPager;
private float prevOffset;
private Handler handler = new Handler();
public ColorChangeEvaluatorListener(ViewPager viewPager, int[] colors) {
this.viewPager = viewPager;
handler.post(() -> {
currentItem = viewPager.getCurrentItem();
updateColor();
});
currentItem = viewPager.getCurrentItem();
evaluator = new ArgbEvaluator();
directionDetector = new SwipeDirectionDetector();
this.colors = colors;
updateColor();
}
@Override
public void onPageScrolled(int position, float offset, int positionOffsetPixels) {
directionDetector.onPageScrolled(position, offset, positionOffsetPixels);
if (Math.abs(prevOffset - offset) > 0.3) {
currentItem = viewPager.getCurrentItem();
}
prevOffset = offset;
if (offset > 0) {
updateColor(offset);
}
}
@Override
public void onPageSelected(int position) {
directionDetector.onPageSelected(position);
}
@Override
public void onPageScrollStateChanged(int state) {
directionDetector.onPageScrollStateChanged(state);
if (state == SCROLL_STATE_IDLE) {
currentItem = viewPager.getCurrentItem();
updateColor();
}
}
private void updateColor(float offset) {
int currentColor = colors[currentItem];
int position = currentItem;
if (directionDetector.getDirection() == RIGHT) {
position = currentItem + 1;
} else if (directionDetector.getDirection() == LEFT) {
position = currentItem - 1;
offset = 1 - offset;
}
position = Math.max(0, position);
position = Math.min(colors.length - 1, position);
int nextColor = colors[position];
colorChanged((Integer) evaluator.evaluate(offset, currentColor, nextColor));
}
private void updateColor() {
handler.post(() -> colorChanged(colors[currentItem]));
}
public abstract void colorChanged(int color);
}
package com.corewillsoft.loansdeposits.ui.utils;
import android.support.v4.view.ViewPager;
import static com.corewillsoft.loansdeposits.ui.utils.SwipeDirectionDetector.Direction.LEFT;
import static com.corewillsoft.loansdeposits.ui.utils.SwipeDirectionDetector.Direction.NO_DIRECTION;
import static com.corewillsoft.loansdeposits.ui.utils.SwipeDirectionDetector.Direction.RIGHT;
public class SwipeDirectionDetector implements ViewPager.OnPageChangeListener {
private Direction direction;
private int lastState;
public enum Direction {
LEFT,
RIGHT,
NO_DIRECTION
}
@Override
public void onPageScrolled(int position, float offset, int positionOffsetPixels) {
boolean leftStarted = direction == LEFT && offset > 0.5;
boolean rightStarted = direction == RIGHT && offset < 0.5;
if (lastState == ViewPager.SCROLL_STATE_DRAGGING && (leftStarted || rightStarted)) {
direction = NO_DIRECTION;
}
initDirection(offset);
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
lastState = state;
}
private void initDirection(float offset) {
if (direction == NO_DIRECTION && offset > 0) {
if (offset > 0.5) {
direction = LEFT;
} else {
direction = RIGHT;
}
}
if (offset == 0) {
direction = NO_DIRECTION;
}
}
public Direction getDirection() {
return direction;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment