You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@weex.apache.org by so...@apache.org on 2017/06/09 01:36:39 UTC

[06/12] incubator-weex git commit: * [android] migrate drag feature into BasicListComponent

* [android] migrate drag feature into BasicListComponent


Project: http://git-wip-us.apache.org/repos/asf/incubator-weex/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-weex/commit/f6f295b4
Tree: http://git-wip-us.apache.org/repos/asf/incubator-weex/tree/f6f295b4
Diff: http://git-wip-us.apache.org/repos/asf/incubator-weex/diff/f6f295b4

Branch: refs/heads/0.14-dev
Commit: f6f295b4b6587a389d23dd6ebce4679136bd96e2
Parents: 7de245d
Author: Rowandjj <rj...@gmail.com>
Authored: Thu Jun 8 15:04:07 2017 +0800
Committer: Rowandjj <rj...@gmail.com>
Committed: Thu Jun 8 15:04:07 2017 +0800

----------------------------------------------------------------------
 .../java/com/taobao/weex/common/Constants.java  |   1 +
 .../ui/component/list/BasicListComponent.java   | 175 +++++++++++++++++-
 .../weex/ui/component/list/WXListComponent.java | 181 -------------------
 3 files changed, 175 insertions(+), 182 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/f6f295b4/android/sdk/src/main/java/com/taobao/weex/common/Constants.java
----------------------------------------------------------------------
diff --git a/android/sdk/src/main/java/com/taobao/weex/common/Constants.java b/android/sdk/src/main/java/com/taobao/weex/common/Constants.java
index 7e8a373..eae7080 100644
--- a/android/sdk/src/main/java/com/taobao/weex/common/Constants.java
+++ b/android/sdk/src/main/java/com/taobao/weex/common/Constants.java
@@ -153,6 +153,7 @@ public class Constants {
     String OFFSET_X_RATIO = "offsetXRatio";
     String ELEVATION = "elevation";
     String SCROLLABLE = "scrollable";
+    String DRAGGABLE = "draggable";
     String DISTANCE_Y = "dy";
     String PULLING_DISTANCE = "pullingDistance";
     String VIEW_HEIGHT = "viewHeight";

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/f6f295b4/android/sdk/src/main/java/com/taobao/weex/ui/component/list/BasicListComponent.java
----------------------------------------------------------------------
diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/component/list/BasicListComponent.java b/android/sdk/src/main/java/com/taobao/weex/ui/component/list/BasicListComponent.java
index efe9147..a6e558a 100644
--- a/android/sdk/src/main/java/com/taobao/weex/ui/component/list/BasicListComponent.java
+++ b/android/sdk/src/main/java/com/taobao/weex/ui/component/list/BasicListComponent.java
@@ -27,12 +27,14 @@ import android.os.Build;
 import android.support.annotation.NonNull;
 import android.support.annotation.Nullable;
 import android.support.v4.util.ArrayMap;
+import android.support.v4.view.MotionEventCompat;
 import android.support.v7.widget.GridLayoutManager;
 import android.support.v7.widget.LinearLayoutManager;
 import android.support.v7.widget.RecyclerView;
 import android.support.v7.widget.StaggeredGridLayoutManager;
 import android.text.TextUtils;
 import android.util.SparseArray;
+import android.view.MotionEvent;
 import android.view.View;
 import android.view.ViewGroup;
 import android.view.ViewTreeObserver;
@@ -45,6 +47,7 @@ import com.taobao.weex.annotation.JSMethod;
 import com.taobao.weex.common.Constants;
 import com.taobao.weex.common.OnWXScrollListener;
 import com.taobao.weex.dom.ImmutableDomObject;
+import com.taobao.weex.dom.WXAttr;
 import com.taobao.weex.dom.WXDomObject;
 import com.taobao.weex.ui.component.AppearanceHelper;
 import com.taobao.weex.ui.component.Scrollable;
@@ -66,7 +69,9 @@ import com.taobao.weex.utils.WXLogUtils;
 import com.taobao.weex.utils.WXUtils;
 import com.taobao.weex.utils.WXViewUtils;
 
+import java.util.ArrayDeque;
 import java.util.ArrayList;
+import java.util.Deque;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
@@ -110,6 +115,33 @@ public abstract class BasicListComponent<T extends ViewGroup & ListComponentView
 
   private RecyclerView.ItemAnimator mItemAnimator;
 
+  private DragHelper mDragHelper;
+
+  /**
+   * exclude cell when dragging(attributes for cell)
+   */
+  private static final String EXCLUDED = "dragExcluded";
+
+  /**
+   * the type to trigger drag-drop
+   */
+  private static final String DRAG_TRIGGER_TYPE = "dragTriggerType";
+
+  private static final String DEFAULT_TRIGGER_TYPE = DragTriggerType.LONG_PRESS;
+  private static final boolean DEFAULT_EXCLUDED = false;
+
+  private static final String DRAG_ANCHOR = "dragAnchor";
+
+  /**
+   * gesture type which can trigger drag&drop
+   */
+  interface DragTriggerType {
+    String PAN = "pan";
+    String LONG_PRESS = "longpress";
+  }
+
+  private String mTriggerType;
+
   /**
    * Map for storing component that is sticky.
    **/
@@ -122,6 +154,31 @@ public abstract class BasicListComponent<T extends ViewGroup & ListComponentView
     stickyHelper = new WXStickyHelper(this);
   }
 
