Bug 1310081 - 2. Refactor TabsListTouchHelperCallback to support any swipe alpha function. r=sebastian
authorTom Klein <twointofive@gmail.com>
Mon, 12 Sep 2016 11:01:08 -0500
changeset 324002 19f73951d4e542473ba819c3bcc5551b97ba4e22
parent 324001 d64d6d962c6712996f0c4f13f22ca7c50d62fbcb
child 324003 59bfc010206650c3c6bff4c23f27bad632d57138
push id24
push usermaklebus@msu.edu
push dateTue, 20 Dec 2016 03:11:33 +0000
reviewerssebastian
bugs1310081
milestone53.0a1
Bug 1310081 - 2. Refactor TabsListTouchHelperCallback to support any swipe alpha function. r=sebastian MozReview-Commit-ID: HqKwdpArcMm
mobile/android/base/java/org/mozilla/gecko/tabs/TabsGridLayout.java
mobile/android/base/java/org/mozilla/gecko/tabs/TabsListLayout.java
mobile/android/base/java/org/mozilla/gecko/tabs/TabsTouchHelperCallback.java
--- a/mobile/android/base/java/org/mozilla/gecko/tabs/TabsGridLayout.java
+++ b/mobile/android/base/java/org/mozilla/gecko/tabs/TabsGridLayout.java
@@ -5,16 +5,17 @@
 
 package org.mozilla.gecko.tabs;
 
 import org.mozilla.gecko.R;
 
 import android.content.Context;
 import android.content.res.Resources;
 import android.support.v7.widget.GridLayoutManager;
+import android.support.v7.widget.helper.ItemTouchHelper;
 import android.util.AttributeSet;
 
 public class TabsGridLayout extends TabsLayout {
     private final int desiredColumnWidth;
 
     public TabsGridLayout(Context context, AttributeSet attrs) {
         super(context, attrs, R.layout.tabs_layout_item_view);
 
@@ -28,16 +29,26 @@ public class TabsGridLayout extends Tabs
 
         setPadding(viewPaddingHorizontal, viewPaddingVertical, viewPaddingHorizontal, viewPaddingVertical);
         setClipToPadding(false);
         setScrollBarStyle(SCROLLBARS_OUTSIDE_OVERLAY);
 
         setItemAnimator(new TabsGridLayoutAnimator());
 
         // TODO Add ItemDecoration.
+
+        // A TouchHelper handler for swipe to close.
+        final TabsTouchHelperCallback callback = new TabsTouchHelperCallback(this) {
+            @Override
+            protected float alphaForItemSwipeDx(float dX, int distanceToAlphaMin) {
+                return 1f - 2f * Math.abs(dX) / distanceToAlphaMin;
+            }
+        };
+        final ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
+        touchHelper.attachToRecyclerView(this);
     }
 
     @Override
     public void closeAll() {
         autoHidePanel();
 
         closeAllTabs();
     }
--- a/mobile/android/base/java/org/mozilla/gecko/tabs/TabsListLayout.java
+++ b/mobile/android/base/java/org/mozilla/gecko/tabs/TabsListLayout.java
@@ -27,17 +27,23 @@ public class TabsListLayout extends Tabs
     public TabsListLayout(Context context, AttributeSet attrs) {
         super(context, attrs, R.layout.tabs_list_item_view);
 
         setHasFixedSize(true);
 
         setLayoutManager(new LinearLayoutManager(context));
 
         // A TouchHelper handler for swipe to close.
-        final TabsTouchHelperCallback callback = new TabsTouchHelperCallback(this);
+        final TabsTouchHelperCallback callback = new TabsTouchHelperCallback(this) {
+            @Override
+            protected float alphaForItemSwipeDx(float dX, int distanceToAlphaMin) {
+                return Math.max(0.1f,
+                        Math.min(1f, 1f - 2f * Math.abs(dX) / distanceToAlphaMin));
+            }
+        };
         final ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
         touchHelper.attachToRecyclerView(this);
 
         setItemAnimator(new TabsListLayoutAnimator(ANIMATION_DURATION));
     }
 
     @Override
     public void closeAll() {
--- a/mobile/android/base/java/org/mozilla/gecko/tabs/TabsTouchHelperCallback.java
+++ b/mobile/android/base/java/org/mozilla/gecko/tabs/TabsTouchHelperCallback.java
@@ -5,17 +5,17 @@
 
 package org.mozilla.gecko.tabs;
 
 import android.graphics.Canvas;
 import android.support.v7.widget.RecyclerView;
 import android.support.v7.widget.helper.ItemTouchHelper;
 import android.view.View;
 
-class TabsTouchHelperCallback extends ItemTouchHelper.Callback {
+abstract class TabsTouchHelperCallback extends ItemTouchHelper.Callback {
     private final DismissListener dismissListener;
 
     interface DismissListener {
         void onItemDismiss(View view);
     }
 
     public TabsTouchHelperCallback(DismissListener dismissListener) {
         this.dismissListener = dismissListener;
@@ -37,33 +37,40 @@ class TabsTouchHelperCallback extends It
     }
 
     @Override
     public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
                           RecyclerView.ViewHolder target) {
         return false;
     }
 
-    // Alpha on an itemView being swiped should decrease to a min over a distance equal to the
-    // width of the item being swiped.
+    /**
+     * Returns the alpha an itemView should be set to when swiped by an amount {@code dX}, given
+     * that alpha should decrease to its min at distance {@code distanceToAlphaMin}.
+     */
+    abstract protected float alphaForItemSwipeDx(float dX, int distanceToAlphaMin);
+
+    /**
+     * Alpha on an itemView being swiped should decrease to a min over a distance equal to the
+     * width of the item being swiped.
+     */
     @Override
     public void onChildDraw(Canvas c,
                             RecyclerView recyclerView,
                             RecyclerView.ViewHolder viewHolder,
                             float dX,
                             float dY,
                             int actionState,
                             boolean isCurrentlyActive) {
         if (actionState != ItemTouchHelper.ACTION_STATE_SWIPE) {
             return;
         }
 
         super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
 
-        viewHolder.itemView.setAlpha(Math.max(0.1f,
-                Math.min(1f, 1f - 2f * Math.abs(dX) / viewHolder.itemView.getWidth())));
+        viewHolder.itemView.setAlpha(alphaForItemSwipeDx(dX, viewHolder.itemView.getWidth()));
     }
 
     public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
         super.clearView(recyclerView, viewHolder);
         viewHolder.itemView.setAlpha(1);
     }
 }