[英]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>
和我的MainActivity
的OnCreate
方法(扩展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错误跟踪器中创建的问题已合并到此错误中,应在下一个版本中修复
在我看到你的帖子之前我没有意识到我遇到了这个问题,但它也发生在我的应用程序中。
望着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.