【问题标题】:Android floating action button not returning to initial positionAndroid浮动操作按钮未返回初始位置
【发布时间】:2016-05-06 14:24:14
【问题描述】:

如果 FAB(浮动操作按钮)在快餐栏出现之前隐藏(在 CoordinatorLayout 中),那么下次我显示 FAB 时,它会在旧位置绘制(未向下移动到原始位置)。如果在小吃店消失时可以看到 FAB,那么一切都按预期工作。我错过了什么还是一个错误?

UPD:根据要求,这是一个最小示例。我将把最重要的部分放在这里,可以找到一个完整的工作示例on my github

这只是一个来自 Android 模板 Activity 项目的略微修改的示例。

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>

还有一个来自我的MainActivityOnCreate 方法(扩展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);
        }
    });
}

基本上,我只是确保在隐藏快餐栏时操作按钮保持隐藏状态。

如果您需要更多信息,请告诉我。

UPD2 如果你偶然发现了这个帖子并且不喜欢它 - 请告诉我我可以改进的地方。我真的很想知道如何解决这个问题。

UPD3 在 Android 错误跟踪器中创建的问题已合并到 this 中,应该会在下一个版本中修复

【问题讨论】:

  • 你能告诉我们你隐藏FAB的代码吗?也向我们展示您的布局。
  • @DanielNugent 你去。告诉我是否还有什么不清楚的地方。
  • @gj_ 我已经在我的问题的 UPD 部分中的一个最小示例中说明了布局

标签: android floating-action-button android-snackbar


【解决方案1】:

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

查看FloatingActionButton source,我看到他们基于 Snackbar 设置了 FAB 的 translateY,但 在 FAB 可见的情况下。如果 FAB 不可见,则不会更改 translateY,因此取决于它何时隐藏和显示,它可能最终卡在错误的位置。

我刚刚做的并且似乎有效的是,如果合适的话,我自己重置翻译。

将snackbar 声明为类级别变量,以便我们检查其状态。

Snackbar snack;

然后在需要时使用它来制作小吃吧:

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

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

        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 的正确 translateY 不是 0.0,您可能需要使用 fab.getTranslationY 来保存正确的值,并在需要重置它时使用它。

【讨论】:

  • 嗯。我不知道它是开源的。太棒了。但是,这种行为对我来说似乎很奇怪。我想知道如果没有看到 FAB,不更新职位的决定背后的原因是什么。无论如何,这解决了我的问题,即使这不是地球上最好的解决方案。谢谢!
  • 我已为此在 Android 问题跟踪器中提交了issue
【解决方案2】:

此问题已作为 Support Library 23.2.0 版本的一部分得到修复,related bug 现已修复。

【讨论】:

    猜你喜欢
    • 2016-05-05
    • 2016-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-26
    • 1970-01-01
    • 2015-11-08
    相关资源
    最近更新 更多