Android CollapsingToolbarLayout折叠监听器 [英] Android CollapsingToolbarLayout collapse Listener
本文介绍了Android CollapsingToolbarLayout折叠监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将CollapsingToolBarLayout
与AppBarLayout
和CoordinatorLayout
一起使用,并且它们一起正常工作.我将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);
}
然后您可以使用它:
appBarLayout.addOnOffsetChangedListener(new AppBarStateChangeListener() {
@Override
public void onStateChanged(AppBarLayout appBarLayout, State state) {
Log.d("STATE", state.name());
}
});
这篇关于Android CollapsingToolbarLayout折叠监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文