Android CollapsingToolbarLayout 折叠监听器 [英] Android CollapsingToolbarLayout collapse Listener
问题描述
我将 CollapsingToolBarLayout
与 AppBarLayout
和 CoordinatorLayout
一起使用,它们完全正常工作.我将 Toolbar
设置为向上滚动时固定,我想知道是否有办法更改 Toolbar 的标题文本,当 CollapsingToolBarLayout
它已折叠.
I am using CollapsingToolBarLayout
alongside with AppBarLayout
and CoordinatorLayout
, and they are working Fine altogether. I set my Toolbar
to be fixed when I scroll up, I want to know if there is a way to change the title text of the Toolbar, when CollapsingToolBarLayout
it is collapsed.
总结一下,当滚动和展开时,我想要两个不同的标题.
Wrapping up, I want two different titles when scrolled and when expanded.
先谢谢大家
推荐答案
我分享了完整的实现,基于@Frodio Beggins 和@Nifhel 代码:
I share the full implementation, based on @Frodio Beggins and @Nifhel code:
public abstract class AppBarStateChangeListener implements AppBarLayout.OnOffsetChangedListener {
public enum State {
EXPANDED,
COLLAPSED,
IDLE
}
private State mCurrentState = State.IDLE;
@Override
public final void onOffsetChanged(AppBarLayout appBarLayout, int i) {
if (i == 0) {
if (mCurrentState != State.EXPANDED) {
onStateChanged(appBarLayout, State.EXPANDED);
}
mCurrentState = State.EXPANDED;
} else if (Math.abs(i) >= appBarLayout.getTotalScrollRange()) {
if (mCurrentState != State.COLLAPSED) {
onStateChanged(appBarLayout, State.COLLAPSED);
}
mCurrentState = State.COLLAPSED;
} else {
if (mCurrentState != State.IDLE) {
onStateChanged(appBarLayout, State.IDLE);
}
mCurrentState = State.IDLE;
}
}
public abstract void onStateChanged(AppBarLayout appBarLayout, State state);
}
然后你就可以使用它了:
And then you can use it:
appBarLayout.addOnOffsetChangedListener(new AppBarStateChangeListener() {
@Override
public void onStateChanged(AppBarLayout appBarLayout, State state) {
Log.d("STATE", state.name());
}
});
这篇关于Android CollapsingToolbarLayout 折叠监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!