Created
June 7, 2016 08:44
-
-
Save VladSumtsov/6fc758c7b21792d121154bd33c1078b5 to your computer and use it in GitHub Desktop.
ViewPager listener to change colors on swiping
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.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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment