Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(112)

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/PaddingListItem.java

Issue 1928063002: [NTP Snippets] Fill space below the last snippet if necessary (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/PaddingListItem.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/PaddingListItem.java b/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/PaddingListItem.java
new file mode 100644
index 0000000000000000000000000000000000000000..5485837a786f83632b3a6aad7847d2ac2df39eaf
--- /dev/null
+++ b/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/PaddingListItem.java
@@ -0,0 +1,91 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.chrome.browser.ntp.cards;
+
+import android.content.Context;
+import android.content.res.Resources;
+import android.support.v7.widget.LinearLayoutManager;
+import android.view.View;
+import android.view.ViewGroup;
+
+import org.chromium.base.Log;
+import org.chromium.chrome.R;
+
+/**
+ * Placeholder item to let the snippets flow to the top of the scroll list even when it does not
+ * contain enough of them. It is displayed as a dummy item with variable height that just occupies
+ * the remaining space between the last item in the RecyclerView and the bottom of the screen.
+ */
+public class PaddingListItem implements NewTabPageListItem {
mcwilliams 2016/05/10 12:42:52 Possibly rename this to SpacingListItem. You might
dgn 2016/05/10 14:26:12 Done.
+ private static final String TAG = "Ntp";
+
+ private static class PaddingListItemView extends View {
+ private final int mMinHeight;
+ private final int mToolbarHeight;
+
+ public PaddingListItemView(Context context) {
+ super(context);
+ Resources res = context.getResources();
+ mMinHeight = res.getDimensionPixelSize(R.dimen.snippets_bottom_padding);
mcwilliams 2016/05/10 12:42:52 Remove this
dgn 2016/05/10 14:26:12 Done.
+ mToolbarHeight = res.getDimensionPixelSize(R.dimen.toolbar_height_no_shadow)
+ + res.getDimensionPixelSize(R.dimen.toolbar_progress_bar_height);
+ }
+
+ @Override
+ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
+ Log.d(TAG, "onMeasure");
May 2016/05/10 10:31:51 Remove
dgn 2016/05/10 14:26:12 Done.
+ setMeasuredDimension(0, calculateHeight());
+ }
+
+ private int calculateHeight() {
+ // An item has been dismissed, ending up calling this method. But the PaddingListItem
+ // is currently not visible and not in the recycler view, so we don't care.
+ if (getParent() == null) return mMinHeight;
+
+ if (!(getParent() instanceof NewTabPageRecyclerView)) {
+ Log.wtf(TAG, "PaddingListItem not in recyclerview: parent=%s", getParent());
+ return mMinHeight;
+ }
+
+ NewTabPageRecyclerView rv = (NewTabPageRecyclerView) getParent();
mcwilliams 2016/05/10 12:42:52 avoid using abbreviations like rv and lm - it make
dgn 2016/05/10 14:26:12 Done.
+ LinearLayoutManager lm = ((LinearLayoutManager) rv.getLayoutManager());
+ int firstPos = lm.findFirstVisibleItemPosition();
May 2016/05/10 10:31:51 As discussed, please clarify what's being done her
mcwilliams 2016/05/10 12:42:52 What is firstPos?
dgn 2016/05/10 14:26:12 Is the current state enough or should I comment so
dgn 2016/05/10 14:26:12 Done.
+ if (firstPos > 1) {
+ Log.d(TAG, "Too many items (first at %d), using min height (%d)", firstPos,
+ mMinHeight);
+ return mMinHeight;
+ }
+
+ int fullHeight = rv.getHeight() - mToolbarHeight;
+ if (rv.getAdapter().getItemViewType(1) == NewTabPageListItem.VIEW_TYPE_PADDING) {
May 2016/05/10 10:31:51 Ditto.
dgn 2016/05/10 14:26:12 Done.
+ Log.d(TAG, "No item, we just use the full height (%d)", fullHeight);
+ return fullHeight;
+ }
+
+ // View position of the item that should be aligned with the top of the screen
+ // It's the item at position 1 since the above-the-fold one is at 0.
+ int snapItemPos = 1 - firstPos;
+ // Position of the last item before the padding item.
+ int prevViewPos = rv.getChildViewHolder(this).getAdapterPosition() - snapItemPos;
+
+ int contentHeight =
+ rv.getChildAt(prevViewPos).getBottom() - rv.getChildAt(snapItemPos).getTop();
+ int height = fullHeight - contentHeight
+ - getResources().getDimensionPixelSize(R.dimen.tab_strip_height);
+
+ return Math.max(mMinHeight, height);
+ }
+ }
+
+ /** Creates the View object for displaying the variable padding. */
+ public static View createView(ViewGroup parent) {
+ return new PaddingListItemView(parent.getContext());
+ }
+
+ @Override
+ public int getType() {
+ return NewTabPageListItem.VIEW_TYPE_PADDING;
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698