Android CollapsingToolbarLayout折叠监听器
我正在使用CollapsingToolBarLayout
与AppBarLayout
和CoordinatorLayout
,他们一起工作很好。 当我向上滚动时,我的Toolbar
被固定,我想知道是否有方法来改变工具栏的标题文本,当CollapsingToolBarLayout
它被折叠。
总结起来,我需要两个不同的标题,当滚动和扩大 。
谢谢大家
我分享完整的实现,基于@Frodio Beggins和@Nifhel代码:
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()); } });
钩一个OnOffsetChangedListener
到你的AppBarLayout
。 当verticalOffset
达到0或leq时, Toolbar
高度表示CollapsingToolbarLayout已折叠,否则将展开或展开。
mAppBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() { @Override public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { if(verticalOffset == 0 || verticalOffset <= mToolbar.getHeight() && !mToolbar.getTitle().equals(mCollapsedTitle)){ mCollapsingToolbar.setTitle(mCollapsedTitle); }else if(!mToolbar.getTitle().equals(mExpandedTitle)){ mCollapsingToolbar.setTitle(mExpandedTitle); } } });
这个解决scheme完美的工作,我检测应用程序栏布局崩溃或扩大。
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() { @Override public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { if (Math.abs(verticalOffset)-appBarLayout.getTotalScrollRange() == 0) { // Collapsed } else { //Expanded } } });
在AppBarLayout上使用addOnOffsetChangedListener。
此代码为我工作
mAppBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() { @Override public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { if (verticalOffset == -mCollapsingToolbarLayout.getHeight() + mToolbar.getHeight()) { //toolbar is collapsed here //write your code here } } });
private enum State { EXPANDED, COLLAPSED, IDLE } private void initViews() { mAppBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() { private State state; @Override public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { if (verticalOffset == 0) { if (state != State.EXPANDED) { ... } state = State.EXPANDED; } else if (Math.abs(verticalOffset) >= appBarLayout.getTotalScrollRange()) { if (state != State.COLLAPSED) { ... } state = State.COLLAPSED; } else { if (state != State.IDLE) { ... } state = State.IDLE; } } }); }
您可以使用下面的代码获取collapsingToolBar alpha百分比:
appbarLayout.addOnOffsetChangedListener( new AppBarLayout.OnOffsetChangedListener() { @Override public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { float percentage = ((float)Math.abs(verticalOffset)/appBarLayout.getTotalScrollRange()); fadedView.setAlpha(percentage); });
参考: 链接
这个解决scheme正在为我工作:
@Override public void onOffsetChanged(AppBarLayout appBarLayout, int i) { if (i == 0) { if (onStateChangeListener != null && state != State.EXPANDED) { onStateChangeListener.onStateChange(State.EXPANDED); } state = State.EXPANDED; } else if (Math.abs(i) >= appBarLayout.getTotalScrollRange()) { if (onStateChangeListener != null && state != State.COLLAPSED) { onStateChangeListener.onStateChange(State.COLLAPSED); } state = State.COLLAPSED; } else { if (onStateChangeListener != null && state != State.IDLE) { onStateChangeListener.onStateChange(State.IDLE); } state = State.IDLE; } }
在AppBarLayout上使用addOnOffsetChangedListener。
如果你正在使用CollapsingToolBarLayout,你可以把这个
collapsingToolbar.setExpandedTitleColor(ContextCompat.getColor(activity, android.R.color.transparent)); collapsingToolbar.setTitle(title);
这段代码对我来说是完美的。 你可以使用百分比尺度你喜欢什么
@Override public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { double percentage = (double) Math.abs(verticalOffset) / collapsingToolbar.getHeight(); if (percentage > 0.8) { collapsingToolbar.setTitle("Collapsed"); } else { collapsingToolbar.setTitle("Expanded"); } }