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:35 UTC

[02/12] incubator-weex git commit: * [android] attach drag-drop feature to list

* [android] attach drag-drop feature to list


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

Branch: refs/heads/0.14-dev
Commit: dde48f58e4b560ac34ae1f87e561b1201c1dd35f
Parents: 3e33dca
Author: Rowandjj <rj...@gmail.com>
Authored: Mon Jun 5 10:44:21 2017 +0800
Committer: Rowandjj <rj...@gmail.com>
Committed: Mon Jun 5 10:44:21 2017 +0800

----------------------------------------------------------------------
 .../ui/component/list/DefaultDragHelper.java    |   8 -
 .../ui/component/list/DragSupportCallback.java  |   3 +-
 .../weex/ui/component/list/WXDragHelper.java    |  20 +-
 .../weex/ui/component/list/WXListComponent.java | 187 +++++++++++++++++++
 4 files changed, 189 insertions(+), 29 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/dde48f58/android/sdk/src/main/java/com/taobao/weex/ui/component/list/DefaultDragHelper.java
----------------------------------------------------------------------
diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/component/list/DefaultDragHelper.java b/android/sdk/src/main/java/com/taobao/weex/ui/component/list/DefaultDragHelper.java
index ab5cb7f..1a85e4d 100644
--- a/android/sdk/src/main/java/com/taobao/weex/ui/component/list/DefaultDragHelper.java
+++ b/android/sdk/src/main/java/com/taobao/weex/ui/component/list/DefaultDragHelper.java
@@ -344,14 +344,6 @@ class DefaultDragHelper implements WXDragHelper {
         return viewHolder.itemView.getTag() != null && TAG_EXCLUDED.equals(viewHolder.itemView.getTag());
     }
 
-    /**
-     * timestamp
-     * target
-     * fromIndex
-     * toIndex
-     *
-     *
-     * */
     private Map<String, Object> buildEvent(@Nullable String target, int fromIndex, int toIndex) {
         Map<String, Object> args = new HashMap<>(4);
         args.put("target", target == null ? "unknown" : target);

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/dde48f58/android/sdk/src/main/java/com/taobao/weex/ui/component/list/DragSupportCallback.java
----------------------------------------------------------------------
diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/component/list/DragSupportCallback.java b/android/sdk/src/main/java/com/taobao/weex/ui/component/list/DragSupportCallback.java
index c554805..e6174e4 100644
--- a/android/sdk/src/main/java/com/taobao/weex/ui/component/list/DragSupportCallback.java
+++ b/android/sdk/src/main/java/com/taobao/weex/ui/component/list/DragSupportCallback.java
@@ -215,8 +215,7 @@ import com.taobao.weex.utils.WXLogUtils;
 
 /**
  * Description:
- * An implementation of {@link ItemTouchHelper.Callback}.
- * with drag&drop support:)
+ * An implementation of {@link ItemTouchHelper.Callback}. with drag&drop support:)
  *
  * <p>
  * Created by rowandjj(chuyi)<br/>

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/dde48f58/android/sdk/src/main/java/com/taobao/weex/ui/component/list/WXDragHelper.java
----------------------------------------------------------------------
diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/component/list/WXDragHelper.java b/android/sdk/src/main/java/com/taobao/weex/ui/component/list/WXDragHelper.java
index 8986674..b8fef78 100644
--- a/android/sdk/src/main/java/com/taobao/weex/ui/component/list/WXDragHelper.java
+++ b/android/sdk/src/main/java/com/taobao/weex/ui/component/list/WXDragHelper.java
@@ -220,29 +220,11 @@ import com.taobao.weex.ui.component.WXComponent;
  * Time: 上午11:34<br/>
  */
 interface WXDragHelper {
-    /**
-     *  开始拖拽
-     *
-     *  1. 发送dragstart事件
-     *  2. 设置选中的样式
-     *
-     * */
+
     void onDragStart(@NonNull WXComponent component, int from);
 
-    /**
-     *
-     *  1. 发送gragend事件
-     *  2. 恢复样式
-     *
-     *  from 可能 等于 to, 代表没有发生位置变化
-     *
-     * */
     void onDragEnd(@NonNull WXComponent component, int from, int to);
 
-
-    /**
-     * 处理数据源变化
-     * */
     void onDragging(int fromPos, int toPos);
 
     boolean isLongPressDragEnabled();

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/dde48f58/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 9b36be4..29d5115 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,10 +19,18 @@
 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;
@@ -37,7 +45,10 @@ 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;
 
 /**
@@ -55,6 +66,34 @@ public class WXListComponent extends BasicListComponent<BounceRecyclerView> {
   private float mPaddingLeft;
   private float mPaddingRight;
 
+  private WXDragHelper mWXDragHelper;
+
+  /**
+   * 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);
@@ -78,6 +117,90 @@ 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;
+    }
+
+    mWXDragHelper = 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 (mWXDragHelper != null) {
+      mWXDragHelper.setDraggable(isDraggable);
+    }
+    if (WXEnvironment.isApkDebugable()) {
+      WXLogUtils.d("set draggable : " + isDraggable);
+    }
+  }
+
+  @Override
+  public void onBindViewHolder(final ListBaseViewHolder holder, int position) {
+    super.onBindViewHolder(holder, position);
+
+    if (mWXDragHelper == 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 = getExcluded(cell.getDomObject());
+      mWXDragHelper.setDragExcluded(holder, isExcluded);
+
+      //NOTICE: event maybe consumed by other views
+      if (DragTriggerType.PAN.equals(mTriggerType)) {
+        mWXDragHelper.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) {
+                mWXDragHelper.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)) {
+        mWXDragHelper.setLongPressDragEnabled(true);
+      }
+    }
+  }
+
+  @Override
   protected BounceRecyclerView generateListView(Context context, int orientation) {
 
     return new BounceRecyclerView(context,mLayoutType,mColumnCount,mColumnGap,orientation);
@@ -237,4 +360,68 @@ public class WXListComponent extends BasicListComponent<BounceRecyclerView> {
       getHostView().removeHeaderView(child);
     }
   }
+
+  @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 getExcluded(@Nullable ImmutableDomObject domObject) {
+    if (domObject == null) {
+      return DEFAULT_EXCLUDED;
+    }
+    WXAttr cellAttrs = domObject.getAttrs();
+    return WXUtils.getBoolean(cellAttrs.get(EXCLUDED), DEFAULT_EXCLUDED);
+  }
 }