【问题标题】:Android: Autocomplete animation for CollapsingToolbarLayoutAndroid:CollapsingToolbarLayout 的自动完成动画
【发布时间】:2015-10-14 19:59:41
【问题描述】:
我正在使用来自Android Design Support Library 的CollapsingToolbarLayout 在活动中为工具栏设置动画。问题是,如果我在动画中间抬起手指,工具栏不会继续动画。它只是在屏幕上显示一半。我该如何解决?
这是一个 gif:http://goo.gl/GQGQqe
感谢您的回答。
【问题讨论】:
标签:
android
animation
toolbar
android-collapsingtoolbarlayout
【解决方案1】:
据我了解,您正在经历的行为是预期的。但是,有一个setExpanded() 方法允许您以编程方式关闭/打开AppBarLayout,并可以选择为进程设置动画。因此,您可以监听MotionEvent.ACTION_UP 事件并根据AppBar 从其原始位置移动的距离启动动画以打开或关闭它。
编辑:
这是我刚刚在运行 KitKat 的模拟器上测试的一个实现:
public class MainActivity extends AppCompatActivity {
//...
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_UP) {
if (appBarLayout.getY() != 0) {
//User has just finished interacting with the screen
//and the AppBar is not fully expanded. So, to close
//it with an animation...
appBarLayout.setExpanded(false, true);
}
}
return super.dispatchTouchEvent(ev);
}
//...
}