CoordinatorLayout status bar padding disappears from ViewPager 2nd page

自作多情 提交于 2019-11-30 00:11:10

Solution proposed by Android Team in the answer of my reported defect. This code should finally work.

ViewPager mViewPager;

ViewCompat.setOnApplyWindowInsetsListener(mViewPager,
        new OnApplyWindowInsetsListener() {
    @Override
    public WindowInsetsCompat onApplyWindowInsets(View v,
            WindowInsetsCompat insets) {
        insets = ViewCompat.onApplyWindowInsets(v, insets);
        if (insets.isConsumed()) {
            return insets;
        }

        boolean consumed = false;
        for (int i = 0, count = mViewPager.getChildCount(); i <  count; i++) {
            ViewCompat.dispatchApplyWindowInsets(mViewPager.getChildAt(i), insets);
            if (insets.isConsumed()) {
                consumed = true;
            }
        }
        return consumed ? insets.consumeSystemWindowInsets() : insets;
    }
});

Like sidecarcat, I ran into a similar issue (using v23.1.1). I post here a workaround by using the code of sidecarcat and add some code to remove superflous padding in some cases.

    // in onCreateView: adjust toolbar padding
    final int initialToolbarHeight = mToolbar.getLayoutParams().height;
    final int initialStatusBarHeight = getStatusBarHeight();
    mToolbar.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            int[] locToolbar = new int[2];
            mToolbar.getLocationOnScreen(locToolbar);
            int yToolbar = locToolbar[1];
            int topPaddingToolbar = mToolbar.getPaddingTop();
            if (isAdded()) {
                //normal case : system status bar padding on toolbar : yToolbar = initialStatusBarHeight && topPaddingToolbar = 0
                //abnormal case : no system status bar padding on toolbar -> toolbar behind status bar => add custom padding
                if (yToolbar != initialStatusBarHeight && topPaddingToolbar == 0) {
                    mToolbar.setPadding(0, initialStatusBarHeight, 0, 0);
                    mToolbar.getLayoutParams().height = initialToolbarHeight + initialStatusBarHeight;
                }
                //abnormal case : system status bar padding and custom padding on toolbar -> toolbar with padding too large => remove custom padding
                else if (yToolbar == initialStatusBarHeight && topPaddingToolbar == initialStatusBarHeight) {
                    mToolbar.setPadding(0, 0, 0, 0);
                    mToolbar.getLayoutParams().height = initialToolbarHeight;
                }
            }
        }
    });

    return mRootView;
}

public int getStatusBarHeight() {
    int result = 0;
    int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
    if (resourceId > 0) {
        result = getResources().getDimensionPixelSize(resourceId);
    }
    return result;
}

I ran into a similar issue (using v23.0.1). In my case, the problem occurs on the first page as well. The workaround I settled on was to adjust the padding and height of the toolbar when the fragment is created.

    // in onCreateView: adjust toolbar padding
    Toolbar toolbar = (Toolbar) mRootView.findViewById(R.id.toolbar);
    toolbar.setPadding(0, getStatusBarHeight(), 0, 0);
    toolbar.getLayoutParams().height = toolbar.getLayoutParams().height + getStatusBarHeight();

    return mRootView;
}

public int getStatusBarHeight() {
    int result = 0;
    int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
    if (resourceId > 0) {
        result = getResources().getDimensionPixelSize(resourceId);
    }
    return result;
}

Seems the solution for this is simple

You have

android:fitsSystemWindows="true"

In the fragment CoordinateLayoutView

remove it from here and put in the Activity's root view.

This should now all work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!