From ec1e7745079bc680a50a277ce334f6888dd18bc4 Mon Sep 17 00:00:00 2001 From: Soichiro Kashima Date: Wed, 14 Jan 2015 23:42:14 +0900 Subject: [PATCH] Fix Toolbar blinks when scrolling fast. #38 --- .../ToolbarControlListViewActivity.java | 60 +++++++++++++------ 1 file changed, 43 insertions(+), 17 deletions(-) diff --git a/observablescrollview-samples/src/main/java/com/github/ksoichiro/android/observablescrollview/samples/ToolbarControlListViewActivity.java b/observablescrollview-samples/src/main/java/com/github/ksoichiro/android/observablescrollview/samples/ToolbarControlListViewActivity.java index 3109035c..0b69cee3 100644 --- a/observablescrollview-samples/src/main/java/com/github/ksoichiro/android/observablescrollview/samples/ToolbarControlListViewActivity.java +++ b/observablescrollview-samples/src/main/java/com/github/ksoichiro/android/observablescrollview/samples/ToolbarControlListViewActivity.java @@ -74,11 +74,11 @@ public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCoun @Override public void onScrollChanged(int scrollY, boolean firstScroll, boolean dragging) { - int toolbarHeight = mToolbarView.getHeight(); - if (dragging || scrollY < toolbarHeight) { + if (dragging) { + int toolbarHeight = mToolbarView.getHeight(); if (firstScroll) { float currentHeaderTranslationY = ViewHelper.getTranslationY(mHeaderView); - if (-toolbarHeight < currentHeaderTranslationY && toolbarHeight < scrollY) { + if (-toolbarHeight < currentHeaderTranslationY) { mBaseTranslationY = scrollY; } } @@ -96,22 +96,48 @@ public void onDownMotionEvent() { public void onUpOrCancelMotionEvent(ScrollState scrollState) { mBaseTranslationY = 0; - float headerTranslationY = ViewHelper.getTranslationY(mHeaderView); - int toolbarHeight = mToolbarView.getHeight(); - if (scrollState == ScrollState.UP) { - if (toolbarHeight < mListView.getCurrentScrollY()) { - if (headerTranslationY != -toolbarHeight) { - ViewPropertyAnimator.animate(mHeaderView).cancel(); - ViewPropertyAnimator.animate(mHeaderView).translationY(-toolbarHeight).setDuration(200).start(); - } + if (scrollState == ScrollState.DOWN) { + showToolbar(); + } else if (scrollState == ScrollState.UP) { + int toolbarHeight = mToolbarView.getHeight(); + int scrollY = mListView.getCurrentScrollY(); + if (toolbarHeight <= scrollY) { + hideToolbar(); + } else { + showToolbar(); } - } else if (scrollState == ScrollState.DOWN) { - if (toolbarHeight < mListView.getCurrentScrollY()) { - if (headerTranslationY != 0) { - ViewPropertyAnimator.animate(mHeaderView).cancel(); - ViewPropertyAnimator.animate(mHeaderView).translationY(0).setDuration(200).start(); - } + } else { + // Even if onScrollChanged occurs without scrollY changing, toolbar should be adjusted + if (!toolbarIsShown() && !toolbarIsHidden()) { + // Toolbar is moving but doesn't know which to move: + // you can change this to hideToolbar() + showToolbar(); } } } + + private boolean toolbarIsShown() { + return ViewHelper.getTranslationY(mHeaderView) == 0; + } + + private boolean toolbarIsHidden() { + return ViewHelper.getTranslationY(mHeaderView) == -mToolbarView.getHeight(); + } + + private void showToolbar() { + float headerTranslationY = ViewHelper.getTranslationY(mHeaderView); + if (headerTranslationY != 0) { + ViewPropertyAnimator.animate(mHeaderView).cancel(); + ViewPropertyAnimator.animate(mHeaderView).translationY(0).setDuration(200).start(); + } + } + + private void hideToolbar() { + float headerTranslationY = ViewHelper.getTranslationY(mHeaderView); + int toolbarHeight = mToolbarView.getHeight(); + if (headerTranslationY != -toolbarHeight) { + ViewPropertyAnimator.animate(mHeaderView).cancel(); + ViewPropertyAnimator.animate(mHeaderView).translationY(-toolbarHeight).setDuration(200).start(); + } + } }