簡體   English   中英

Android浮動操作按鈕未返回初始位置

[英]Android floating action button not returning to initial position

如果FAB(浮動操作按鈕)隱藏在小吃欄出現之前(在CoordinatorLayout中),那么下次我顯示FAB時,它將被繪制在舊位置(不會向下移動到原始位置)。 如果在小吃店消失時可以看到FAB,那么一切都按預期工作。 我錯過了什么或者它是一個錯誤嗎?

UPD:根據要求,這是一個最小的例子。 我將把最重要的部分放在這里, 在我的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錯誤跟蹤器中創建的問題已合並到此錯誤中,應在下一個版本中修復

這已作為支持庫23.2.0發行版的一部分修復 ,現在已修復相關錯誤

在我看到你的帖子之前我沒有意識到我遇到了這個問題,但它也發生在我的應用程序中。

望着FloatingActionButton源我看到他們設置了FAB的基礎上,小吃吧的translationY,但前提是FAB是可見的。 如果FAB不可見,則translationY不會改變,因此取決於它何時被隱藏並顯示它最終會卡在錯誤的位置。

我剛剛完成並且似乎工作的是,如果合適的話,我自己重置翻譯。

將快餐欄聲明為類級變量,以便我們檢查其狀態。

Snackbar snack;

然后在需要時使用它來制作小吃吧:

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

然后,當我們調用fab.show()方法時,我們可以檢查snack的狀態並fab.show()重置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的正確translationY不是0.0,您可能希望使用fab.getTranslationY來保存正確的值,並在需要重置它時使用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM