【发布时间】:2016-06-23 01:57:16
【问题描述】:
考虑在工具栏中制作文本过渡/动画,如下面的 Twitter 应用程序所示:当用户名位于工具栏后面时,它会“弹出”到工具栏中。
或者,使文本滚动到工具栏后面并停留在那里。
【问题讨论】:
标签: android android-toolbar android-coordinatorlayout android-collapsingtoolbarlayout
考虑在工具栏中制作文本过渡/动画,如下面的 Twitter 应用程序所示:当用户名位于工具栏后面时,它会“弹出”到工具栏中。
或者,使文本滚动到工具栏后面并停留在那里。
【问题讨论】:
标签: android android-toolbar android-coordinatorlayout android-collapsingtoolbarlayout
您可以将 OnOffsetChangedListener 添加到 AppBarLayout 以确定 CollapsingToolbarLayout 何时折叠或展开并设置其标题。
final CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
collapsingToolbarLayout.setTitle(" ");
AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.app_bar_layout);
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
boolean isShow = false;
int scrollRange = -1;
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
if (scrollRange == -1) {
scrollRange = appBarLayout.getTotalScrollRange();
}
if (scrollRange + verticalOffset == 0) {
collapsingToolbarLayout.setTitle("Title");
isShow = true;
} else if(isShow) {
collapsingToolbarLayout.setTitle(" ");//carefull there should a space between double quote otherwise it wont work
isShow = false;
}
}
});
【讨论】: