Android floating action button not returning to initial position

放肆的年华 提交于 2019-12-01 08:52:28

I didn't realise I had this problem until I saw your post, but it happens in my app too.

Looking at the FloatingActionButton source I see that they set the translationY of the FAB based on the Snackbar, but only if the FAB is visible. If the FAB is not visible, the translationY is not changed and so depending on when it is hidden and shown it could end up stuck in the wrong position.

What I have just done, and seems to work, is to reset the translationY myself if appropriate.

Declare the snackbar as a class level variable so we can check on its status.

Snackbar snack;

Then use this to make your snackbar when you need it:

snack.make(...etc...).show();

Then when we are calling our fab.show() method we can check on the status of snack and reset translationY ourselves.

        if(snack != null){
            if(!snack.isShown()) {
                //if the snackbar is not shown, make sure the fab is at the
                //original location
                fab.setTranslationY(0.0f);
            }
        }
        fab.show();

If for whatever reason the correct translationY of the FAB is not 0.0 you may want to use fab.getTranslationY to save the correct value and use that when you need to reset it.

This has been fixed as part of the Support Library 23.2.0 release and the related bug is now fixed.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!