Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/NewTabPageRecyclerView.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/NewTabPageRecyclerView.java b/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/NewTabPageRecyclerView.java |
| index f09d1c43398b0dfd220e95c5b0166a01496f2bd5..06401edbc9b0524b26953e38ae543c09c5f175b5 100644 |
| --- a/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/NewTabPageRecyclerView.java |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/NewTabPageRecyclerView.java |
| @@ -5,6 +5,7 @@ |
| package org.chromium.chrome.browser.ntp.cards; |
| import android.content.Context; |
| +import android.content.res.Resources; |
| import android.support.v7.widget.LinearLayoutManager; |
| import android.support.v7.widget.RecyclerView; |
| import android.util.AttributeSet; |
| @@ -14,13 +15,27 @@ |
| import android.view.inputmethod.EditorInfo; |
| import android.view.inputmethod.InputConnection; |
| +import org.chromium.chrome.R; |
| + |
| /** |
| * Simple wrapper on top of a RecyclerView that will acquire focus when tapped. Ensures the |
| * New Tab page receives focus when clicked. |
| */ |
| public class NewTabPageRecyclerView extends RecyclerView { |
| + /** |
| + * Minimum height of the bottom spacing item. |
| + */ |
|
mcwilliams
2016/05/17 15:04:59
I believe you can change these from /** to just //
|
| + private static final int MIN_BOTTOM_SPACING = 0; |
| + |
| + /** |
| + * Position in the adapter of the item we snap the scroll at, when switching between above and |
| + * below the fold. |
| + */ |
| + private static final int SNAP_ITEM_ADAPTER_POSITION = 1; |
| + |
| private GestureDetector mGestureDetector; |
| private LinearLayoutManager mLayoutManager; |
| + private int mToolbarHeight; |
|
Bernhard Bauer
2016/05/12 11:21:00
This can be final.
dgn
2016/05/13 13:49:58
Done.
|
| /** |
| * Constructor needed to inflate from XML. |
| @@ -39,6 +54,10 @@ public boolean onSingleTapUp(MotionEvent e) { |
| }); |
| mLayoutManager = new LinearLayoutManager(getContext()); |
| setLayoutManager(mLayoutManager); |
| + |
| + Resources res = context.getResources(); |
| + mToolbarHeight = res.getDimensionPixelSize(R.dimen.toolbar_height_no_shadow) |
| + + res.getDimensionPixelSize(R.dimen.toolbar_progress_bar_height); |
| } |
| public boolean isFirstItemVisible() { |
| @@ -78,4 +97,60 @@ public InputConnection onCreateInputConnection(EditorInfo outAttrs) { |
| public LinearLayoutManager getLinearLayoutManager() { |
| return mLayoutManager; |
| } |
| + |
| + /** |
| + * Returns the view associated to a given adapter position. This is to be used instead of |
| + * {@link #getChildAt}, which takes indices in terms of visible items. |
|
Bernhard Bauer
2016/05/12 11:21:00
Couldn't you just use `findViewHolderForAdapterPos
dgn
2016/05/13 13:49:58
Ah thanks, I missed it. I was searching under "get
|
| + */ |
| + private View getViewForAdapterPosition(int adapterPosition) { |
| + int viewPosition = adapterPosition - mLayoutManager.findFirstVisibleItemPosition(); |
| + return mLayoutManager.getChildAt(viewPosition); |
| + } |
| + |
| + /** |
| + * Calculates the height of the bottom spacing item, such that there is always enough content |
| + * below the fold. |
|
gone
2016/05/12 18:04:39
Isn't the point of the bottom spacing item that th
dgn
2016/05/13 13:49:58
Hum, yes, there isn't enough content so we have th
gone
2016/05/13 17:08:09
Acknowledged.
|
| + */ |
| + int calculateBottomSpacing() { |
| + int firstVisiblePos = mLayoutManager.findFirstVisibleItemPosition(); |
| + |
| + // We have enough items to fill the view, since the snap point item is not even visible. |
| + if (firstVisiblePos > SNAP_ITEM_ADAPTER_POSITION) return MIN_BOTTOM_SPACING; |
| + |
| + int fullHeight = getHeight() - mToolbarHeight; |
| + if (getAdapter().getItemViewType(SNAP_ITEM_ADAPTER_POSITION) |
| + == NewTabPageListItem.VIEW_TYPE_SPACING) { |
| + // No snippets, the spacing item should occupy the full height. |
|
gone
2016/05/12 18:04:39
... full height because the header isn't being dis
dgn
2016/05/13 13:49:58
Removed that code.
|
| + return fullHeight; |
|
gone
2016/05/12 18:04:39
Were there any concerns from UX about users accide
dgn
2016/05/13 13:49:58
Yes, it's going to be added for the MVP.
gone
2016/05/13 17:08:09
Er, guessing that's a Most Visited Page or somethi
dgn
2016/05/16 09:44:09
oh sorry, MVP here stands for Minimum Viable Produ
|
| + } |
| + |
| + int lastContentItemPosition = getAdapter().getItemCount() - 2; |
|
gone
2016/05/12 18:04:39
nit: Put a comment here about the spacing item bei
dgn
2016/05/13 13:49:58
Done.
|
| + int contentHeight = getViewForAdapterPosition(lastContentItemPosition).getBottom() |
| + - getViewForAdapterPosition(SNAP_ITEM_ADAPTER_POSITION).getTop(); |
| + int height = fullHeight - contentHeight; |
| + |
| + return Math.max(MIN_BOTTOM_SPACING, height); |
| + } |
| + |
| + /** |
| + * Updates the space added at the end of the list to make sure the above/below the fold |
| + * distinction can be preserved. |
| + * |
| + * This method relies on the adapter and the layout being in sync, so the call sequence while |
| + * removing items must be adjusted accordingly: either before the item is removed from both |
| + * the adapter and the recyclerview |
| + * ({@link android.support.v7.widget.helper.ItemTouchHelper.Callback#onSwiped}) or after it is |
| + * removed from both |
| + * ({@link android.support.v7.widget.helper.ItemTouchHelper.Callback#clearView}) |
| + */ |
| + public void refreshBottomSpacing() { |
| + View bottomSpacing = getViewForAdapterPosition(getAdapter().getItemCount() - 1); |
| + |
| + // It might not be in the layout yet if it's not visible or ready to be displayed. |
| + if (bottomSpacing == null) return; |
| + |
| + assert getChildViewHolder(bottomSpacing).getItemViewType() |
| + == NewTabPageListItem.VIEW_TYPE_SPACING; |
| + bottomSpacing.requestLayout(); |
| + } |
| } |