+  @Override
+  protected void onHostViewInitialized(T host) {
+    super.onHostViewInitialized(host);
+
+    WXRecyclerView recyclerView = host.getInnerView();
+    if (recyclerView == null || recyclerView.getAdapter() == null) {
+      WXLogUtils.e(TAG, "RecyclerView is not found or Adapter is not bound");
+      return;
+    }
+
+    if (mChildren == null) {
+      WXLogUtils.e(TAG, "children is null");
+      return;
+    }
+
+    mDragHelper = new DefaultDragHelper(mChildren, recyclerView, new EventTrigger() {
+      @Override
+      public void triggerEvent(String type, Map<String, Object> args) {
+        fireEvent(type, args);
+      }
+    });
+
+    mTriggerType = getTriggerType(getDomObject());
+  }
+
   /**
    * Measure the size of the recyclerView.
    *
@@ -335,6 +392,9 @@ public abstract class BasicListComponent<T extends ViewGroup & ListComponentView
         int accuracy = WXUtils.getInteger(param, 10);
         setOffsetAccuracy(accuracy);
         return true;
+      case Constants.Name.DRAGGABLE:
+        boolean draggable = WXUtils.getBoolean(param,false);
+        setDraggable(draggable);
     }
     return super.setProperty(key, param);
   }
@@ -354,6 +414,16 @@ public abstract class BasicListComponent<T extends ViewGroup & ListComponentView
     this.mOffsetAccuracy = (int) real;
   }
 
+  @WXComponentProp(name = Constants.Name.DRAGGABLE)
+  public void setDraggable(boolean isDraggable) {
+    if (mDragHelper != null) {
+      mDragHelper.setDraggable(isDraggable);
+    }
+    if (WXEnvironment.isApkDebugable()) {
+      WXLogUtils.d("set draggable : " + isDraggable);
+    }
+  }
+
   @Override
   public boolean isScrollable() {
     return isScrollable;
@@ -746,7 +816,7 @@ public abstract class BasicListComponent<T extends ViewGroup & ListComponentView
    * @param position position of component in list
    */
   @Override
-  public void onBindViewHolder(ListBaseViewHolder holder, int position) {
+  public void onBindViewHolder(final ListBaseViewHolder holder, int position) {
     if (holder == null) return;
     holder.setComponentUsing(true);
     WXComponent component = getChild(position);
@@ -765,6 +835,45 @@ public abstract class BasicListComponent<T extends ViewGroup & ListComponentView
       if(holder.isRecycled()) {
         holder.bindData(component);
       }
+      if (mDragHelper == null || !mDragHelper.isDraggable()) {
+        return;
+      }
+      mTriggerType = (mTriggerType == null) ? DEFAULT_TRIGGER_TYPE : mTriggerType;
+
+      WXCell cell = (WXCell) holder.getComponent();
+      boolean isExcluded = isDragExcluded(cell.getDomObject());
+      mDragHelper.setDragExcluded(holder, isExcluded);
+
+      //NOTICE: event maybe consumed by other views
+      if (DragTriggerType.PAN.equals(mTriggerType)) {
+        mDragHelper.setLongPressDragEnabled(false);
+
+        WXComponent anchorComponent = findComponentByAnchorName(cell, DRAG_ANCHOR);
+
+        if (anchorComponent != null && anchorComponent.getHostView() != null && !isExcluded) {
+          View anchor = anchorComponent.getHostView();
+          anchor.setOnTouchListener(new View.OnTouchListener() {
+            @Override
+            public boolean onTouch(View v, MotionEvent event) {
+              if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
+                mDragHelper.startDrag(holder);
+              }
+              return true;
+            }
+          });
+        } else {
+          if (WXEnvironment.isApkDebugable()) {
+            if(!isExcluded) {
+              WXLogUtils.e(TAG, "[error] onBindViewHolder: the anchor component or view is not found");
+            } else {
+              WXLogUtils.d(TAG, "onBindViewHolder: position "+ position + " is drag excluded");
+            }
+          }
+        }
+
+      } else if (DragTriggerType.LONG_PRESS.equals(mTriggerType)) {
+        mDragHelper.setLongPressDragEnabled(true);
+      }
     }
 
   }
