Android浮动操作按钮未返回初始位置 [英] Android floating action button not returning to initial position

查看:91
本文介绍了Android浮动操作按钮未返回初始位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果在小吃栏出现之前(在CoordinatorLayout中)FAB(浮动操作按钮)隐藏,那么下次我显示FAB时,它将绘制在旧位置(而不是向下移动到原始位置).如果在小吃店消失时可以看到FAB,则说明一切正常.我错过了什么吗?还是有bug?

If the FAB (Floating Action Button) hides before a snackbar appears (in CoordinatorLayout) then the next time I show the FAB it is drawn in the old position (not moved down to the original position). If the FAB is visible when the snackbar dissapears, then everything is working as expected. Did I miss something or is it a bug?

UPD::根据要求,这是一个最小的示例.我将在这里放置最重要的部分,并在我的github上找到一个完整的示例

UPD: As requested, here is a minimal example. I will put most important bits here, and a fully working example can be found on my github

这只是Android模板活动"项目中经过稍微修改的示例.

This is just a very slightly modified example from Android template Activity project.

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="coordination.fab.snackbar.test.testfabsnackbar.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

还有我的MainActivity中的OnCreate方法(扩展了AppCompatActivity):

And an OnCreate method from my MainActivity (extends AppCompatActivity):

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            fab.hide();
            Snackbar.make(view, "blah", Snackbar.LENGTH_SHORT).show();
            new Timer().schedule(new TimerTask() {
                @Override
                public void run() { fab.show(); }
            }, 3000);
        }
    });
}

本质上,我只是确保在隐藏小吃栏时,动作按钮保持隐藏状态.

Essentially, I just ensure that the action button stays hidden when the snackbar get hidden.

请告诉我您是否需要更多信息.

Please tell me if you need more info on anything.

UPD2 如果您偶然发现了该线程而又不喜欢它-请告诉我可以改进的地方.我真的很想知道如何解决这个问题.

UPD2 If you stumble upon this thread and don't like it - please tell me what I can improve. I really would like to know how to overcome the issue.

UPD3 在Android错误跟踪器中创建的问题已合并到

UPD3 The issue created in Android bug tracker is merged into this one and should be fixed in the next build

推荐答案

直到看到您的帖子,我才意识到自己遇到了这个问题,但它也发生在我的应用程序中.

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();

然后,当我们调用fab.show()方法时,我们可以检查snack的状态并自行重置translationY.

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();

如果出于某种原因,FAB的正确翻译Y不是0.0,则可能要使用fab.getTranslationY保存正确的值,并在需要重置时使用它.

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.

这篇关于Android浮动操作按钮未返回初始位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