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

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

Issue 2158413004: Make NewTabPageAdapter the only source of truth for adapter item positions. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase. Created 4 years, 5 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
« no previous file with comments | « chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/NewTabPageAdapter.java ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 e1f115529eef6b423a5f764642f925d749b141f0..1062092ced059507398140a6faaf6db248e2b3e0 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
@@ -8,6 +8,7 @@
import android.content.res.Resources;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
+import android.support.v7.widget.RecyclerView.ViewHolder;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
@@ -26,13 +27,6 @@
public class NewTabPageRecyclerView extends RecyclerView {
private static final String TAG = "NtpCards";
- /**
- * Positions of key items in the RecyclerView.
- */
- private static final int ABOVE_THE_FOLD_ITEM_POSITION = 0;
- private static final int ARTICLES_HEADER_ITEM_POSITION = 1;
- private static final int FIRST_CARD_ITEM_POSITION = 2;
-
private final GestureDetector mGestureDetector;
private final LinearLayoutManager mLayoutManager;
private final int mToolbarHeight;
@@ -118,10 +112,6 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
}
- public LinearLayoutManager getLinearLayoutManager() {
- return mLayoutManager;
- }
-
public void setAboveTheFoldView(View aboveTheFoldView) {
mAboveTheFoldView = aboveTheFoldView;
}
@@ -140,8 +130,7 @@ public void showCardsFrom(int cardCurrentScroll) {
* distinction can be preserved.
*/
public void refreshBottomSpacing() {
- ViewHolder bottomSpacingViewHolder =
- findViewHolderForAdapterPosition(getAdapter().getItemCount() - 1);
+ ViewHolder bottomSpacingViewHolder = findBottomSpacer();
// It might not be in the layout yet if it's not visible or ready to be displayed.
if (bottomSpacingViewHolder == null) return;
@@ -158,16 +147,15 @@ 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 > ARTICLES_HEADER_ITEM_POSITION) return mMinBottomSpacing;
-
- // The spacing item is the last item, the last content item is directly above that.
- int lastContentItemPosition = getAdapter().getItemCount() - 2;
+ if (firstVisiblePos > getNewTabPageAdapter().getHeaderPosition()) {
+ return mMinBottomSpacing;
+ }
- ViewHolder lastContentItem = findViewHolderForAdapterPosition(lastContentItemPosition);
- ViewHolder snapItem = findViewHolderForAdapterPosition(ARTICLES_HEADER_ITEM_POSITION);
+ ViewHolder lastCard = findLastCard();
+ ViewHolder firstHeader = findHeader();
int bottomSpacing = getHeight() - mToolbarHeight;
- if (lastContentItem == null || snapItem == null) {
+ if (lastCard == null || firstHeader == null) {
// This can happen in several cases, where some elements are not visible and the
// RecyclerView didn't already attach them. We handle it by just adding space to make
// sure that we never run out and force the UI to jump around and get stuck in a
@@ -178,13 +166,13 @@ int calculateBottomSpacing() {
// - Dismissing a snippet and having the status card coming to take its place.
// - Refresh while being below the fold, for example by tapping the status card.
- if (snapItem != null) bottomSpacing -= snapItem.itemView.getTop();
+ if (firstHeader != null) bottomSpacing -= firstHeader.itemView.getTop();
Log.w(TAG, "The RecyclerView items are not attached, can't determine the content "
+ "height: snap=%s, last=%s. Using full height: %d ",
- snapItem, lastContentItem, bottomSpacing);
+ firstHeader, lastCard, bottomSpacing);
} else {
- int contentHeight = lastContentItem.itemView.getBottom() - snapItem.itemView.getTop();
+ int contentHeight = lastCard.itemView.getBottom() - firstHeader.itemView.getTop();
bottomSpacing -= contentHeight - mCompensationHeight;
}
@@ -204,23 +192,11 @@ public void updatePeekingCard() {
}
/**
- * Finds the first card in this RecyclerView.
- * @return The viewholder for the first card or null if no card is available.
- */
- private CardViewHolder findFirstCard() {
- int firstCardIndex = FIRST_CARD_ITEM_POSITION;
- ViewHolder viewHolder = findViewHolderForAdapterPosition(firstCardIndex);
- if (!(viewHolder instanceof CardViewHolder)) return null;
-
- return (CardViewHolder) viewHolder;
- }
-
- /**
* Show the snippets header when the user scrolls down and snippet articles starts reaching the
* top of the screen.
*/
public void updateSnippetsHeaderDisplay() {
- SnippetHeaderViewHolder header = findHeaderView();
+ SnippetHeaderViewHolder header = findHeader();
if (header == null) return;
// Start doing the calculations if the snippet header is currently shown on screen.
@@ -230,18 +206,54 @@ public void updateSnippetsHeaderDisplay() {
refreshBottomSpacing();
}
+ private NewTabPageAdapter getNewTabPageAdapter() {
+ return (NewTabPageAdapter) getAdapter();
+ }
+
/**
- * Finds the header view.
- * @return The ViewHolder of the header or null, if it is not present.
+ * Finds the view holder for the first header.
+ * @return The {@link ViewHolder} of the header, or null if it is not present.
*/
- private SnippetHeaderViewHolder findHeaderView() {
- // Get the snippet header view. It is always at position 1
- ViewHolder viewHolder = findViewHolderForAdapterPosition(ARTICLES_HEADER_ITEM_POSITION);
+ private SnippetHeaderViewHolder findHeader() {
+ ViewHolder viewHolder =
+ findViewHolderForAdapterPosition(getNewTabPageAdapter().getHeaderPosition());
if (!(viewHolder instanceof SnippetHeaderViewHolder)) return null;
return (SnippetHeaderViewHolder) viewHolder;
}
+ /**
+ * Finds the view holder for the first card.
+ * @return The {@link ViewHolder} for the first card, or null if it is not present.
+ */
+ private CardViewHolder findFirstCard() {
+ ViewHolder viewHolder =
+ findViewHolderForAdapterPosition(getNewTabPageAdapter().getFirstCardPosition());
+ if (!(viewHolder instanceof CardViewHolder)) return null;
+
+ return (CardViewHolder) viewHolder;
+ }
+
+ /**
+ * Finds the view holder for the last card.
+ * @return The {@link ViewHolder} of the last card, or null if it is not present.
+ */
+ private CardViewHolder findLastCard() {
+ ViewHolder viewHolder =
+ findViewHolderForAdapterPosition(getNewTabPageAdapter().getLastCardPosition());
+ if (!(viewHolder instanceof CardViewHolder)) return null;
+
+ return (CardViewHolder) viewHolder;
+ }
+
+ /**
+ * Finds the view holder for the bottom spacer.
+ * @return The {@link ViewHolder} of the bottom spacer, or null if it is not present.
+ */
+ private ViewHolder findBottomSpacer() {
+ return findViewHolderForAdapterPosition(getNewTabPageAdapter().getBottomSpacerPosition());
+ }
+
/** Called when an item is in the process of being removed from the view. */
void onItemDismissStarted(View itemView) {
mCompensationHeight += itemView.getHeight();
@@ -302,7 +314,7 @@ public void snapScroll(View fakeBox, int parentScrollY, int parentHeight) {
if (!peekingCardViewHolder.canPeek()) return;
View peekingCardView = findFirstCard().itemView;
- View headerView = findHeaderView().itemView;
+ View headerView = findHeader().itemView;
final int peekingHeight = getResources().getDimensionPixelSize(
R.dimen.snippets_padding_and_peeking_card_height);
« no previous file with comments | « chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/NewTabPageAdapter.java ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698