Bug 1379021 - Draw a horizontal divider between highlight items. r?mcomella draft
authorSebastian Kaspari <s.kaspari@gmail.com>
Thu, 13 Jul 2017 14:00:28 +0200
changeset 615944 08327db53dcee7803d50367bc244a749d8c6864b
parent 615943 077a9761a0077905b157da6918c34319d68e0f33
child 615945 f1b94d27b8d8ec0053496db10aad1a2d6ae6f8b7
push id70532
push users.kaspari@gmail.com
push dateWed, 26 Jul 2017 15:42:23 +0000
reviewersmcomella
bugs1379021
milestone56.0a1
Bug 1379021 - Draw a horizontal divider between highlight items. r?mcomella MozReview-Commit-ID: Er7TPfw6Xb3
mobile/android/base/java/org/mozilla/gecko/activitystream/homepanel/ActivityStreamPanel.java
mobile/android/base/java/org/mozilla/gecko/activitystream/homepanel/HighlightsDividerItemDecoration.java
mobile/android/base/moz.build
--- a/mobile/android/base/java/org/mozilla/gecko/activitystream/homepanel/ActivityStreamPanel.java
+++ b/mobile/android/base/java/org/mozilla/gecko/activitystream/homepanel/ActivityStreamPanel.java
@@ -62,16 +62,17 @@ public class ActivityStreamPanel extends
 
         final RecyclerView rv = (RecyclerView) findViewById(R.id.activity_stream_main_recyclerview);
 
         rv.setAdapter(adapter);
         rv.setLayoutManager(new LinearLayoutManager(getContext()));
         rv.setHasFixedSize(true);
         // Override item animations to avoid horrible topsites refreshing
         rv.setItemAnimator(new StreamItemAnimator());
+        rv.addItemDecoration(new HighlightsDividerItemDecoration(context));
 
         RecyclerViewClickSupport.addTo(rv)
                 .setOnItemClickListener(adapter);
 
         final Resources resources = getResources();
         desiredTileWidth = resources.getDimensionPixelSize(R.dimen.activity_stream_desired_tile_width);
         desiredTilesHeight = resources.getDimensionPixelSize(R.dimen.activity_stream_desired_tile_height);
         tileMargin = resources.getDimensionPixelSize(R.dimen.activity_stream_base_margin);
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/java/org/mozilla/gecko/activitystream/homepanel/HighlightsDividerItemDecoration.java
@@ -0,0 +1,55 @@
+/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+package org.mozilla.gecko.activitystream.homepanel;
+
+import android.content.Context;
+import android.content.res.TypedArray;
+import android.graphics.Canvas;
+import android.graphics.Rect;
+import android.graphics.drawable.Drawable;
+import android.support.v7.widget.RecyclerView;
+import android.view.View;
+
+/**
+ * ItemDecoration implementation that draws horizontal divider line between highlight items.
+ */
+/* package */ class HighlightsDividerItemDecoration extends RecyclerView.ItemDecoration {
+    // We do not want to draw a divider for the first items: Top sites panel and highlights title.
+    private static final int START_DRAWING_AT_POSITION = 2;
+
+    private static final int[] ATTRS = new int[]{
+            android.R.attr.listDivider
+    };
+    private Drawable mDivider;
+
+    /* package */ HighlightsDividerItemDecoration(Context context) {
+        final TypedArray a = context.obtainStyledAttributes(ATTRS);
+        mDivider = a.getDrawable(0);
+        a.recycle();
+    }
+
+    @Override
+    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
+        final int left = parent.getPaddingLeft();
+        final int right = parent.getWidth() - parent.getPaddingRight();
+
+        final int childCount = parent.getChildCount();
+        for (int i = START_DRAWING_AT_POSITION; i < childCount; i++) {
+            final View child = parent.getChildAt(i);
+            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
+                    .getLayoutParams();
+            final int top = child.getBottom() + params.bottomMargin;
+            final int bottom = top + mDivider.getIntrinsicHeight();
+            mDivider.setBounds(left, top, right, bottom);
+            mDivider.draw(c);
+        }
+    }
+
+    @Override
+    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
+        outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
+    }
+}
--- a/mobile/android/base/moz.build
+++ b/mobile/android/base/moz.build
@@ -499,16 +499,17 @@ gbjar.sources += ['java/org/mozilla/geck
     'ActionModeCompatView.java',
     'ActivityHandlerHelper.java',
     'activitystream/ActivityStream.java',
     'activitystream/ActivityStreamPreference.java',
     'activitystream/ActivityStreamTelemetry.java',
     'activitystream/homepanel/ActivityStreamHomeFragment.java',
     'activitystream/homepanel/ActivityStreamHomeScreen.java',
     'activitystream/homepanel/ActivityStreamPanel.java',
+    'activitystream/homepanel/HighlightsDividerItemDecoration.java',
     'activitystream/homepanel/HighlightsLoader.java',
     'activitystream/homepanel/menu/ActivityStreamContextMenu.java',
     'activitystream/homepanel/menu/BottomSheetContextMenu.java',
     'activitystream/homepanel/menu/PopupContextMenu.java',
     'activitystream/homepanel/model/Highlight.java',
     'activitystream/homepanel/model/Item.java',
     'activitystream/homepanel/model/Metadata.java',
     'activitystream/homepanel/model/TopSite.java',