Show view when toolbar collapses

后端 未结 2 950
时光取名叫无心
时光取名叫无心 2020-12-23 14:42

I have an activity with a CoordinatorLayout, AppBarLayout, CollapsingToolbarLayout and Toolbar. So, basically, a view tha

相关标签:
2条回答
  • 2020-12-23 15:23

    Taking a look at the CollapsingToolbarLayout source, the content scrim animations are triggered via an OnOffsetChangedListener on the AppBarLayout. So you could add another one to trigger alpha animations on your text view:

    mListener = new AppBarLayout.OnOffsetChangedListener() {
        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
            if(collapsingToolbar.getHeight() + verticalOffset < 2 * ViewCompat.getMinimumHeight(collapsingToolbar)) {
                hello.animate().alpha(1).setDuration(600);
            } else {
                hello.animate().alpha(0).setDuration(600);
            }
        }
    };
    
    appBar.addOnOffsetChangedListener(mListener);
    
    0 讨论(0)
  • 2020-12-23 15:36

    Instead of trying to replicate when the scrim is supposed to show (collapsed) or not (expanded), a better way is to override the setScrimsShown method, which is called every time inside the CollapsingToolbarLayout's onOffsetChanged, and add a listener to it like this:

    public class CollapsingToolbarLayoutWithScrimListener extends CollapsingToolbarLayout {
    
        public CollapsingToolbarLayoutWithScrimListener(Context context) {
            super(context);
        }
    
        public CollapsingToolbarLayoutWithScrimListener(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public CollapsingToolbarLayoutWithScrimListener(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context,attrs,defStyleAttr);
        }
    
        private ScrimListener scrimListener = null;
    
        @Override
        public void setScrimsShown(boolean shown, boolean animate) {
            super.setScrimsShown(shown, animate);
            if (scrimListener != null)
                scrimListener.onScrimShown(shown);
        }
    
        public void setScrimListener(ScrimListener listener) {
            scrimListener = listener;
        }
    
        public interface ScrimListener {
    
            void onScrimShown(boolean shown);
        }
    }
    
    0 讨论(0)
提交回复
热议问题