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

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..ced483f6ea581d577953aa93b47b5ff37cf08981 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,82 @@
package org.chromium.chrome.browser.ntp.snippets;
+import android.content.Context;
+import android.graphics.Canvas;
+import android.graphics.Paint;
import android.graphics.Rect;
+import android.support.v4.content.ContextCompat;
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 int mVerticalSpace;
+ private final Paint mPaint = new Paint();
+
+ public SnippetItemDecoration(Context context) {
+ mVerticalSpace =
+ context.getResources().getDimensionPixelSize(R.dimen.snippets_vertical_spacing);
+ mPaint.setColor(ContextCompat.getColor(context, R.color.snippets_separator));
+ }
+ /**
+ * Add space between items to show the separator.
Bernhard Bauer 2016/05/09 11:21:56 Nit: I would make this an implementation (non-Java
PEConn 2016/05/09 13:33:38 Done.
+ */
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
RecyclerView.State state) {
- // Reset the bounds of the outRect.
- outRect.setEmpty();
- // Do not add a vertical space to the last item.
- if (parent.getChildAdapterPosition(view) != parent.getAdapter().getItemCount() - 1) {
- outRect.bottom = VERTICAL_SPACE;
+ if (shouldPlaceSeparator(view, parent)) {
+ // Reset the bounds of the outRect.
+ outRect.setEmpty();
+ outRect.bottom = mVerticalSpace;
}
}
+
+ /**
+ * Draw the separators as simple rectangles underneath relevant items.
+ */
+ @Override
+ public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
+ final int left = parent.getPaddingLeft();
May 2016/05/09 11:47:53 Can you just create this as a drawable shape xml o
PEConn 2016/05/09 13:33:38 Done.
+ final int right = parent.getWidth() - parent.getPaddingRight();
+
+ for (int i = 0; i < parent.getChildCount(); i++) {
+ View child = parent.getChildAt(i);
+ if (shouldPlaceSeparator(child, parent)) {
+ final RecyclerView.LayoutParams params =
+ (RecyclerView.LayoutParams) child.getLayoutParams();
+
+ final int top = child.getBottom() + params.bottomMargin;
+ final int bottom = top + mVerticalSpace;
+
+ c.drawRect(left, top, right, bottom, mPaint);
+ }
+ }
+ }
+
+ /**
+ * 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 articles.
+ */
+ private boolean shouldPlaceSeparator(View view, RecyclerView parent) {
+ int childPos = parent.getChildAdapterPosition(view);
+ Adapter adapter = parent.getAdapter();
+
+ // Ensure we are not the last child.
+ 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