ViewPager + CoordinatorLayout 显示问题

使用 ViewPager + CoordinatorLayout 滑动显示多个页面时,可能会出现 CoordinatorLayout 的 Toolbar 被状态栏覆盖的问题. StackOverflow 上有一个类似的问题 CoordinatorLayout status bar padding disappears from ViewPager 2nd page. 需要在代码中对 ViewPager 设置 windowInsetsListener.

        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;
            }
        });

ViewPager 布局:




每个 pager 的布局:



    

        

            

            

                

                    
                
            
        
    

    

        
    

    

    

styles.xml (values-v19)

    

styles.xml (values-v21)

    

你可能感兴趣的:(ViewPager + CoordinatorLayout 显示问题)