@@ -866,6 +975,70 @@ public abstract class BasicListComponent<T extends ViewGroup & ListComponentView
     return generateViewType(getChild(position));
   }
 
+  @Nullable
+  private WXComponent findComponentByAnchorName(@NonNull WXComponent root, @NonNull String anchorName) {
+    long start = 0;
+    if (WXEnvironment.isApkDebugable()) {
+      start = System.currentTimeMillis();
+    }
+
+    Deque<WXComponent> deque = new ArrayDeque<>();
+    deque.add(root);
+    while (!deque.isEmpty()) {
+      WXComponent curComponent = deque.removeFirst();
+      ImmutableDomObject object = curComponent.getDomObject();
+      if (object != null) {
+        String isAnchorSet = WXUtils.getString(object.getAttrs().get(anchorName), null);
+
+        //hit
+        if (isAnchorSet != null && isAnchorSet.equals("true")) {
+          if (WXEnvironment.isApkDebugable()) {
+            WXLogUtils.d("dragPerf", "findComponentByAnchorName time: " + (System.currentTimeMillis() - start) + "ms");
+          }
+          return curComponent;
+        }
+      }
+      if (curComponent instanceof WXVContainer) {
+        WXVContainer container = (WXVContainer) curComponent;
+        for (int i = 0, len = container.childCount(); i < len; i++) {
+          WXComponent child = container.getChild(i);
+          deque.add(child);
+        }
+      }
+    }
+
+    if (WXEnvironment.isApkDebugable()) {
+      WXLogUtils.d("dragPerf", "findComponentByAnchorName elapsed time: " + (System.currentTimeMillis() - start) + "ms");
+    }
+    return null;
+
+  }
+
+  private String getTriggerType(@Nullable ImmutableDomObject domObject) {
+    String triggerType = DEFAULT_TRIGGER_TYPE;
+    if (domObject == null) {
+      return triggerType;
+    }
+    triggerType = WXUtils.getString(domObject.getAttrs().get(DRAG_TRIGGER_TYPE), DEFAULT_TRIGGER_TYPE);
+    if (!DragTriggerType.LONG_PRESS.equals(triggerType) && !DragTriggerType.PAN.equals(triggerType)) {
+      triggerType = DEFAULT_TRIGGER_TYPE;
+    }
+
+    if (WXEnvironment.isApkDebugable()) {
+      WXLogUtils.d(TAG, "trigger type is " + triggerType);
+    }
+
+    return triggerType;
+  }
+
+  private boolean isDragExcluded(@Nullable ImmutableDomObject domObject) {
+    if (domObject == null) {
+      return DEFAULT_EXCLUDED;
+    }
+    WXAttr cellAttrs = domObject.getAttrs();
+    return WXUtils.getBoolean(cellAttrs.get(EXCLUDED), DEFAULT_EXCLUDED);
+  }
+
   /**
    * ViewType will be classified into {HashMap<Integer,ArrayList<Integer>> mViewTypes}
    *

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/f6f295b4/android/sdk/src/main/java/com/taobao/weex/ui/component/list/WXListComponent.java
----------------------------------------------------------------------
diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/component/list/WXListComponent.java b/android/sdk/src/main/java/com/taobao/weex/ui/component/list/WXListComponent.java
index c43ba68..2360e06 100644
--- a/android/sdk/src/main/java/com/taobao/weex/ui/component/list/WXListComponent.java
+++ b/android/sdk/src/main/java/com/taobao/weex/ui/component/list/WXListComponent.java
@@ -19,18 +19,10 @@
 package com.taobao.weex.ui.component.list;
 
 import android.content.Context;
-import android.support.annotation.NonNull;
-import android.support.annotation.Nullable;
-import android.support.v4.view.MotionEventCompat;
-import android.view.MotionEvent;
-import android.view.View;
 
-import com.taobao.weex.WXEnvironment;
 import com.taobao.weex.WXSDKInstance;
 import com.taobao.weex.annotation.Component;
 import com.taobao.weex.common.Constants;
-import com.taobao.weex.dom.ImmutableDomObject;
-import com.taobao.weex.dom.WXAttr;
 import com.taobao.weex.dom.WXDomObject;
 import com.taobao.weex.dom.WXRecyclerDomObject;
 import com.taobao.weex.dom.flex.Spacing;
@@ -45,10 +37,7 @@ import com.taobao.weex.ui.view.listview.WXRecyclerView;
 import com.taobao.weex.ui.view.listview.adapter.ListBaseViewHolder;
 import com.taobao.weex.ui.view.refresh.wrapper.BounceRecyclerView;
 import com.taobao.weex.utils.WXLogUtils;
-import com.taobao.weex.utils.WXUtils;
 
-import java.util.ArrayDeque;
-import java.util.Deque;
 import java.util.Map;
 
 /**
@@ -66,34 +55,6 @@ public class WXListComponent extends BasicListComponent<BounceRecyclerView> {
   private float mPaddingLeft;
   private float mPaddingRight;
 
-  private DragHelper mDragHelper;
-
-  /**
-   * attributes for cell
-   */
-  private static final String EXCLUDED = "dragExcluded";
-
-  /**
-   * attributes for list self
-   */
-  private static final String DRAGGABLE = "draggable";
-  private static final String DRAG_TRIGGER_TYPE = "dragTriggerType";
-
-  private static final String DEFAULT_TRIGGER_TYPE = DragTriggerType.LONG_PRESS;
-  private static final boolean DEFAULT_EXCLUDED = false;
-
-  private static final String DRAG_ANCHOR = "dragAnchor";
-
-  /**
-   * gesture type which can trigger drag&drop
-   */
-  interface DragTriggerType {
-    String PAN = "pan";
-    String LONG_PRESS = "longpress";
-  }
-
-  private String mTriggerType;
-
   @Deprecated
   public WXListComponent(WXSDKInstance instance, WXDomObject dom, WXVContainer parent, String instanceId, boolean isLazy) {
     this(instance, dom, parent, isLazy);
@@ -117,87 +78,8 @@ public class WXListComponent extends BasicListComponent<BounceRecyclerView> {
   }
 
   @Override
-  protected void onHostViewInitialized(BounceRecyclerView host) {
-    super.onHostViewInitialized(host);
-
-    WXRecyclerView recyclerView = host.getInnerView();
-    if (recyclerView == null || recyclerView.getAdapter() == null) {
-      WXLogUtils.e(TAG, "RecyclerView is not found or Adapter is not bound");
-      return;
-    }
-
-    if (mChildren == null) {
-      WXLogUtils.e(TAG, "children is null");
-      return;
-    }
-
-    mDragHelper = new DefaultDragHelper(mChildren, recyclerView, new EventTrigger() {
-      @Override
-      public void triggerEvent(String type, Map<String, Object> args) {
-        fireEvent(type, args);
-      }
-    });
-
-    mTriggerType = getTriggerType(getDomObject());
-  }
-
-  @WXComponentProp(name = DRAGGABLE)
-  @SuppressWarnings("unused")
-  public void setDraggable(boolean isDraggable) {
-    if (mDragHelper != null) {
-      mDragHelper.setDraggable(isDraggable);
-    }
-    if (WXEnvironment.isApkDebugable()) {
-      WXLogUtils.d("set draggable : " + isDraggable);
-    }
-  }
-
-  @Override
   public void onBindViewHolder(final ListBaseViewHolder holder, int position) {
     super.onBindViewHolder(holder, position);
-
-    if (mDragHelper == null) {
-      return;
-    }
-
-    if (holder != null && holder.getComponent() != null && holder.getComponent() instanceof WXCell) {
-      mTriggerType = (mTriggerType == null) ? DEFAULT_TRIGGER_TYPE : mTriggerType;
-
-      WXCell cell = (WXCell) holder.getComponent();
-      boolean isExcluded = isDragExcluded(cell.getDomObject());
-      mDragHelper.setDragExcluded(holder, isExcluded);
-
-      //NOTICE: event maybe consumed by other views
-      if (DragTriggerType.PAN.equals(mTriggerType)) {
-        mDragHelper.setLongPressDragEnabled(false);
-
-        WXComponent anchorComponent = findComponentByAnchorName(cell, DRAG_ANCHOR);
-
-        if (anchorComponent != null && anchorComponent.getHostView() != null && !isExcluded) {
-          View anchor = anchorComponent.getHostView();
-          anchor.setOnTouchListener(new View.OnTouchListener() {
-            @Override
-            public boolean onTouch(View v, MotionEvent event) {
-              if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
-                mDragHelper.startDrag(holder);
-              }
-              return true;
-            }
-          });
-        } else {
-          if (WXEnvironment.isApkDebugable()) {
-            if(!isExcluded) {
-              WXLogUtils.e(TAG, "[error] onBindViewHolder: the anchor component or view is not found");
-            } else {
-              WXLogUtils.d(TAG, "onBindViewHolder: position "+ position + " is drag excluded");
-            }
-          }
-        }
-
-      } else if (DragTriggerType.LONG_PRESS.equals(mTriggerType)) {
-        mDragHelper.setLongPressDragEnabled(true);
-      }
-    }
   }
 
   @Override
