【发布时间】:2016-10-02 01:06:54
【问题描述】:
TL;DR
我希望能够像在 Android Studio 中一样通过 Appbar 本身的手势来隐藏/显示 Appbar 内容
Scrolling Activity 模板。但是需要防止下面描述的奇怪效果。
详情
1.打开Android Studio 2.0,启动新的Android Studio项目,命名为ScrollingActivity,选择Phone and Tablet,API 9.选择Scrolling Activity模板。保留所有其余默认值。
2.在 Android Lollipop 5.1.1 上运行 Activity。就我而言,它是 MS VS 模拟器。
3.将文本滚动到最顶部,以便 Appbar 完全展开(并且 FAB 可见)。
4.注意现在您可以通过应用在 Appbar 本身上的手势滚动布局,展开到全高 180dp 或仅折叠到工具栏。
酷。我想在我的应用中使用它。
5.但是在 Appbar 上使用手势我已经设法使 所有布局,但 FAB 完全消失 :( 奇怪。 好吧,它会在一分钟内恢复,但我绝对不希望它出现在我的应用程序中。
为了获得更多细节,让我们在 Appbar 中添加一个监听器
// Track how AppBarLayout is flying
AppBarLayout appBarLayout = (AppBarLayout)findViewById(R.id.app_bar);
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
Log.d ("OffsetChangedListener", "appBarLayout goes to=" + verticalOffset );
}
});
在我的 XXHDPI 设备上,通常偏移量在 (-273, 0) 范围内。但是突然在Appbar上我的logcat显示了一些 那个Appbar去外太空长途旅行:
06-02 11:37:13.059 18116-18116/com.example.scrollingactivity D/OffsetChangedListener: appBarLayout goes to=-273
06-02 11:37:15.523 18116-18116/com.example.scrollingactivity D/OffsetChangedListener: appBarLayout goes to=-186
06-02 11:37:15.529 18116-18116/com.example.scrollingactivity D/OffsetChangedListener: appBarLayout goes to=311
06-02 11:37:15.563 18116-18116/com.example.scrollingactivity D/OffsetChangedListener: appBarLayout goes to=3833
06-02 11:37:15.564 18116-18116/com.example.scrollingactivity D/OffsetChangedListener: appBarLayout goes to=0
06-02 11:37:15.638 18116-18116/com.example.scrollingactivity D/OffsetChangedListener: appBarLayout goes to=11594
06-02 11:37:15.690 18116-18116/com.example.scrollingactivity D/OffsetChangedListener: appBarLayout goes to=16968
06-02 11:37:15.710 18116-18116/com.example.scrollingactivity D/OffsetChangedListener: appBarLayout goes to=19137
...
06-02 11:38:07.242 18116-18116/com.example.scrollingactivity D/OffsetChangedListener: appBarLayout goes to=2684733
06-02 11:38:07.343 18116-18116/com.example.scrollingactivity D/OffsetChangedListener: appBarLayout goes to=-536483
...
06-02 11:38:29.983 18116-18116/com.example.scrollingactivity D/OffsetChangedListener: appBarLayout goes to=-796
06-02 11:38:30.066 18116-18116/com.example.scrollingactivity D/OffsetChangedListener: appBarLayout goes to=-563
06-02 11:38:30.125 18116-18116/com.example.scrollingactivity D/OffsetChangedListener: appBarLayout goes to=-422
06-02 11:38:30.202 18116-18116/com.example.scrollingactivity D/OffsetChangedListener: appBarLayout goes to=-268
06-02 11:38:30.253 18116-18116/com.example.scrollingactivity D/OffsetChangedListener: appBarLayout goes to=-185
06-02 11:38:30.385 18116-18116/com.example.scrollingactivity D/OffsetChangedListener: appBarLayout goes to=-41
06-02 11:38:30.587 18116-18116/com.example.scrollingactivity D/OffsetChangedListener: appBarLayout goes to=0
06-02 11:38:30.588 18116-18116/com.example.scrollingactivity D/OffsetChangedListener: appBarLayout goes to=0
如何防止这种行为?
【问题讨论】:
标签: android android-studio android-coordinatorlayout