Skip to content
This repository has been archived by the owner on Oct 23, 2020. It is now read-only.

Fab position #320

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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
6 changes: 4 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
@@ -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
}
Expand All @@ -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'
105 changes: 84 additions & 21 deletions library/src/main/java/com/github/clans/fab/FloatingActionMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -323,14 +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;

usedWidth += child.getMeasuredWidth();
if (mOpenDirection == OPEN_CENTER && child == mMenuButton) continue;

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);
Expand All @@ -341,9 +345,15 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
}
}


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 += mButtonSpacing * (mButtonsCount - 1) + getPaddingTop() + getPaddingBottom();
height = adjustForOvershoot(height);

if (getLayoutParams().width == LayoutParams.MATCH_PARENT) {
Expand All @@ -359,15 +369,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());
Expand All @@ -378,9 +404,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 = menuButtonTop + mMenuButton.getMeasuredHeight() / 2 + mHeightActions / 2;
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);
Expand All @@ -392,7 +428,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(),
Expand All @@ -406,6 +458,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;
Expand All @@ -432,9 +488,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;
}
}
}

Expand Down Expand Up @@ -559,8 +622,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
Expand Down Expand Up @@ -985,7 +1048,7 @@ public void addMenuButton(FloatingActionButton fab, int index) {

public void removeAllMenuButtons() {
close(true);

List<FloatingActionButton> viewsToRemove = new ArrayList<>();
for (int i = 0; i < getChildCount(); i++) {
View v = getChildAt(i);
Expand Down
1 change: 1 addition & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
<attr name="menu_openDirection" format="enum">
<enum name="up" value="0" />
<enum name="down" value="1" />
<enum name="center" value="2" />
</attr>
<attr name="menu_backgroundColor" format="color" />
<attr name="menu_fab_label" format="string" />
Expand Down
11 changes: 6 additions & 5 deletions sample/build.gradle
Original file line number Diff line number Diff line change
@@ -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
}
Expand All @@ -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')
}
Loading