@@ -361,67 +243,4 @@ public class WXListComponent extends BasicListComponent<BounceRecyclerView> {
     }
   }
 
-  @Nullable
-  private WXComponent findComponentByAnchorName(@NonNull WXComponent root, @NonNull String anchorName) {
-    long start = 0;
-    if (WXEnvironment.isApkDebugable()) {
-      start = System.currentTimeMillis();
-    }
-
-    Deque<WXComponent> deque = new ArrayDeque<>();
-    deque.add(root);
-    while (!deque.isEmpty()) {
-      WXComponent curComponent = deque.removeFirst();
-      ImmutableDomObject object = curComponent.getDomObject();
-      if (object != null) {
-        String isAnchorSet = WXUtils.getString(object.getAttrs().get(anchorName), null);
-
-        //hit
-        if (isAnchorSet != null && isAnchorSet.equals("true")) {
-          if (WXEnvironment.isApkDebugable()) {
-            WXLogUtils.d("dragPerf", "findComponentByAnchorName time: " + (System.currentTimeMillis() - start) + "ms");
-          }
-          return curComponent;
-        }
-      }
-      if (curComponent instanceof WXVContainer) {
-        WXVContainer container = (WXVContainer) curComponent;
-        for (int i = 0, len = container.childCount(); i < len; i++) {
-          WXComponent child = container.getChild(i);
-          deque.add(child);
-        }
-      }
-    }
-
-    if (WXEnvironment.isApkDebugable()) {
-      WXLogUtils.d("dragPerf", "findComponentByAnchorName elapsed time: " + (System.currentTimeMillis() - start) + "ms");
-    }
-    return null;
-
-  }
-
-  private String getTriggerType(@Nullable ImmutableDomObject domObject) {
-    String triggerType = DEFAULT_TRIGGER_TYPE;
-    if (domObject == null) {
-      return triggerType;
-    }
-    triggerType = WXUtils.getString(domObject.getAttrs().get(DRAG_TRIGGER_TYPE), DEFAULT_TRIGGER_TYPE);
-    if (!DragTriggerType.LONG_PRESS.equals(triggerType) && !DragTriggerType.PAN.equals(triggerType)) {
-      triggerType = DEFAULT_TRIGGER_TYPE;
-    }
-
-    if (WXEnvironment.isApkDebugable()) {
-      WXLogUtils.d(TAG, "trigger type is " + triggerType);
-    }
-
-    return triggerType;
-  }
-
-  private boolean isDragExcluded(@Nullable ImmutableDomObject domObject) {
-    if (domObject == null) {
-      return DEFAULT_EXCLUDED;
-    }
-    WXAttr cellAttrs = domObject.getAttrs();
-    return WXUtils.getBoolean(cellAttrs.get(EXCLUDED), DEFAULT_EXCLUDED);
-  }
 }