监听CollapsingToolbarLayout的展开与折叠

http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2016/0619/4362.html

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 ) {
                    //展开状态
                    tvTitle.setVisibility(View.GONE);
                    ivBack.setColorFilter(Color.WHITE);
                }else if(state == State.COLLAPSED){
                    //折叠状态
                    tvTitle.setVisibility(View.VISIBLE);
                    ivBack.setColorFilter(null);
                }else {
                    //中间状态

                }
            }
        });

你可能感兴趣的:(监听CollapsingToolbarLayout的展开与折叠)