美文网首页Android 安卓技术分享
如何监听CollapsingToolbarLayout的展开与折

如何监听CollapsingToolbarLayout的展开与折

作者: ayvytr | 来源:发表于2018-09-03 10:13 被阅读0次

    自定义一个继承了 AppBarLayout.OnOffsetChangedListener的类

    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);
    }
    

    然后添加监听

    
    mAppBarLayout.addOnOffsetChangedListener(new AppBarStateChangeListener() {
                @Override
                public void onStateChanged(AppBarLayout appBarLayout, State state) {
                    Log.d("STATE", state.name());
                    if( state == State.EXPANDED ) {
                        
                        //展开状态
                        
                    }else if(state == State.COLLAPSED){
                        
                        //折叠状态
                         
                    }else {
                    
                        //中间状态
                    
                    }
                }
            });
    
    

    原文地址:https://blog.csdn.net/qq_21265915/article/details/55002056

    相关文章

      网友评论

        本文标题:如何监听CollapsingToolbarLayout的展开与折

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