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

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/ntp/snippets/SnippetItemDecoration.java

Issue 1948223006: Set article spacing in dp, not px. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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/snippets/SnippetItemDecoration.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ntp/snippets/SnippetItemDecoration.java b/chrome/android/java/src/org/chromium/chrome/browser/ntp/snippets/SnippetItemDecoration.java
index e7590a127ebfbda10365ba73f848266fadee88a6..da0719b053036e8a39bf6d07e491c9fce4132696 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/ntp/snippets/SnippetItemDecoration.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/ntp/snippets/SnippetItemDecoration.java
@@ -4,25 +4,69 @@
package org.chromium.chrome.browser.ntp.snippets;
+import android.content.Context;
+import android.graphics.Canvas;
import android.graphics.Rect;
+import android.graphics.drawable.Drawable;
+import android.support.v4.content.res.ResourcesCompat;
import android.support.v7.widget.RecyclerView;
+import android.support.v7.widget.RecyclerView.Adapter;
import android.view.View;
+import org.chromium.chrome.R;
+import org.chromium.chrome.browser.ntp.cards.NewTabPageListItem;
+
/**
- * A class that decorates the RecyclerView elements.
+ * A decorator that places separators between RecyclerView items of type
+ * {@value NewTabPageListItem#VIEW_TYPE_SNIPPET}.
*/
public class SnippetItemDecoration extends RecyclerView.ItemDecoration {
- private static final int VERTICAL_SPACE = 1;
+ private final Drawable mSeparator;
+
+ public SnippetItemDecoration(Context context) {
+ mSeparator = ResourcesCompat
+ .getDrawable(context.getResources(), R.drawable.snippet_separator, null);
+ }
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
RecyclerView.State state) {
- // Reset the bounds of the outRect.
- outRect.setEmpty();
+ if (!shouldPlaceSeparator(view, parent)) return;
+
+ // Add space below the item to show the separator.
+ outRect.set(0, 0, 0, mSeparator.getIntrinsicHeight());
+ }
+
+ @Override
+ public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
+ final int left = parent.getPaddingLeft();
+ final int right = parent.getWidth() - parent.getPaddingRight();
- // Do not add a vertical space to the last item.
- if (parent.getChildAdapterPosition(view) != parent.getAdapter().getItemCount() - 1) {
- outRect.bottom = VERTICAL_SPACE;
+ // Draw the separators underneath the relevant items.
+ for (int i = 0; i < parent.getChildCount(); i++) {
+ View child = parent.getChildAt(i);
+ if (!shouldPlaceSeparator(child, parent)) continue;
+
+ RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
+ final int top = child.getBottom() + params.bottomMargin;
+ final int bottom = top + mSeparator.getIntrinsicHeight();
+
+ mSeparator.setBounds(left, top, right, bottom);
+ mSeparator.draw(c);
}
}
+
+ /**
+ * Checks whether there should be a separator below the given view in the RecyclerView.
+ * Current logic checks if the current view and next view show snippets.
+ */
+ private boolean shouldPlaceSeparator(View view, RecyclerView parent) {
+ int childPos = parent.getChildAdapterPosition(view);
+ Adapter adapter = parent.getAdapter();
+
+ if (childPos == adapter.getItemCount() - 1) return false;
+
+ return adapter.getItemViewType(childPos) == NewTabPageListItem.VIEW_TYPE_SNIPPET
+ && adapter.getItemViewType(childPos + 1) == NewTabPageListItem.VIEW_TYPE_SNIPPET;
+ }
}

Powered by Google App Engine
This is Rietveld 408576698