diff --git a/library/src/main/java/com/github/clans/fab/FloatingActionButton.java b/library/src/main/java/com/github/clans/fab/FloatingActionButton.java index 3482019..b582c7a 100755 --- a/library/src/main/java/com/github/clans/fab/FloatingActionButton.java +++ b/library/src/main/java/com/github/clans/fab/FloatingActionButton.java @@ -33,6 +33,7 @@ import android.view.ViewGroup; import android.view.ViewOutlineProvider; import android.view.ViewParent; +import android.view.ViewTreeObserver; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.ImageButton; @@ -98,6 +99,8 @@ public class FloatingActionButton extends ImageButton { private int mProgressMax = 100; private boolean mShowProgressBackground; + private boolean isLayoutInit = false; + public FloatingActionButton(Context context) { this(context, null); } @@ -165,6 +168,21 @@ private void init(Context context, AttributeSet attrs, int defStyleAttr) { // updateBackground(); setClickable(true); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { + this.getViewTreeObserver().addOnGlobalLayoutListener( + new ViewTreeObserver.OnGlobalLayoutListener() { + @TargetApi(Build.VERSION_CODES.JELLY_BEAN) + @Override + public void onGlobalLayout() { + isLayoutInit = true; + saveButtonOriginalPosition(); + getViewTreeObserver().removeOnGlobalLayoutListener(this); + } + }); + } else { + isLayoutInit = true; + } } private void initShowAnimation(TypedArray attr) { @@ -439,7 +457,7 @@ private void setBackgroundCompat(Drawable drawable) { } private void saveButtonOriginalPosition() { - if (!mButtonPositionSaved) { + if (!mButtonPositionSaved && isLayoutInit) { if (mOriginalX == -1) { mOriginalX = getX(); } @@ -448,6 +466,11 @@ private void saveButtonOriginalPosition() { mOriginalY = getY(); } + if (mProgressBarEnabled) { + mOriginalX = mOriginalX + mProgressWidth; + mOriginalY = mOriginalY + mProgressWidth; + } + mButtonPositionSaved = true; } } diff --git a/sample/src/main/java/com/github/clans/fab/sample/ProgressFragment.java b/sample/src/main/java/com/github/clans/fab/sample/ProgressFragment.java index 830646f..8ae3189 100644 --- a/sample/src/main/java/com/github/clans/fab/sample/ProgressFragment.java +++ b/sample/src/main/java/com/github/clans/fab/sample/ProgressFragment.java @@ -41,6 +41,7 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { } final FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab); +// fab.setIndeterminate(true); // To reproduce the bug fab.setMax(mMaxProgress); RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.my_recycler_view);