最近在做项目中遇到一个滑动需要改变状态栏的back图标
如果只是必全局那就简单了
GIF.gif
直接在Application引用的Style里加一句这个就可以了
可需求又不能改全部的,并且项目中用到的是 android.support.v7.widget.Toolbar在调一单个Activity布局也不行。设置样式也行不通。
后面就直接在代码里设置了
toolbar.setNavigationIcon(R.drawable.bt_title_back_selector);
然后就是监听滑动改变back icon了
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) {
if( state == State.EXPANDED ) {
//展开状态
toolbar.setNavigationIcon(R.drawable.bt_title_back_selector);
}else if(state == State.COLLAPSED){
//折叠状态
toolbar.setNavigationIcon(android.support.v7.appcompat.R.drawable.abc_ic_ab_back_material);
}else {
//中间状态
toolbar.setNavigationIcon(android.support.v7.appcompat.R.drawable.abc_ic_ab_back_material);
}
}
});
然后就是对android.support.design.widget.AppBarLayout这个控件设置监听了
因为android.support.design.widget.CollapsingToolbarLayout外层是 android.support.design.widget.AppBarLayout所以设置的监听是它了。然后在相应的监听里你可以为所欲为了。
上面代码只是记录,防以后工作中会使用到。