Bug 1116415 - 7. Refactor TabsListTouchHelperCallback to support any swipe alpha function. r?sebastian
MozReview-Commit-ID: HqKwdpArcMm
--- a/mobile/android/base/java/org/mozilla/gecko/tabs/TabsGridLayout.java
+++ b/mobile/android/base/java/org/mozilla/gecko/tabs/TabsGridLayout.java
@@ -7,16 +7,17 @@ package org.mozilla.gecko.tabs;
import org.mozilla.gecko.R;
import org.mozilla.gecko.widget.SpacingDecoration;
import android.content.Context;
import android.content.res.Resources;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
+import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.AttributeSet;
public class TabsGridLayout extends TabsLayout {
public TabsGridLayout(Context context, AttributeSet attrs) {
super(context, attrs, R.layout.tabs_layout_item_view);
final Resources resources = context.getResources();
@@ -48,16 +49,26 @@ public class TabsGridLayout extends Tabs
final DefaultItemAnimator animator = new DefaultItemAnimator();
// On close we only animate the moves, not the remove.
animator.setRemoveDuration(0);
// A fade in/out each time the title/thumbnail/etc. gets updated isn't helpful, so disable
// the change animation.
animator.setSupportsChangeAnimations(false);
setItemAnimator(animator);
+
+ // 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);
final TabsListLayoutAnimator animator = new TabsListLayoutAnimator();
animator.setRemoveDuration(ANIMATION_DURATION);
// A fade in/out each time the title/thumbnail/etc. gets updated isn't helpful, so disable
// the change animation.
animator.setSupportsChangeAnimations(false);
--- 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;
-public class TabsTouchHelperCallback extends ItemTouchHelper.Callback {
+public abstract class TabsTouchHelperCallback extends ItemTouchHelper.Callback {
private final DismissListener dismissListener;
// Distance over which alpha on the itemView should decrease to its min as we swipe.
private int distanceToAlphaMin;
public interface DismissListener {
void onItemDismiss(View view);
}
@@ -39,32 +39,35 @@ public class TabsTouchHelperCallback ext
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
RecyclerView.ViewHolder target) {
return false;
}
+ // Returns the alpha an itemView should be set to when swiped by an amount dX, given that
+ // alpha should decrease to its min at distance 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) {
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
if (distanceToAlphaMin == 0) {
// Assumes all itemViews are the same width.
distanceToAlphaMin = viewHolder.itemView.getWidth();
}
- viewHolder.itemView.setAlpha(Math.max(0.1f,
- Math.min(1f, 1f - 2f * Math.abs(dX) / distanceToAlphaMin)));
+ viewHolder.itemView.setAlpha(alphaForItemSwipeDx(dX, distanceToAlphaMin));
}
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);
viewHolder.itemView.setAlpha(1);
}
}