diff options
author | Aurelien Hubert <aurel.hubert@gmail.com> | 2016-04-22 16:15:36 +0200 |
---|---|---|
committer | Aurelien Hubert <aurel.hubert@gmail.com> | 2016-04-22 16:15:36 +0200 |
commit | 412f23f9af933445e16aeaec47a15bebb0ade8d1 (patch) | |
tree | 6156abf246832f08891595f50be1062da1b51673 | |
parent | 973cf979c414ee9c13855025142c2d38db12482b (diff) | |
download | android_external_ahbottomnavigation-412f23f9af933445e16aeaec47a15bebb0ade8d1.tar.gz android_external_ahbottomnavigation-412f23f9af933445e16aeaec47a15bebb0ade8d1.tar.bz2 android_external_ahbottomnavigation-412f23f9af933445e16aeaec47a15bebb0ade8d1.zip |
Added hideBottomNavigation()
Added CURRENT_ITEM_NONE to unselect all items
Improved Notifications (animation, size)
-rw-r--r-- | CHANGELOG.md | 8 | ||||
-rw-r--r-- | README.md | 8 | ||||
-rw-r--r-- | ahbottomnavigation/build.gradle | 6 | ||||
-rw-r--r-- | ahbottomnavigation/src/main/java/com/aurelhubert/ahbottomnavigation/AHBottomNavigation.java | 104 | ||||
-rw-r--r-- | ahbottomnavigation/src/main/java/com/aurelhubert/ahbottomnavigation/AHBottomNavigationBehavior.java | 13 | ||||
-rw-r--r-- | ahbottomnavigation/src/main/res/values/dimens.xml | 2 |
6 files changed, 106 insertions, 35 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 69bf535..47ec640 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,12 @@ ## Changelog -### Newest version: 1.1.4 +### Newest version: 1.1.5 + +* Added hideBottomNavigation() +* Added CURRENT_ITEM_NONE to unselect all items +* Improved Notifications (animation, size) + +### 1.1.4 * Updated lib dependencies @@ -5,9 +5,11 @@ Library to implement the Bottom Navigation component from Material Design guidel ## Demo <img src="https://raw.githubusercontent.com/aurelhubert/ahbottomnavigation/master/demo1.gif" width="208" height="368" /> <img src="https://raw.githubusercontent.com/aurelhubert/ahbottomnavigation/master/demo2.gif" width="208" height="368" /> <img src="https://raw.githubusercontent.com/aurelhubert/ahbottomnavigation/master/demo3.gif" width="208" height="368" /> <img src="https://raw.githubusercontent.com/aurelhubert/ahbottomnavigation/master/demo4.gif" width="208" height="368" /> -## What's new (1.1.4) - [Changelog](https://github.com/aurelhubert/ahbottomnavigation/blob/master/CHANGELOG.md) +## What's new (1.1.5) - [Changelog](https://github.com/aurelhubert/ahbottomnavigation/blob/master/CHANGELOG.md) -* Updated lib dependencies +* Added hideBottomNavigation() +* Unselect all items with setCurrentItem(AHBottomNavigation.CURRENT_ITEM_NONE) +* Improved Notifications (animation, position) ## Features * Follow the bottom navigation guidelines (https://www.google.com/design/spec/components/bottom-navigation.html) @@ -22,7 +24,7 @@ Library to implement the Bottom Navigation component from Material Design guidel ### Gradle ```groovy dependencies { - compile 'com.aurelhubert:ahbottomnavigation:1.1.4' + compile 'com.aurelhubert:ahbottomnavigation:1.1.5' } ``` ### XML diff --git a/ahbottomnavigation/build.gradle b/ahbottomnavigation/build.gradle index 3643a32..22f6e76 100644 --- a/ahbottomnavigation/build.gradle +++ b/ahbottomnavigation/build.gradle @@ -13,7 +13,7 @@ ext { siteUrl = 'https://github.com/aurelhubert/ahbottomnavigation' gitUrl = 'https://github.com/aurelhubert/ahbottomnavigation.git' - libraryVersion = '1.1.4' + libraryVersion = '1.1.5' developerId = 'aurelhubert' developerName = 'Aurelien Hubert' @@ -31,8 +31,8 @@ android { defaultConfig { minSdkVersion 14 targetSdkVersion 23 - versionCode 21 - versionName "1.1.4" + versionCode 22 + versionName "1.1.5" } buildTypes { release { diff --git a/ahbottomnavigation/src/main/java/com/aurelhubert/ahbottomnavigation/AHBottomNavigation.java b/ahbottomnavigation/src/main/java/com/aurelhubert/ahbottomnavigation/AHBottomNavigation.java index 20e2fc6..44ac23d 100644 --- a/ahbottomnavigation/src/main/java/com/aurelhubert/ahbottomnavigation/AHBottomNavigation.java +++ b/ahbottomnavigation/src/main/java/com/aurelhubert/ahbottomnavigation/AHBottomNavigation.java @@ -12,6 +12,7 @@ import android.support.annotation.ColorRes; import android.support.design.widget.CoordinatorLayout; import android.support.v4.content.ContextCompat; import android.support.v4.view.ViewCompat; +import android.support.v4.view.animation.LinearOutSlowInInterpolator; import android.util.AttributeSet; import android.util.Log; import android.util.TypedValue; @@ -20,6 +21,8 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewAnimationUtils; import android.view.ViewGroup; +import android.view.animation.AccelerateInterpolator; +import android.view.animation.OvershootInterpolator; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.LinearLayout; @@ -34,6 +37,10 @@ import java.util.List; */ public class AHBottomNavigation extends FrameLayout { + // Constant + public static final int CURRENT_ITEM_NONE = -1; + public static final int UPDATE_ALL_NOTIFICATIONS = -1; + // Static private static String TAG = "AHBottomNavigation"; private static final int MIN_ITEMS = 3; @@ -60,10 +67,12 @@ public class AHBottomNavigation extends FrameLayout { private int currentItem = 0; private int currentColor = 0; + private int bottomNavigationHeight; private float selectedItemWidth, notSelectedItemWidth; private boolean behaviorTranslationEnabled = true; private boolean forceTint = false; private boolean forceTitlesDisplay = false; + private boolean needHideBottomNavigation = false; // Notifications private @@ -75,7 +84,9 @@ public class AHBottomNavigation extends FrameLayout { private Drawable notificationBackgroundDrawable; private Typeface notificationTypeface; - + /** + * Constructors + */ public AHBottomNavigation(Context context) { super(context); init(context); @@ -122,13 +133,13 @@ public class AHBottomNavigation extends FrameLayout { accentColor = ContextCompat.getColor(this.context, R.color.colorAccent); inactiveColor = ContextCompat.getColor(this.context, R.color.colorInactive); notificationTextColor = ContextCompat.getColor(this.context, android.R.color.white); + bottomNavigationHeight = (int) resources.getDimension(R.dimen.bottom_navigation_height); + ViewCompat.setElevation(this, resources.getDimension(R.dimen.bottom_navigation_elevation)); setClipToPadding(false); ViewGroup.LayoutParams params = new ViewGroup.LayoutParams( - ViewGroup.LayoutParams.MATCH_PARENT, - (int) resources.getDimension(R.dimen.bottom_navigation_height) - ); + ViewGroup.LayoutParams.MATCH_PARENT, bottomNavigationHeight); setLayoutParams(params); } @@ -207,6 +218,10 @@ public class AHBottomNavigation extends FrameLayout { float activeSize = resources.getDimension(R.dimen.bottom_navigation_text_size_active); float inactiveSize = resources.getDimension(R.dimen.bottom_navigation_text_size_inactive); + + int activePaddingTop = (int) resources.getDimension(R.dimen.bottom_navigation_margin_top_active); + int notificationActiveMarginLeft = (int) resources.getDimension(R.dimen.bottom_navigation_notification_margin_left_active); + if (forceTitlesDisplay && items.size() > MIN_ITEMS) { activeSize = resources.getDimension(R.dimen.bottom_navigation_text_size_forced_active); inactiveSize = resources.getDimension(R.dimen.bottom_navigation_text_size_forced_inactive); @@ -221,6 +236,8 @@ public class AHBottomNavigation extends FrameLayout { FrameLayout container = (FrameLayout) view.findViewById(R.id.bottom_navigation_container); ImageView icon = (ImageView) view.findViewById(R.id.bottom_navigation_item_icon); TextView title = (TextView) view.findViewById(R.id.bottom_navigation_item_title); + TextView notification = (TextView) view.findViewById(R.id.bottom_navigation_notification); + icon.setImageDrawable(item.getDrawable(context)); title.setText(item.getTitle(context)); @@ -229,11 +246,16 @@ public class AHBottomNavigation extends FrameLayout { } if (i == currentItem) { - int activePaddingTop = (int) resources - .getDimension(R.dimen.bottom_navigation_margin_top_active); + // Update margins (icon & notification) if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) { ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) icon.getLayoutParams(); p.setMargins(p.leftMargin, activePaddingTop, p.rightMargin, p.bottomMargin); + + ViewGroup.MarginLayoutParams paramsNotification = (ViewGroup.MarginLayoutParams) + notification.getLayoutParams(); + paramsNotification.setMargins(notificationActiveMarginLeft, paramsNotification.topMargin, + paramsNotification.rightMargin, paramsNotification.bottomMargin); + view.requestLayout(); } } @@ -271,7 +293,7 @@ public class AHBottomNavigation extends FrameLayout { views.add(view); } - updateNotifications(true); + updateNotifications(true, UPDATE_ALL_NOTIFICATIONS); } /** @@ -300,7 +322,10 @@ public class AHBottomNavigation extends FrameLayout { itemWidth = maxWidth; } + int activeMarginTop = (int) resources.getDimension(R.dimen.bottom_navigation_small_margin_top_active); + int notificationActiveMarginLeft = (int) resources.getDimension(R.dimen.bottom_navigation_notification_margin_left_active); float difference = resources.getDimension(R.dimen.bottom_navigation_small_selected_width_difference); + selectedItemWidth = itemWidth + items.size() * difference; itemWidth -= difference; notSelectedItemWidth = itemWidth; @@ -313,15 +338,21 @@ public class AHBottomNavigation extends FrameLayout { View view = inflater.inflate(R.layout.bottom_navigation_small_item, this, false); ImageView icon = (ImageView) view.findViewById(R.id.bottom_navigation_small_item_icon); TextView title = (TextView) view.findViewById(R.id.bottom_navigation_small_item_title); + TextView notification = (TextView) view.findViewById(R.id.bottom_navigation_notification); icon.setImageDrawable(item.getDrawable(context)); title.setText(item.getTitle(context)); if (i == currentItem) { - int activeMarginTop = (int) resources - .getDimension(R.dimen.bottom_navigation_small_margin_top_active); + // Update margins (icon & notification) if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) { ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) icon.getLayoutParams(); p.setMargins(p.leftMargin, activeMarginTop, p.rightMargin, p.bottomMargin); + + ViewGroup.MarginLayoutParams paramsNotification = (ViewGroup.MarginLayoutParams) + notification.getLayoutParams(); + paramsNotification.setMargins(notificationActiveMarginLeft, paramsNotification.topMargin, + paramsNotification.rightMargin, paramsNotification.bottomMargin); + view.requestLayout(); } } @@ -359,7 +390,7 @@ public class AHBottomNavigation extends FrameLayout { views.add(view); } - updateNotifications(true); + updateNotifications(true, UPDATE_ALL_NOTIFICATIONS); } @@ -459,7 +490,12 @@ public class AHBottomNavigation extends FrameLayout { } currentItem = itemIndex; - currentColor = items.get(currentItem).getColor(context); + if (currentItem > 0 && currentItem < items.size()) { + currentColor = items.get(currentItem).getColor(context); + } else if (currentItem == CURRENT_ITEM_NONE) { + setBackgroundColor(defaultBackgroundColor); + backgroundColorView.setBackgroundColor(Color.TRANSPARENT); + } if (listener != null && useCallback) { listener.onTabSelected(itemIndex); @@ -563,7 +599,12 @@ public class AHBottomNavigation extends FrameLayout { } currentItem = itemIndex; - currentColor = items.get(currentItem).getColor(context); + if (currentItem > 0 && currentItem < items.size()) { + currentColor = items.get(currentItem).getColor(context); + } else if (currentItem == CURRENT_ITEM_NONE) { + setBackgroundColor(defaultBackgroundColor); + backgroundColorView.setBackgroundColor(Color.TRANSPARENT); + } if (listener != null && useCallback) { listener.onTabSelected(itemIndex); @@ -576,10 +617,16 @@ public class AHBottomNavigation extends FrameLayout { /** * Update notifications */ - private void updateNotifications(boolean updateStyle) { + private void updateNotifications(boolean updateStyle, int itemPosition) { float textSize = resources.getDimension(R.dimen.bottom_navigation_notification_text_size); float textSizeMin = resources.getDimension(R.dimen.bottom_navigation_notification_text_size_min); + for (int i = 0; i < views.size(); i++) { + + if (itemPosition != UPDATE_ALL_NOTIFICATIONS && itemPosition != i) { + continue; + } + TextView notification = (TextView) views.get(i).findViewById(R.id.bottom_navigation_notification); String currentValue = notification.getText().toString(); @@ -618,6 +665,7 @@ public class AHBottomNavigation extends FrameLayout { .scaleX(0) .scaleY(0) .alpha(0) + .setInterpolator(new AccelerateInterpolator()) .setDuration(150) .start(); } @@ -636,6 +684,7 @@ public class AHBottomNavigation extends FrameLayout { .scaleX(1) .scaleY(1) .alpha(1) + .setInterpolator(new OvershootInterpolator()) .setDuration(150) .start(); } @@ -827,6 +876,21 @@ public class AHBottomNavigation extends FrameLayout { ViewGroup.LayoutParams params = getLayoutParams(); bottomNavigationBehavior = new AHBottomNavigationBehavior<>(behaviorTranslationEnabled); ((CoordinatorLayout.LayoutParams) params).setBehavior(bottomNavigationBehavior); + if (needHideBottomNavigation) { + needHideBottomNavigation = false; + bottomNavigationBehavior.hideView(this, bottomNavigationHeight); + } + } + } + + /** + * Hide Bottom Navigation + */ + public void hideBottomNavigation() { + if (bottomNavigationBehavior != null) { + bottomNavigationBehavior.hideView(this, bottomNavigationHeight); + } else { + needHideBottomNavigation = true; } } @@ -921,7 +985,7 @@ public class AHBottomNavigation extends FrameLayout { return; } notifications[itemPosition] = nbNotification; - updateNotifications(false); + updateNotifications(false, itemPosition); } /** @@ -931,7 +995,7 @@ public class AHBottomNavigation extends FrameLayout { */ public void setNotificationTextColor(@ColorInt int textColor) { notificationTextColor = textColor; - updateNotifications(true); + updateNotifications(true, UPDATE_ALL_NOTIFICATIONS); } /** @@ -941,7 +1005,7 @@ public class AHBottomNavigation extends FrameLayout { */ public void setNotificationTextColorResource(@ColorRes int textColor) { notificationTextColor = ContextCompat.getColor(context, textColor); - updateNotifications(true); + updateNotifications(true, UPDATE_ALL_NOTIFICATIONS); } /** @@ -951,7 +1015,7 @@ public class AHBottomNavigation extends FrameLayout { */ public void setNotificationBackground(Drawable drawable) { notificationBackgroundDrawable = drawable; - updateNotifications(true); + updateNotifications(true, UPDATE_ALL_NOTIFICATIONS); } /** @@ -961,7 +1025,7 @@ public class AHBottomNavigation extends FrameLayout { */ public void setNotificationBackgroundColor(@ColorInt int color) { notificationBackgroundColor = color; - updateNotifications(true); + updateNotifications(true, UPDATE_ALL_NOTIFICATIONS); } /** @@ -971,7 +1035,7 @@ public class AHBottomNavigation extends FrameLayout { */ public void setNotificationBackgroundColorResource(@ColorRes int color) { notificationBackgroundColor = ContextCompat.getColor(context, color); - updateNotifications(true); + updateNotifications(true, UPDATE_ALL_NOTIFICATIONS); } /** @@ -981,7 +1045,7 @@ public class AHBottomNavigation extends FrameLayout { */ public void setNotificationBackgroundColorResource(Typeface typeface) { notificationTypeface = typeface; - updateNotifications(true); + updateNotifications(true, UPDATE_ALL_NOTIFICATIONS); } /** diff --git a/ahbottomnavigation/src/main/java/com/aurelhubert/ahbottomnavigation/AHBottomNavigationBehavior.java b/ahbottomnavigation/src/main/java/com/aurelhubert/ahbottomnavigation/AHBottomNavigationBehavior.java index 274e69a..321e373 100644 --- a/ahbottomnavigation/src/main/java/com/aurelhubert/ahbottomnavigation/AHBottomNavigationBehavior.java +++ b/ahbottomnavigation/src/main/java/com/aurelhubert/ahbottomnavigation/AHBottomNavigationBehavior.java @@ -14,7 +14,6 @@ import android.support.v4.view.ViewPropertyAnimatorCompat; import android.support.v4.view.ViewPropertyAnimatorUpdateListener; import android.support.v4.view.animation.LinearOutSlowInInterpolator; import android.util.AttributeSet; -import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.view.animation.Interpolator; @@ -173,10 +172,9 @@ public class AHBottomNavigationBehavior<V extends View> extends VerticalScrollin private void ensureOrCancelObjectAnimation(final V child, final int offset) { if (translationObjectAnimator != null) { - Log.d("AHBottomNav", "ensureOrCancelObjectAnimation: CANCEL"); translationObjectAnimator.cancel(); } - Log.d("AHBottomNav", "ensureOrCancelObjectAnimation: NEW"); + translationObjectAnimator = ObjectAnimator.ofFloat(child, View.TRANSLATION_Y, offset); translationObjectAnimator.setDuration(ANIM_DURATION); translationObjectAnimator.setInterpolator(INTERPOLATOR); @@ -195,10 +193,6 @@ public class AHBottomNavigationBehavior<V extends View> extends VerticalScrollin fabTargetOffset = fabDefaultBottomMargin - child.getTranslationY() + snackBarY; p.setMargins(p.leftMargin, p.topMargin, p.rightMargin, (int) fabTargetOffset); floatingActionButton.requestLayout(); - - Log.d("AHBottomNav", "onLayoutChange: " + fabTargetOffset + " / " - + fabDefaultBottomMargin + " / " + child.getTranslationY() + " / " - + snackBarY); } } }); @@ -223,6 +217,11 @@ public class AHBottomNavigationBehavior<V extends View> extends VerticalScrollin this.mTabLayoutId = tabId; } + + public void hideView(V view, int offset) { + animateOffset(view, offset); + } + public void resetOffset(V view) { animateOffset(view, 0); } diff --git a/ahbottomnavigation/src/main/res/values/dimens.xml b/ahbottomnavigation/src/main/res/values/dimens.xml index 664b045..5eff23a 100644 --- a/ahbottomnavigation/src/main/res/values/dimens.xml +++ b/ahbottomnavigation/src/main/res/values/dimens.xml @@ -37,7 +37,7 @@ <dimen name="bottom_navigation_notification_margin_top">4dp</dimen> <dimen name="bottom_navigation_notification_margin_left">16dp</dimen> <dimen name="bottom_navigation_notification_margin_left_active">26dp</dimen> - <dimen name="bottom_navigation_notification_text_size">10sp</dimen> + <dimen name="bottom_navigation_notification_text_size">9sp</dimen> <dimen name="bottom_navigation_notification_text_size_min">8sp</dimen> </resources> |