diff --git a/library/src/main/java/com/oguzdev/circularfloatingactionmenu/library/FloatingActionMenu.java b/library/src/main/java/com/oguzdev/circularfloatingactionmenu/library/FloatingActionMenu.java index 184897d..cc3b3e4 100644 --- a/library/src/main/java/com/oguzdev/circularfloatingactionmenu/library/FloatingActionMenu.java +++ b/library/src/main/java/com/oguzdev/circularfloatingactionmenu/library/FloatingActionMenu.java @@ -4,6 +4,7 @@ package com.oguzdev.circularfloatingactionmenu.library; import android.app.Activity; +import android.content.ClipData; import android.content.Context; import android.graphics.Path; import android.graphics.PathMeasure; @@ -88,15 +89,15 @@ public FloatingActionMenu(View mainActionView, // Find items with undefined sizes for(final Item item : subActionItems) { - if(item.width == 0 || item.height == 0) { - // Figure out the size by temporarily adding it to the Activity content view hierarchy - // and ask the size from the system - ((ViewGroup) getActivityContentView()).addView(item.view); - // Make item view invisible, just in case - item.view.setAlpha(0); - // Wait for the right time - item.view.post(new ItemViewQueueListener(item)); - } + if (item.width == 0 || item.height == 0) { + // Figure out the size by temporarily adding it to the Activity content view hierarchy + // and ask the size from the system + ((ViewGroup) getActivityContentView()).addView(item.view); + // Make item view invisible, just in case + item.view.setAlpha(0); + // Wait for the right time + item.view.post(new ItemViewQueueListener(item)); + } } } @@ -121,29 +122,35 @@ public void open(boolean animated) { // It is required that these Item views are not currently added to any parent // Because they are supposed to be added to the Activity content view, // just before the animation starts - if (subActionItems.get(i).view.getParent() != null) { - throw new RuntimeException("All of the sub action items have to be independent from a parent."); - } - // Initially, place all items right at the center of the main action view - // Because they are supposed to start animating from that point. - FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(subActionItems.get(i).width, subActionItems.get(i).height, Gravity.TOP | Gravity.LEFT); - params.setMargins(center.x - subActionItems.get(i).width / 2, center.y - subActionItems.get(i).height / 2, 0, 0); - // - ((ViewGroup) getActivityContentView()).addView(subActionItems.get(i).view, params); + Item item = subActionItems.get(i); + if (item.view.getParent() != null) { + throw new RuntimeException("All of the sub action items have to be independent from a parent."); + } + + // Initially, place all items right at the center of the main action view + // Because they are supposed to start animating from that point. + FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(item.width, item.height, Gravity.TOP | Gravity.LEFT); + params.setMargins(center.x - item.width / 2, center.y - item.height / 2, 0, 0); + // + ((ViewGroup) getActivityContentView()).addView(item.view, params); } + + getActivityContentView().invalidate(); + getActivityContentView().requestLayout(); // Tell the current MenuAnimationHandler to animate from the center animationHandler.animateMenuOpening(center); } else { // If animations are disabled, just place each of the items to their calculated destination positions. for (int i = 0; i < subActionItems.size(); i++) { - // This is currently done by giving them large margins - final FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(subActionItems.get(i).width, subActionItems.get(i).height, Gravity.TOP | Gravity.LEFT); - params.setMargins(subActionItems.get(i).x, subActionItems.get(i).y, 0, 0); - subActionItems.get(i).view.setLayoutParams(params); - // Because they are placed into the main content view of the Activity, - // which is itself a FrameLayout - ((ViewGroup) getActivityContentView()).addView(subActionItems.get(i).view, params); + Item item = subActionItems.get(i); + // This is currently done by giving them large margins + final FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(item.width, item.height, Gravity.TOP | Gravity.LEFT); + params.setMargins(item.x, item.y, 0, 0); + item.view.setLayoutParams(params); + // Because they are placed into the main content view of the Activity, + // which is itself a FrameLayout + ((ViewGroup) getActivityContentView()).addView(item.view, params); } } // do not forget to specify that the menu is open. @@ -170,7 +177,8 @@ public void close(boolean animated) { else { // If animations are disabled, just detach each of the Item views from the Activity content view. for (int i = 0; i < subActionItems.size(); i++) { - ((ViewGroup) getActivityContentView()).removeView(subActionItems.get(i).view); + Item item = subActionItems.get(i); + ((ViewGroup) getActivityContentView()).removeView(item.view); } } // do not forget to specify that the menu is now closed. @@ -215,9 +223,12 @@ public void updateItemPositions() { // Simply update layout params for each item for (int i = 0; i < subActionItems.size(); i++) { // This is currently done by giving them large margins - final FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(subActionItems.get(i).width, subActionItems.get(i).height, Gravity.TOP | Gravity.LEFT); - params.setMargins(subActionItems.get(i).x, subActionItems.get(i).y, 0, 0); - subActionItems.get(i).view.setLayoutParams(params); + Item item = subActionItems.get(i); + if (View.INVISIBLE != item.view.getVisibility()) { + final FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(item.width, item.height, Gravity.TOP | Gravity.LEFT); + params.setMargins(item.x, item.y, 0, 0); + item.view.setLayoutParams(params); + } } } @@ -265,21 +276,37 @@ private void calculateItemPositions() { // Prevent overlapping when it is a full circle int divisor; - if(Math.abs(endAngle - startAngle) >= 360 || subActionItems.size() <= 1) { - divisor = subActionItems.size(); + int size = getVisibleSubActionsCount(); + if(Math.abs(endAngle - startAngle) >= 360 || size <= 1) { + divisor = size; } else { - divisor = subActionItems.size() -1; + divisor = size -1; } + int visibleCounter = 0; // Measure this path, in order to find points that have the same distance between each other for(int i=0; i