How can i determine that CollapsingToolbar is collapsed?

后端 未结 5 1745
萌比男神i
萌比男神i 2020-12-04 16:26

I need to know when CollapsingToolbar from material design library is collapsed.

5条回答
  •  渐次进展
    2020-12-04 17:21

    Take a look over this github gist

    public class MyAppBarLayout extends AppBarLayout
            implements AppBarLayout.OnOffsetChangedListener {
    
        private State state;
        private OnStateChangeListener onStateChangeListener;
    
        public MyAppBarLayout(Context context) {
            super(context);
        }
    
        public MyAppBarLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        protected void onAttachedToWindow() {
            super.onAttachedToWindow();
            if (!(getLayoutParams() instanceof CoordinatorLayout.LayoutParams)
                    || !(getParent() instanceof CoordinatorLayout)) {
                throw new IllegalStateException(
                        "MyAppBarLayout must be a direct child of CoordinatorLayout.");
            }
            addOnOffsetChangedListener(this);
        }
    
        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
            if (verticalOffset == 0) {
                if (onStateChangeListener != null && state != State.EXPANDED) {
                    onStateChangeListener.onStateChange(State.EXPANDED);
                }
                state = State.EXPANDED;
            } else if (Math.abs(verticalOffset) >= 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;
            }
        }
    
        public void setOnStateChangeListener(OnStateChangeListener listener) {
            this.onStateChangeListener = listener;
        }
    
        public interface OnStateChangeListener {
            void onStateChange(State toolbarChange);
        }
    
        public enum State {
            COLLAPSED,
            EXPANDED,
            IDLE
        }
    }
    

提交回复
热议问题