From 7118bb45ca2dd563919557538c941e38079ebc2d Mon Sep 17 00:00:00 2001 From: timofey malygin Date: Fri, 7 Oct 2016 17:45:28 +0300 Subject: [PATCH 1/2] add center attribut for fab action --- build.gradle | 2 +- gradle/wrapper/gradle-wrapper.properties | 4 +- library/build.gradle | 6 +- .../github/clans/fab/FloatingActionMenu.java | 98 ++++++-- library/src/main/res/values/attrs.xml | 1 + sample/build.gradle | 11 +- sample/src/main/res/layout/menus_fragment.xml | 214 +++++++++++------- 7 files changed, 221 insertions(+), 115 deletions(-) diff --git a/build.gradle b/build.gradle index 28b37cb..698b43c 100755 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.1.0' + classpath 'com.android.tools.build:gradle:2.2.0' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index ff74751..2290083 100755 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Mon Apr 11 11:06:38 EEST 2016 +#Fri Oct 07 16:07:34 MSK 2016 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip diff --git a/library/build.gradle b/library/build.gradle index 5493a7b..4493a35 100755 --- a/library/build.gradle +++ b/library/build.gradle @@ -1,12 +1,12 @@ apply plugin: 'com.android.library' android { - compileSdkVersion 23 + compileSdkVersion 24 buildToolsVersion "23.0.3" defaultConfig { minSdkVersion 14 - targetSdkVersion 23 + targetSdkVersion 24 versionCode Integer.parseInt(project.VERSION_CODE) versionName project.VERSION_NAME } @@ -25,6 +25,8 @@ android { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) + compile 'com.android.support:support-compat:24.2.0' + compile 'com.android.support:appcompat-v7:24.2.0' } apply from: '../gradle-mvn-push.gradle' diff --git a/library/src/main/java/com/github/clans/fab/FloatingActionMenu.java b/library/src/main/java/com/github/clans/fab/FloatingActionMenu.java index 0b80e4c..04a83a0 100755 --- a/library/src/main/java/com/github/clans/fab/FloatingActionMenu.java +++ b/library/src/main/java/com/github/clans/fab/FloatingActionMenu.java @@ -14,7 +14,6 @@ import android.util.AttributeSet; import android.util.TypedValue; import android.view.ContextThemeWrapper; -import android.view.GestureDetector; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; @@ -37,6 +36,7 @@ public class FloatingActionMenu extends ViewGroup { private static final int OPEN_UP = 0; private static final int OPEN_DOWN = 1; + private static final int OPEN_CENTER = 2; private static final int LABELS_POSITION_LEFT = 0; private static final int LABELS_POSITION_RIGHT = 1; @@ -328,6 +328,8 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { if (child.getVisibility() == GONE || child == mImageToggle) continue; + if (mOpenDirection == OPEN_CENTER && child == mMenuButton) continue; + usedWidth += child.getMeasuredWidth(); height += child.getMeasuredHeight(); @@ -343,7 +345,12 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { width = Math.max(mMaxButtonWidth, maxLabelWidth + mLabelsMargin) + getPaddingLeft() + getPaddingRight(); - height += mButtonSpacing * (mButtonsCount - 1) + getPaddingTop() + getPaddingBottom(); + if (mOpenDirection == OPEN_CENTER) { + width += mMenuButton.getMeasuredWidth(); + } else { + height = Math.max(mMenuButton.getMeasuredHeight(), height + mButtonSpacing * (mButtonsCount - 1)) + getPaddingTop() + getPaddingBottom(); + } + height = adjustForOvershoot(height); if (getLayoutParams().width == LayoutParams.MATCH_PARENT) { @@ -359,15 +366,31 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { - int buttonsHorizontalCenter = mLabelsPosition == LABELS_POSITION_LEFT - ? r - l - mMaxButtonWidth / 2 - getPaddingRight() + final int width = r - l; + final int height = b - t; + + final int buttonsHorizontalCenter = mLabelsPosition == LABELS_POSITION_LEFT + ? width - mMaxButtonWidth / 2 - getPaddingRight() : mMaxButtonWidth / 2 + getPaddingLeft(); - boolean openUp = mOpenDirection == OPEN_UP; - int menuButtonTop = openUp - ? b - t - mMenuButton.getMeasuredHeight() - getPaddingBottom() - : getPaddingTop(); - int menuButtonLeft = buttonsHorizontalCenter - mMenuButton.getMeasuredWidth() / 2; + int menuButtonTop; + int menuButtonLeft; + + switch (mOpenDirection) { + case OPEN_DOWN: + menuButtonTop = getPaddingTop(); + menuButtonLeft = buttonsHorizontalCenter - mMenuButton.getMeasuredWidth() / 2; + break; + case OPEN_CENTER: + menuButtonTop = (height) / 2 - mMenuButton.getMeasuredHeight() / 2 - getPaddingBottom(); + menuButtonLeft = buttonsHorizontalCenter - mMenuButton.getMeasuredWidth() / 2; + break; + case OPEN_UP: + default: + menuButtonTop = height - mMenuButton.getMeasuredHeight() - getPaddingBottom(); + menuButtonLeft = buttonsHorizontalCenter - mMenuButton.getMeasuredWidth() / 2; + break; + } mMenuButton.layout(menuButtonLeft, menuButtonTop, menuButtonLeft + mMenuButton.getMeasuredWidth(), menuButtonTop + mMenuButton.getMeasuredHeight()); @@ -378,9 +401,19 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) { mImageToggle.layout(imageLeft, imageTop, imageLeft + mImageToggle.getMeasuredWidth(), imageTop + mImageToggle.getMeasuredHeight()); - int nextY = openUp - ? menuButtonTop + mMenuButton.getMeasuredHeight() + mButtonSpacing - : menuButtonTop; + int nextY; + switch (mOpenDirection) { + case OPEN_CENTER: + nextY = height - getPaddingBottom(); + break; + case OPEN_DOWN: + nextY = menuButtonTop; + break; + case OPEN_UP: + default: + nextY = menuButtonTop + mMenuButton.getMeasuredHeight() + mButtonSpacing; + break; + } for (int i = mButtonsCount - 1; i >= 0; i--) { View child = getChildAt(i); @@ -392,7 +425,23 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) { if (fab.getVisibility() == GONE) continue; int childX = buttonsHorizontalCenter - fab.getMeasuredWidth() / 2; - int childY = openUp ? nextY - fab.getMeasuredHeight() - mButtonSpacing : nextY; + int childY; + switch (mOpenDirection) { + case OPEN_CENTER: + if (fab == mMenuButton) { + continue; + } + childX = buttonsHorizontalCenter - fab.getMeasuredWidth() / 2 + (mLabelsPosition == LABELS_POSITION_LEFT ? -mMenuButton.getMeasuredWidth() : mMenuButton.getMeasuredWidth()); + childY = nextY - fab.getMeasuredHeight() - mButtonSpacing; + break; + case OPEN_DOWN: + childY = nextY; + break; + case OPEN_UP: + default: + childY = nextY - fab.getMeasuredHeight() - mButtonSpacing; + break; + } if (fab != mMenuButton) { fab.layout(childX, childY, childX + fab.getMeasuredWidth(), @@ -406,6 +455,10 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) { View label = (View) fab.getTag(R.id.fab_label); if (label != null) { int labelsOffset = (mUsingMenuLabel ? mMaxButtonWidth / 2 : fab.getMeasuredWidth() / 2) + mLabelsMargin; + if (mOpenDirection == OPEN_CENTER) { + labelsOffset += mMenuButton.getMeasuredWidth(); + } + int labelXNearButton = mLabelsPosition == LABELS_POSITION_LEFT ? buttonsHorizontalCenter - labelsOffset : buttonsHorizontalCenter + labelsOffset; @@ -432,9 +485,16 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) { } } - nextY = openUp - ? childY - mButtonSpacing - : childY + child.getMeasuredHeight() + mButtonSpacing; + switch (mOpenDirection) { + case OPEN_DOWN: + nextY = childY + child.getMeasuredHeight() + mButtonSpacing; + break; + case OPEN_CENTER: + case OPEN_UP: + default: + nextY = childY - mButtonSpacing; + break; + } } } @@ -559,8 +619,8 @@ protected MarginLayoutParams generateLayoutParams(LayoutParams p) { @Override protected MarginLayoutParams generateDefaultLayoutParams() { - return new MarginLayoutParams(MarginLayoutParams.WRAP_CONTENT, - MarginLayoutParams.WRAP_CONTENT); + //noinspection ResourceType + return new MarginLayoutParams(MarginLayoutParams.WRAP_CONTENT, MarginLayoutParams.WRAP_CONTENT); } @Override @@ -985,7 +1045,7 @@ public void addMenuButton(FloatingActionButton fab, int index) { public void removeAllMenuButtons() { close(true); - + List viewsToRemove = new ArrayList<>(); for (int i = 0; i < getChildCount(); i++) { View v = getChildAt(i); diff --git a/library/src/main/res/values/attrs.xml b/library/src/main/res/values/attrs.xml index a88bc79..3761539 100755 --- a/library/src/main/res/values/attrs.xml +++ b/library/src/main/res/values/attrs.xml @@ -77,6 +77,7 @@ + diff --git a/sample/build.gradle b/sample/build.gradle index 0ed7dcc..2cfa4a5 100755 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -1,13 +1,13 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 23 + compileSdkVersion 24 buildToolsVersion "23.0.3" defaultConfig { applicationId "com.github.fab.sample" minSdkVersion 14 - targetSdkVersion 23 + targetSdkVersion 24 versionCode Integer.parseInt(project.VERSION_CODE) versionName project.VERSION_NAME } @@ -28,8 +28,9 @@ android { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) - compile 'com.android.support:appcompat-v7:23.3.0' - compile 'com.android.support:recyclerview-v7:23.3.0' - compile 'com.android.support:design:23.3.0' + compile 'com.android.support:support-compat:24.2.0' + compile 'com.android.support:appcompat-v7:24.2.0' + compile 'com.android.support:recyclerview-v7:24.2.0' + compile 'com.android.support:design:24.2.0' compile project(':library') } diff --git a/sample/src/main/res/layout/menus_fragment.xml b/sample/src/main/res/layout/menus_fragment.xml index ee76f6a..1dc1521 100644 --- a/sample/src/main/res/layout/menus_fragment.xml +++ b/sample/src/main/res/layout/menus_fragment.xml @@ -2,33 +2,33 @@ + android:layout_height="match_parent" + android:orientation="vertical"> + android:maxLines="3" + android:paddingLeft="60dp" + android:paddingRight="8dp" + android:paddingTop="8dp" + android:text="@string/lorem_ipsum" + android:textColor="@android:color/white" + android:textSize="22sp" /> + android:text="@string/lorem_ipsum_large" + android:textSize="18sp" /> + fab:menu_colorRipple="#D99200" + fab:menu_labels_hideAnimation="@anim/jump_to_down" + fab:menu_labels_showAnimation="@anim/jump_from_down" + fab:menu_labels_style="@style/MenuLabelsStyle" + fab:menu_shadowColor="#444"> + fab:fab_label="Menu item 1" /> + fab:fab_label="Menu item 2" /> + fab:fab_label="@string/lorem_ipsum" /> + fab:menu_colorNormal="#1565C0" + fab:menu_colorPressed="#2272CD" + fab:menu_colorRipple="#62B2FF" + fab:menu_fab_size="mini"> + android:src="@drawable/ic_star" /> + android:src="@drawable/ic_star" /> + android:src="@drawable/ic_star" /> + fab:menu_colorRipple="#62B2FF" + fab:menu_fab_size="mini" + fab:menu_labels_position="left" + fab:menu_openDirection="center"> + fab:fab_label="asdad" /> + fab:fab_label="asdad2" /> + fab:fab_label="asdad3" /> + + fab:fab_shadowColor="#000" + fab:fab_size="mini" + tools:visibility="visible" /> + fab:fab_label="Menu item 1" + fab:fab_size="mini" /> + fab:fab_label="Menu item 2" + fab:fab_size="mini" /> + fab:fab_label="@string/lorem_ipsum" + fab:fab_size="mini" /> @@ -215,37 +219,37 @@ android:layout_height="match_parent" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" - android:paddingRight="10dp" android:paddingBottom="10dp" android:paddingLeft="10dp" - fab:menu_labels_ellipsize="end" - fab:menu_labels_singleLine="true" + android:paddingRight="10dp" fab:menu_backgroundColor="#ccffffff" - fab:menu_fab_label="Menu label"> + fab:menu_fab_label="Menu label" + fab:menu_labels_ellipsize="end" + fab:menu_labels_singleLine="true"> + fab:fab_label="Disabled" + fab:fab_size="mini" /> + fab:fab_label="Remove button" + fab:fab_size="mini" /> + fab:fab_label="Restore Button" + fab:fab_size="mini" /> @@ -254,33 +258,71 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentBottom="true" - android:paddingRight="10dp" android:paddingBottom="10dp" android:paddingLeft="10dp" - fab:menu_labels_ellipsize="end" - fab:menu_labels_singleLine="true" + android:paddingRight="10dp" fab:menu_backgroundColor="#ccffffff" - fab:menu_labels_position="right"> + fab:menu_labels_ellipsize="end" + fab:menu_labels_position="right" + fab:menu_labels_singleLine="true"> + + + + + + + + + + From 1ae5afeac4964bb04154f4763f73a3491ffa8632 Mon Sep 17 00:00:00 2001 From: timofey malygin Date: Fri, 7 Oct 2016 17:57:02 +0300 Subject: [PATCH 2/2] fix: actions location and android:layout_height="match_parent" --- .../java/com/github/clans/fab/FloatingActionMenu.java | 11 +++++++---- sample/src/main/res/layout/menus_fragment.xml | 7 +++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/library/src/main/java/com/github/clans/fab/FloatingActionMenu.java b/library/src/main/java/com/github/clans/fab/FloatingActionMenu.java index 04a83a0..7118879 100755 --- a/library/src/main/java/com/github/clans/fab/FloatingActionMenu.java +++ b/library/src/main/java/com/github/clans/fab/FloatingActionMenu.java @@ -48,6 +48,7 @@ public class FloatingActionMenu extends ViewGroup { private int mButtonSpacing = Util.dpToPx(getContext(), 0f); private FloatingActionButton mMenuButton; private int mMaxButtonWidth; + private int mHeightActions; private int mLabelsMargin = Util.dpToPx(getContext(), 0f); private int mLabelsVerticalOffset = Util.dpToPx(getContext(), 0f); private int mButtonsCount; @@ -323,16 +324,17 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { } for (int i = 0; i < mButtonsCount; i++) { - int usedWidth = 0; + int usedWidth; View child = getChildAt(i); if (child.getVisibility() == GONE || child == mImageToggle) continue; if (mOpenDirection == OPEN_CENTER && child == mMenuButton) continue; - usedWidth += child.getMeasuredWidth(); + usedWidth = child.getMeasuredWidth(); height += child.getMeasuredHeight(); + Label label = (Label) child.getTag(R.id.fab_label); if (label != null) { int labelOffset = (mMaxButtonWidth - child.getMeasuredWidth()) / (mUsingMenuLabel ? 1 : 2); @@ -343,13 +345,14 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { } } - width = Math.max(mMaxButtonWidth, maxLabelWidth + mLabelsMargin) + getPaddingLeft() + getPaddingRight(); + width = Math.max(mMaxButtonWidth, maxLabelWidth + mLabelsMargin) + getPaddingLeft() + getPaddingRight(); if (mOpenDirection == OPEN_CENTER) { width += mMenuButton.getMeasuredWidth(); } else { height = Math.max(mMenuButton.getMeasuredHeight(), height + mButtonSpacing * (mButtonsCount - 1)) + getPaddingTop() + getPaddingBottom(); } + mHeightActions = height; height = adjustForOvershoot(height); @@ -404,7 +407,7 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) { int nextY; switch (mOpenDirection) { case OPEN_CENTER: - nextY = height - getPaddingBottom(); + nextY = menuButtonTop + mMenuButton.getMeasuredHeight() / 2 + mHeightActions / 2; break; case OPEN_DOWN: nextY = menuButtonTop; diff --git a/sample/src/main/res/layout/menus_fragment.xml b/sample/src/main/res/layout/menus_fragment.xml index 1dc1521..451f6ff 100644 --- a/sample/src/main/res/layout/menus_fragment.xml +++ b/sample/src/main/res/layout/menus_fragment.xml @@ -116,9 +116,8 @@