美文网首页Andorid的好东西
监听CollapsingToolbarLayout的展开与折叠

监听CollapsingToolbarLayout的展开与折叠

作者: 白日梦__ | 来源:发表于2017-04-13 13:29 被阅读241次

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的展开与折叠

    本文链接:https://www.haomeiwen.com/subject/visbattx.html