From 17abc7482f5da8f104fb57c2e5d592636d3770c3 Mon Sep 17 00:00:00 2001 From: Fabian Kozynski Date: Fri, 14 May 2021 10:35:44 -0400 Subject: [PATCH] Use padding in pages instead of pageMargin ViewPager pageMargin does some rounding when calculating the offset of each page, leading to positions that are not integers at the snapping points. This is usually not an issue, except that we use those positions to drive the PageIndicator. Instead, set padding on the pages and negative margin in PagedTileLayout. As everything has clip false, this produces the exact same effect. Test: manual Fixes: 187954110 Change-Id: Ie1055a34c26214b012e96e3ec032ce4db576694d --- packages/SystemUI/res/layout/qs_paged_page.xml | 6 +++++- .../android/systemui/qs/PagedTileLayout.java | 18 ++++++++++++++++-- .../android/systemui/qs/QSContainerImpl.java | 2 +- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/packages/SystemUI/res/layout/qs_paged_page.xml b/packages/SystemUI/res/layout/qs_paged_page.xml index a4f0a0c85d411..98804fc80b50e 100644 --- a/packages/SystemUI/res/layout/qs_paged_page.xml +++ b/packages/SystemUI/res/layout/qs_paged_page.xml @@ -18,4 +18,8 @@ xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tile_page" android:layout_width="match_parent" - android:layout_height="match_parent"/> + android:layout_height="match_parent" + android:paddingStart="@dimen/notification_side_paddings" + android:paddingEnd="@dimen/notification_side_paddings" + android:clipChildren="false" + android:clipToPadding="false" /> diff --git a/packages/SystemUI/src/com/android/systemui/qs/PagedTileLayout.java b/packages/SystemUI/src/com/android/systemui/qs/PagedTileLayout.java index e0c8af6b89776..ce1066ee41c25 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/PagedTileLayout.java +++ b/packages/SystemUI/src/com/android/systemui/qs/PagedTileLayout.java @@ -80,8 +80,22 @@ public class PagedTileLayout extends ViewPager implements QSTileLayout { @Override public void setPageMargin(int marginPixels) { - if (marginPixels != getPageMargin()) { - super.setPageMargin(marginPixels); + // Using page margins creates some rounding issues that interfere with the correct position + // in the onPageChangedListener and therefore present bad positions to the PageIndicator. + // Instead, we use negative margins in the container and positive padding in the pages, + // matching the margin set from QSContainerImpl (note that new pages will always be inflated + // with the correct value. + // QSContainerImpl resources are set onAttachedView, so this view will always have the right + // values when attached. + MarginLayoutParams lp = (MarginLayoutParams) getLayoutParams(); + lp.setMarginStart(-marginPixels); + lp.setMarginEnd(-marginPixels); + setLayoutParams(lp); + + int nPages = mPages.size(); + for (int i = 0; i < nPages; i++) { + View v = mPages.get(i); + v.setPadding(marginPixels, v.getPaddingTop(), marginPixels, v.getPaddingBottom()); } } diff --git a/packages/SystemUI/src/com/android/systemui/qs/QSContainerImpl.java b/packages/SystemUI/src/com/android/systemui/qs/QSContainerImpl.java index cd97f976540d5..edfbed04f70da 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/QSContainerImpl.java +++ b/packages/SystemUI/src/com/android/systemui/qs/QSContainerImpl.java @@ -295,7 +295,7 @@ public class QSContainerImpl extends FrameLayout { qsPanelController.setContentMargins(mContentPadding, mContentPadding); // Set it as double the side margin (to simulate end margin of current page + // start margin of next page). - qsPanelController.setPageMargin(2 * mSideMargins); + qsPanelController.setPageMargin(mSideMargins); } else if (view == mHeader) { // No content padding for the header. } else {