Last active
October 16, 2020 08:57
-
-
Save VladSumtsov/32ad7f36356eaeb59863 to your computer and use it in GitHub Desktop.
Change viewpager background color on swipe. ViewPager background color evaluator.
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
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; | |
public ColorChangeEvaluatorListener(ViewPager viewPager, int[] colors) { | |
this.viewPager = viewPager; | |
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() { | |
new Handler().post(() -> colorChanged(colors[currentItem])); | |
} | |
public abstract void colorChanged(int color); | |
} |
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
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