Skip to content

Commit

Permalink
Fixed issues related to hide on idle feature
Browse files Browse the repository at this point in the history
  • Loading branch information
romandanylyk committed Sep 11, 2018
1 parent 83ec4a1 commit 2755ba9
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 76 deletions.
113 changes: 67 additions & 46 deletions pageindicatorview/src/main/java/com/rd/PageIndicatorView.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import android.database.DataSetObserver;
import android.graphics.Canvas;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
Expand All @@ -19,12 +21,7 @@
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

import com.rd.animation.type.AnimationType;
import com.rd.animation.type.BaseAnimation;
import com.rd.animation.type.ColorAnimation;
import com.rd.animation.type.FillAnimation;
import com.rd.animation.type.ScaleAnimation;
import com.rd.animation.type.*;
import com.rd.draw.controller.DrawController;
import com.rd.draw.data.Indicator;
import com.rd.draw.data.Orientation;
Expand All @@ -34,16 +31,13 @@
import com.rd.utils.DensityUtils;
import com.rd.utils.IdUtils;

import java.util.Timer;
import java.util.TimerTask;
public class PageIndicatorView extends View implements ViewPager.OnPageChangeListener, IndicatorManager.Listener, ViewPager.OnAdapterChangeListener, View.OnTouchListener {

public class PageIndicatorView extends View implements ViewPager.OnPageChangeListener, IndicatorManager.Listener, ViewPager.OnAdapterChangeListener {
private static final Handler HANDLER = new Handler(Looper.getMainLooper());

private IndicatorManager manager;
private DataSetObserver setObserver;
private ViewPager viewPager;
private Timer idleTimer = new Timer();
private TimerTask idleTimerTask;
private boolean isInteractionEnabled;

public PageIndicatorView(Context context) {
Expand Down Expand Up @@ -123,6 +117,22 @@ public boolean onTouchEvent(MotionEvent event) {
return true;
}

@Override
public boolean onTouch(View v, MotionEvent event) {
if (!manager.indicator().isFadeOnIdle()) return false;

switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
stopIdleRunnable();
break;

case MotionEvent.ACTION_UP:
startIdleRunnable();
break;
}
return false;
}

@Override
public void onIndicatorUpdated() {
invalidate();
Expand All @@ -142,11 +152,7 @@ public void onPageSelected(int position) {
public void onPageScrollStateChanged(int state) {
if (state == ViewPager.SCROLL_STATE_IDLE) {
manager.indicator().setInteractiveAnimation(isInteractionEnabled);
} else {
manager.indicator().setIdle(false);
startIdleTimerTask();
}
updateAlpha();
}

@Override
Expand Down Expand Up @@ -194,15 +200,19 @@ public void setDynamicCount(boolean dynamicCount) {

/**
* Fade on idle will make {@link PageIndicatorView} {@link View#INVISIBLE} if {@link ViewPager} is not interacted
* in time equal to {@link Indicator#millisToBecomeIdle}. Take care when setting {@link PageIndicatorView} alpha
* in time equal to {@link Indicator#idleDuration}. Take care when setting {@link PageIndicatorView} alpha
* manually if this is true. Alpha is used to manage fading and appearance of {@link PageIndicatorView} and value you provide
* will be overridden when {@link PageIndicatorView} enters or leaves idle state.
*
* @param fadeOnIdle boolean value to hide {@link PageIndicatorView} when {@link ViewPager} is idle
*/
public void setFadeOnIdle(boolean fadeOnIdle) {
manager.indicator().setFadeOnIdle(fadeOnIdle);
updateAlpha();
if (fadeOnIdle) {
startIdleRunnable();
} else {
stopIdleRunnable();
}
}

/**
Expand Down Expand Up @@ -441,11 +451,15 @@ public void setAnimationDuration(long duration) {
* If {@link Indicator#fadeOnIdle} is true, {@link PageIndicatorView} will
* fade away after entering idle state and appear when it is left.
*
* @param millis time in millis after which {@link ViewPager} is considered idle
* @param duration time in millis after which {@link ViewPager} is considered idle
*/
public void setMillisToBecomeIdle(long millis) {
manager.indicator().setMillisToBecomeIdle(millis);
updateAlpha();
public void setIdleDuration(long duration) {
manager.indicator().setIdleDuration(duration);
if (manager.indicator().isFadeOnIdle()) {
startIdleRunnable();
} else {
stopIdleRunnable();
}
}

/**
Expand Down Expand Up @@ -491,6 +505,7 @@ public void setInteractiveAnimation(boolean isInteractive) {
*
* @param pager instance of {@link ViewPager} to work with
*/
@SuppressLint("ClickableViewAccessibility")
public void setViewPager(@Nullable ViewPager pager) {
releaseViewPager();
if (pager == null) {
Expand All @@ -500,6 +515,7 @@ public void setViewPager(@Nullable ViewPager pager) {
viewPager = pager;
viewPager.addOnPageChangeListener(this);
viewPager.addOnAdapterChangeListener(this);
viewPager.setOnTouchListener(this);

This comment has been minimized.

Copy link
@tristangrichard

tristangrichard Nov 8, 2018

This breaks everyones code if they have an ontouchlistener me included.

manager.indicator().setViewPagerId(viewPager.getId());

setDynamicCount(manager.indicator().isDynamicCount());
Expand All @@ -512,6 +528,7 @@ public void setViewPager(@Nullable ViewPager pager) {
public void releaseViewPager() {
if (viewPager != null) {
viewPager.removeOnPageChangeListener(this);
viewPager.removeOnAdapterChangeListener(this);
viewPager = null;
}
}
Expand Down Expand Up @@ -651,6 +668,10 @@ public void setClickListener(@Nullable DrawController.ClickListener listener) {
private void init(@Nullable AttributeSet attrs) {
setupId();
initIndicatorManager(attrs);

if (manager.indicator().isFadeOnIdle()) {
startIdleRunnable();
}
}

private void setupId() {
Expand Down Expand Up @@ -737,19 +758,6 @@ private void updateVisibility() {
}
}

private void updateAlpha() {
if (manager.indicator().isFadeOnIdle()) {
if (manager.indicator().isIdle()) {
animate().cancel();
animate().alpha(0F).setDuration(100L);

} else {
animate().cancel();
setAlpha(1F);
}
}
}

private void onPageSelect(int position) {
Indicator indicator = manager.indicator();
boolean canSelectIndicator = isViewMeasured();
Expand Down Expand Up @@ -847,18 +855,31 @@ private int adjustPosition(int position) {
return position;
}

private void startIdleTimerTask() {
if (idleTimerTask != null) {
idleTimerTask.cancel();
}
idleTimerTask = new TimerTask() {
@Override
public void run() {
manager.indicator().setIdle(true);
updateAlpha();
}
};
idleTimer.schedule(idleTimerTask, manager.indicator().getMillisToBecomeIdle());
private void displayWithAnimation() {
animate().cancel();
animate().alpha(1.0f).setDuration(Indicator.IDLE_ANIMATION_DURATION);
}

private void hideWithAnimation() {
animate().cancel();
animate().alpha(0f).setDuration(Indicator.IDLE_ANIMATION_DURATION);
}

private void startIdleRunnable() {
HANDLER.removeCallbacks(idleRunnable);
HANDLER.postDelayed(idleRunnable, manager.indicator().getIdleDuration());
}

private void stopIdleRunnable() {
HANDLER.removeCallbacks(idleRunnable);
displayWithAnimation();
}

private Runnable idleRunnable = new Runnable() {
@Override
public void run() {
manager.indicator().setIdle(true);
hideWithAnimation();
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class AttributeController {

private Indicator indicator;

private static final int DEFAULT_MILLIS_TO_BECOME_IDLE = 1500;
private static final int DEFAULT_IDLE_DURATION = 3000;

public AttributeController(@NonNull Indicator indicator) {
this.indicator = indicator;
Expand Down Expand Up @@ -65,7 +65,6 @@ private void initCountAttribute(@NonNull TypedArray typedArray) {
indicator.setLastSelectedPosition(position);
}


private void initColorAttribute(@NonNull TypedArray typedArray) {
int unselectedColor = typedArray.getColor(R.styleable.PageIndicatorView_piv_unselectedColor, Color.parseColor(ColorAnimation.DEFAULT_UNSELECTED_COLOR));
int selectedColor = typedArray.getColor(R.styleable.PageIndicatorView_piv_selectedColor, Color.parseColor(ColorAnimation.DEFAULT_SELECTED_COLOR));
Expand All @@ -76,7 +75,7 @@ private void initColorAttribute(@NonNull TypedArray typedArray) {

private void initAnimationAttribute(@NonNull TypedArray typedArray) {
boolean interactiveAnimation = typedArray.getBoolean(R.styleable.PageIndicatorView_piv_interactiveAnimation, false);
int animationDuration = typedArray.getInt(R.styleable.PageIndicatorView_piv_animationDuration, BaseAnimation.DEFAULT_ANIMATION_TIME);
long animationDuration = (long) typedArray.getInt(R.styleable.PageIndicatorView_piv_animationDuration, BaseAnimation.DEFAULT_ANIMATION_TIME);
if (animationDuration < 0) {
animationDuration = 0;
}
Expand All @@ -88,14 +87,14 @@ private void initAnimationAttribute(@NonNull TypedArray typedArray) {
RtlMode rtlMode = getRtlMode(rtlIndex);

boolean fadeOnIdle = typedArray.getBoolean(R.styleable.PageIndicatorView_piv_fadeOnIdle, false);
long millisToBecomeIdle = (long) typedArray.getInt(R.styleable.PageIndicatorView_piv_millisToBecomeIdle, DEFAULT_MILLIS_TO_BECOME_IDLE);
long idleDuration = (long) typedArray.getInt(R.styleable.PageIndicatorView_piv_idleDuration, DEFAULT_IDLE_DURATION);

indicator.setAnimationDuration(animationDuration);
indicator.setInteractiveAnimation(interactiveAnimation);
indicator.setAnimationType(animationType);
indicator.setRtlMode(rtlMode);
indicator.setFadeOnIdle(fadeOnIdle);
indicator.setMillisToBecomeIdle(millisToBecomeIdle);
indicator.setIdleDuration(idleDuration);
}

private void initSizeAttribute(@NonNull TypedArray typedArray) {
Expand Down
50 changes: 26 additions & 24 deletions pageindicatorview/src/main/java/com/rd/draw/data/Indicator.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class Indicator {

public static final int DEFAULT_RADIUS_DP = 6;
public static final int DEFAULT_PADDING_DP = 8;
public static final int IDLE_ANIMATION_DURATION = 250;

private int height;
private int width;
Expand All @@ -32,11 +33,12 @@ public class Indicator {
private boolean interactiveAnimation;
private boolean autoVisibility;
private boolean dynamicCount;

private boolean fadeOnIdle;
private boolean idle;
private boolean isIdle;
private long idleDuration;

private long animationDuration;
private long millisToBecomeIdle;
private int count = DEFAULT_COUNT;

private int selectedPosition;
Expand Down Expand Up @@ -178,11 +180,19 @@ public void setFadeOnIdle(boolean fadeOnIdle) {
}

public boolean isIdle() {
return idle;
return isIdle;
}

public void setIdle(boolean idle) {
this.idle = idle;
isIdle = idle;
}

public long getIdleDuration() {
return idleDuration;
}

public void setIdleDuration(long idleDuration) {
this.idleDuration = idleDuration;
}

public long getAnimationDuration() {
Expand All @@ -193,6 +203,14 @@ public void setAnimationDuration(long animationDuration) {
this.animationDuration = animationDuration;
}

public int getCount() {
return count;
}

public void setCount(int count) {
this.count = count;
}

public int getSelectedPosition() {
return selectedPosition;
}
Expand All @@ -217,12 +235,12 @@ public void setLastSelectedPosition(int lastSelectedPosition) {
this.lastSelectedPosition = lastSelectedPosition;
}

public int getCount() {
return count;
public int getViewPagerId() {
return viewPagerId;
}

public void setCount(int count) {
this.count = count;
public void setViewPagerId(int viewPagerId) {
this.viewPagerId = viewPagerId;
}

@NonNull
Expand All @@ -249,14 +267,6 @@ public void setAnimationType(AnimationType animationType) {
this.animationType = animationType;
}

public long getMillisToBecomeIdle() {
return millisToBecomeIdle;
}

public void setMillisToBecomeIdle(long millisToBecomeIdle) {
this.millisToBecomeIdle = millisToBecomeIdle;
}

@NonNull
public RtlMode getRtlMode() {
if (rtlMode == null) {
Expand All @@ -268,12 +278,4 @@ public RtlMode getRtlMode() {
public void setRtlMode(RtlMode rtlMode) {
this.rtlMode = rtlMode;
}

public int getViewPagerId() {
return viewPagerId;
}

public void setViewPagerId(int viewPagerId) {
this.viewPagerId = viewPagerId;
}
}
2 changes: 1 addition & 1 deletion pageindicatorview/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<attr name="piv_selectedColor" format="color" />

<attr name="piv_fadeOnIdle" format="boolean" />
<attr name="piv_millisToBecomeIdle" format="integer" />
<attr name="piv_idleDuration" format="integer" />

<attr name="piv_interactiveAnimation" format="boolean" />
<attr name="piv_animationDuration" format="integer" />
Expand Down
1 change: 1 addition & 0 deletions sample/src/main/res/layout/ac_home.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="48dp"
app:piv_animationType="drop"
attrs:piv_padding="12dp"
attrs:piv_radius="8dp" />

Expand Down

0 comments on commit 2755ba9

Please sign in to comment.