美文网首页
修复CollapsingToolbarLayout,当FitsS

修复CollapsingToolbarLayout,当FitsS

作者: Kingsley_Wu | 来源:发表于2020-07-15 16:08 被阅读0次
    分为:
     android.support.design.widget.CollapsingToolbarLayout;  // 需反射 mLastInsets
     com.google.android.material.appbar.CollapsingToolbarLayout;  // 需反射 lastInsets
    
    public class FixCollapsingToolbarLayout extends CollapsingToolbarLayout {
    
        public FixCollapsingToolbarLayout(Context context) {
            super(context);
        }
    
        public FixCollapsingToolbarLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public FixCollapsingToolbarLayout(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            try {
                // android.support.design.widget.CollapsingToolbarLayout;  // 需反射 mLastInsets
                // com.google.android.material.appbar.CollapsingToolbarLayout;  // 需反射 lastInsets
                Field fs = CollapsingToolbarLayout.class..getDeclaredField("mLastInsets");
                、、Field fs = this.getClass().getSuperclass().getDeclaredField("mLastInsets");
                fs.setAccessible(true);
                WindowInsetsCompat mLastInsets = (WindowInsetsCompat) fs.get(this);
                final int mode = MeasureSpec.getMode(heightMeasureSpec);
                int topInset = mLastInsets != null ? mLastInsets.getSystemWindowInsetTop() : 0;
                if (mode == MeasureSpec.UNSPECIFIED && topInset > 0) {
                    // fix the bottom empty padding
                    heightMeasureSpec = MeasureSpec.makeMeasureSpec(
                            getMeasuredHeight() - topInset, MeasureSpec.EXACTLY);
                    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
                }
            } catch (Exception e) {
                LogUtils.e(e);
            }
        }
    }
    
    或继承  android.support.design.widget.CollapsingToolbarLayout 时,使用如下:
    
    public class FixCollapsingToolbarLayout extends CollapsingToolbarLayout {
        private boolean mLayoutReady;
        public FixCollapsingToolbarLayout(Context context) {
            super(context);
        }
    
        public FixCollapsingToolbarLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public FixCollapsingToolbarLayout(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
            if (!mLayoutReady) {
                if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
                    if ((getWindowSystemUiVisibility() &
                            (SYSTEM_UI_FLAG_LAYOUT_STABLE|SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)) ==
                            (SYSTEM_UI_FLAG_LAYOUT_STABLE|SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)) {
                        try {
                            Field mLastInsets = CollapsingToolbarLayout.class.getDeclaredField("mLastInsets");
                            mLastInsets.setAccessible(true);
                            mLastInsets.set(this, null);
                        } catch (NoSuchFieldException e) {
                            e.printStackTrace();
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                    }
                }
                mLayoutReady = true;
            }
            super.onLayout(changed, left, top, right, bottom);
        }
    }
    

    相关文章

      网友评论

          本文标题:修复CollapsingToolbarLayout,当FitsS

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