之前做项目的时候,发现如果在DrawerLayout布局中,如果要Remove掉这个View,然后重新Add,期间有做动画,就会发现在调用openDrawer会卡住一会再打开,找了很久然后发现 openDrawer有下面这个方法,注意
mFirstLayout
public void openDrawer(@NonNull View drawerView, boolean animate) { if (!isDrawerView(drawerView)) { throw new IllegalArgumentException("View " + drawerView + " is not a sliding drawer"); } final LayoutParams lp = (LayoutParams) drawerView.getLayoutParams(); if (mFirstLayout) { lp.onScreen = 1.f; lp.openState = LayoutParams.FLAG_IS_OPENED; updateChildrenImportantForAccessibility(drawerView, true); } else if (animate) { lp.openState |= LayoutParams.FLAG_IS_OPENING; if (checkDrawerViewAbsoluteGravity(drawerView, Gravity.LEFT)) { mLeftDragger.smoothSlideViewTo(drawerView, 0, drawerView.getTop()); } else { mRightDragger.smoothSlideViewTo(drawerView, getWidth() - drawerView.getWidth(), drawerView.getTop()); } } else { moveDrawerToOffset(drawerView, 1.f); updateDrawerState(lp.gravity, STATE_IDLE, drawerView); drawerView.setVisibility(VISIBLE); } invalidate(); }输出的时候发现在onDetachedFromWindow 和onAttachedToWindow的时候都会把这个参数置为true
@Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); mFirstLayout = true; } @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); mFirstLayout = true; }
在onLayout才会置为false,一般都会调用onLayout,但是重新add的时候有时没有调用onLayout,
所以我就手动调用就没问题了