Repository: incubator-weex
Updated Branches:
  refs/heads/master d3ffa31f0 -> 613d780c1


[WEEX-386][Core] Add leftgap & rightgap for waterfall.


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

Branch: refs/heads/master
Commit: 613d780c1ebb55734855257db36022773077f39e
Parents: d3ffa31
Author: 驱影 <[email protected]>
Authored: Wed May 23 11:53:04 2018 +0800
Committer: YorkShen <[email protected]>
Committed: Thu May 24 17:01:14 2018 +0800

----------------------------------------------------------------------
 .../java/com/taobao/weex/common/Constants.java  |  1 +
 .../ui/component/list/GapItemDecoration.java    | 55 +++++++------
 .../weex/ui/component/list/WXListComponent.java | 83 +++++++++++---------
 weex_core/Source/core/css/constants_name.h      |  3 +
 weex_core/Source/core/render/node/render_list.h | 64 +++++++++++++--
 5 files changed, 136 insertions(+), 70 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/613d780c/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 fdf5e82..aff5fd3 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
@@ -127,6 +127,7 @@ public class Constants {
     String LOADMOREOFFSET = "loadmoreoffset";
     String RECYCLE_IMAGE = "recycleImage";
     String LAYOUT = "layout";
+    String SPAN_OFFSETS = "spanOffsets";
     String COLUMN_WIDTH = "columnWidth";
     String COLUMN_COUNT = "columnCount";
     String COLUMN_GAP = "columnGap";

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/613d780c/android/sdk/src/main/java/com/taobao/weex/ui/component/list/GapItemDecoration.java
----------------------------------------------------------------------
diff --git 
a/android/sdk/src/main/java/com/taobao/weex/ui/component/list/GapItemDecoration.java
 
b/android/sdk/src/main/java/com/taobao/weex/ui/component/list/GapItemDecoration.java
index 1808c16..821d7ce 100644
--- 
a/android/sdk/src/main/java/com/taobao/weex/ui/component/list/GapItemDecoration.java
+++ 
b/android/sdk/src/main/java/com/taobao/weex/ui/component/list/GapItemDecoration.java
@@ -41,31 +41,34 @@ public class GapItemDecoration extends 
RecyclerView.ItemDecoration {
 
     @Override
     public void getItemOffsets(Rect outRect, View view, RecyclerView parent, 
RecyclerView.State state) {
-        // TODO
-//        int position = parent.getChildAdapterPosition(view);
-//        if(position < 0){
-//            return;
-//        }
-//        if(view.getLayoutParams() instanceof 
StaggeredGridLayoutManager.LayoutParams){
-//            StaggeredGridLayoutManager.LayoutParams params = 
(StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();
-//            if(params.isFullSpan()){
-//                return;
-//            }
-//            WXComponent component = listComponent.getChild(position);
-//            if(component instanceof WXCell){
-//                WXCell cell = (WXCell) component;
-//                if(cell.isFixed() || cell.isSticky()){
-//                    return;
-//                }
-//                WXRecyclerDomObject recyclerDomObject = 
listComponent.getRecyclerDom();
-//                if(recyclerDomObject.getSpanOffsets() == null){
-//                    return;
-//                }
-//                float spanOffset = 
recyclerDomObject.getSpanOffsets()[params.getSpanIndex()];
-//                int   spanOffsetPx =  
Math.round(WXViewUtils.getRealPxByWidth(spanOffset, 
recyclerDomObject.getViewPortWidth()));
-//                outRect.left =  spanOffsetPx;
-//                outRect.right = -spanOffsetPx;
-//            }
-//        }
+        final Float[] spanOffsets = listComponent.getSpanOffsets();
+        if (spanOffsets == null) {
+            return;
+        }
+        int position = parent.getChildAdapterPosition(view);
+        if(position < 0) {
+            return;
+        }
+        if(view.getLayoutParams() instanceof 
StaggeredGridLayoutManager.LayoutParams){
+            StaggeredGridLayoutManager.LayoutParams params = 
(StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();
+            if(params.isFullSpan()){
+                return;
+            }
+            WXComponent component = listComponent.getChild(position);
+            if(component instanceof WXCell){
+                WXCell cell = (WXCell) component;
+                if(cell.isFixed() || cell.isSticky()) {
+                    return;
+                }
+
+                if (params.getSpanIndex() >= spanOffsets.length) {
+                    return;
+                }
+                float spanOffset = 
listComponent.getSpanOffsets()[params.getSpanIndex()];
+                int   spanOffsetPx =  
Math.round(WXViewUtils.getRealPxByWidth(spanOffset, 
listComponent.getViewPortWidth()));
+                outRect.left =  spanOffsetPx;
+                outRect.right = -spanOffsetPx;
+            }
+        }
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/613d780c/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 feca9f9..1db34a4 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
@@ -18,14 +18,19 @@
  */
 package com.taobao.weex.ui.component.list;
 
-import android.content.Context;
-import android.util.Log;
+import java.lang.reflect.Array;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+
+import com.alibaba.fastjson.JSON;
 
+import android.content.Context;
+import android.text.TextUtils;
 import com.taobao.weex.WXSDKInstance;
 import com.taobao.weex.annotation.Component;
 import com.taobao.weex.common.Constants;
 import com.taobao.weex.common.WXThread;
-import com.taobao.weex.dom.CSSShorthand;
 import com.taobao.weex.ui.action.BasicComponentData;
 import com.taobao.weex.ui.component.WXBaseRefresh;
 import com.taobao.weex.ui.component.WXBasicComponentType;
@@ -40,8 +45,6 @@ import 
com.taobao.weex.ui.view.refresh.wrapper.BounceRecyclerView;
 import com.taobao.weex.utils.WXLogUtils;
 import com.taobao.weex.utils.WXUtils;
 
-import java.util.Map;
-
 /**
  * Unlike other components, there is immutable bi-directional association 
between View and
  * ViewHolder, while only mutable and temporal uni-directional association 
between view and
@@ -56,6 +59,9 @@ public class WXListComponent extends 
BasicListComponent<BounceRecyclerView> {
   //  private WXRecyclerDomObject mDomObject;
   private float mPaddingLeft;
   private float mPaddingRight;
+  private String mSpanOffsetsStr;
+  private Float[] mSpanOffsets;
+  private boolean hasSetGapItemDecoration = false;
 
   @Deprecated
   public WXListComponent(WXSDKInstance instance, WXVContainer parent, String 
instanceId, boolean isLazy, BasicComponentData basicComponentData) {
@@ -75,10 +81,6 @@ public class WXListComponent extends 
BasicListComponent<BounceRecyclerView> {
         bounceRecyclerView.getSwipeLayout().setNestedScrollingEnabled(true);
       }
     }
-    // TODO
-    //    if(mRecyclerDom != null && mRecyclerDom.getSpanOffsets() != null){
-    //      bounceRecyclerView.getInnerView().addItemDecoration(new 
GapItemDecoration(this));
-    //    }
     return bounceRecyclerView;
   }
 
@@ -143,34 +145,39 @@ public class WXListComponent extends 
BasicListComponent<BounceRecyclerView> {
     mColumnWidth = 
WXUtils.parseFloat(getAttrs().get(Constants.Name.COLUMN_WIDTH));
     mPaddingLeft = 
WXUtils.parseFloat(getAttrs().get(Constants.Name.PADDING_LEFT));
     mPaddingRight = 
WXUtils.parseFloat(getAttrs().get(Constants.Name.PADDING_RIGHT));
-    // TODO
-    //      mLeftGap = mRecyclerDom.getLeftGap();
-    //      mRightGap = mRecyclerDom.getRightGap();
-    //      mRecyclerDom.preCalculateCellWidth();
+    mSpanOffsetsStr = (String)getAttrs().get(Constants.Name.SPAN_OFFSETS);
+
+    try {
+      if (!TextUtils.isEmpty(mSpanOffsetsStr)) {
+        List<Float> list = JSON.parseArray(mSpanOffsetsStr, Float.class);
+        final int size = list.size();
+        if (null == mSpanOffsets || mSpanOffsets.length != size) {
+          mSpanOffsets = new Float[size];
+        }
+        list.toArray(mSpanOffsets);
+      } else {
+        mSpanOffsets = null;
+      }
+    } catch (Throwable e) {
+      WXLogUtils.w("Parser SpanOffsets error ", e);
+    }
+
+    if (!hasSetGapItemDecoration && null != getSpanOffsets() && null != 
getHostView()
+        && null != getHostView().getInnerView()) {
+      hasSetGapItemDecoration = true;
+      getHostView().getInnerView().addItemDecoration(new 
GapItemDecoration(this));
+    }
   }
 
-  // TODO
-//  @WXComponentProp(name = Constants.Name.LEFT_GAP)
-  //  public void setLeftGap(float leftGap)  {
-  //    if(mRecyclerDom != null && mRecyclerDom.getLeftGap() != mLeftGap){
-  //      markComponentUsable();
-  //      mRecyclerDom.preCalculateCellWidth();
-  //      updateRecyclerAttr();
-  //      WXRecyclerView wxRecyclerView = getHostView().getInnerView();
-  //      wxRecyclerView.initView(getContext(), 
mLayoutType,mColumnCount,mColumnGap,getOrientation());
-  //    }
-  //  }
-  //
-  //  @WXComponentProp(name = Constants.Name.RIGHT_GAP)
-  //  public void setRightGap(float rightGap)  {
-  //    if(mRecyclerDom != null && mRecyclerDom.getRightGap() != mRightGap){
-  //      markComponentUsable();
-  //      mRecyclerDom.preCalculateCellWidth();
-  //      updateRecyclerAttr();
-  //      WXRecyclerView wxRecyclerView = getHostView().getInnerView();
-  //      wxRecyclerView.initView(getContext(), 
mLayoutType,mColumnCount,mColumnGap,getOrientation());
-  //    }
-  //  }
+  @WXComponentProp(name = Constants.Name.SPAN_OFFSETS)
+  public void setSpanOffsets(String spanOffsets)  {
+    if (!TextUtils.equals(spanOffsets, mSpanOffsetsStr)) {
+      markComponentUsable();
+      updateRecyclerAttr();
+      WXRecyclerView wxRecyclerView = getHostView().getInnerView();
+      wxRecyclerView.initView(getContext(), mLayoutType, mColumnCount, 
mColumnGap, getOrientation());
+    }
+  }
 
   @WXComponentProp(name = Constants.Name.COLUMN_WIDTH)
   public void setColumnWidth(int columnWidth)  {
@@ -184,7 +191,7 @@ public class WXListComponent extends 
BasicListComponent<BounceRecyclerView> {
 
   @WXComponentProp(name = Constants.Name.COLUMN_COUNT)
   public void setColumnCount(int columnCount){
-    if(columnCount != mColumnCount){
+    if(columnCount != mColumnCount) {
       markComponentUsable();
       updateRecyclerAttr();
       WXRecyclerView wxRecyclerView = getHostView().getInnerView();
@@ -283,4 +290,8 @@ public class WXListComponent extends 
BasicListComponent<BounceRecyclerView> {
             || 
WXBasicComponentType.RECYCLE_LIST.equals(component.getComponentType())
             || 
WXBasicComponentType.RECYCLER.equals(component.getComponentType());
   }
+
+  public Float[] getSpanOffsets() {
+    return mSpanOffsets;
+  }
 }

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/613d780c/weex_core/Source/core/css/constants_name.h
----------------------------------------------------------------------
diff --git a/weex_core/Source/core/css/constants_name.h 
b/weex_core/Source/core/css/constants_name.h
index 29bcb15..15ec758 100644
--- a/weex_core/Source/core/css/constants_name.h
+++ b/weex_core/Source/core/css/constants_name.h
@@ -73,6 +73,9 @@ namespace WeexCore {
   constexpr char COLUMN_WIDTH[] = "columnWidth";
   constexpr char COLUMN_COUNT[] = "columnCount";
   constexpr char COLUMN_GAP[] = "columnGap";
+  constexpr char LEFT_GAP[] = "leftGap";
+  constexpr char RIGHT_GAP[] = "rightGap";
+  constexpr char SPAN_OFFSETS[] = "spanOffsets";
 
   constexpr char COLOR[] = "color";
   constexpr char BACKGROUND_COLOR[] = "backgroundColor";

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/613d780c/weex_core/Source/core/render/node/render_list.h
----------------------------------------------------------------------
diff --git a/weex_core/Source/core/render/node/render_list.h 
b/weex_core/Source/core/render/node/render_list.h
index abc23cd..2cb0897 100644
--- a/weex_core/Source/core/render/node/render_list.h
+++ b/weex_core/Source/core/render/node/render_list.h
@@ -40,7 +40,9 @@ namespace WeexCore {
     bool mIsSetFlex = false;
     std::vector<RenderObject*> cellSlots;
     std::vector<RenderObject*> cellSlotsCopys;
-      
+    float mLeftGap = 0;
+    float mRightGap = 0;
+
   public:
       ~RenderList(){
 
@@ -128,6 +130,9 @@ namespace WeexCore {
             mColumnWidth = getColumnWidth();
             mColumnGap = getColumnGap();
 
+            mLeftGap = getLeftGap();
+            mRightGap = getRightGap();
+
             mAvailableWidth = getStyleWidth()- 
getWebPxByWidth(getPaddingLeft(), GetRenderPage()->ViewPortWidth()) - 
getWebPxByWidth(getPaddingRight(), GetRenderPage()->ViewPortWidth());
 
             if (AUTO_VALUE == mColumnCount && AUTO_VALUE == mColumnWidth) {
@@ -135,35 +140,56 @@ namespace WeexCore {
                 mColumnWidth = (mAvailableWidth - ((mColumnCount - 1) * 
mColumnGap)) / mColumnCount;
                 mColumnWidth = mColumnWidth > 0 ? mColumnWidth :0;
             } else if (AUTO_VALUE == mColumnWidth && AUTO_VALUE != 
mColumnCount) {
-                mColumnWidth = (mAvailableWidth - ((mColumnCount - 1) * 
mColumnGap)) / mColumnCount;
+                mColumnWidth = (mAvailableWidth - mLeftGap - mRightGap - 
((mColumnCount - 1) * mColumnGap)) / mColumnCount;
                 mColumnWidth = mColumnWidth > 0 ? mColumnWidth :0;
             } else if (AUTO_VALUE != mColumnWidth && AUTO_VALUE == 
mColumnCount) {
-                mColumnCount = round((mAvailableWidth + mColumnGap) / 
(mColumnWidth + mColumnGap)-0.5f);
+                mColumnCount = (int)round((mAvailableWidth + mColumnGap) / 
(mColumnWidth + mColumnGap)-0.5f);
                 mColumnCount = mColumnCount > 0 ? mColumnCount :1;
                 if (mColumnCount <= 0) {
                     mColumnCount = COLUMN_COUNT_NORMAL;
                 }
-                mColumnWidth =((mAvailableWidth + mColumnGap) / mColumnCount) 
- mColumnGap;
+                mColumnWidth =((mAvailableWidth + mColumnGap - mLeftGap - 
mRightGap) / mColumnCount) - mColumnGap;
 
-            } else if(AUTO_VALUE != mColumnWidth && AUTO_VALUE != 
mColumnCount){
-                int columnCount = round((mAvailableWidth + mColumnGap) / 
(mColumnWidth + mColumnGap)-0.5f);
+            } else if(AUTO_VALUE != mColumnWidth && AUTO_VALUE != 
mColumnCount) {
+                int columnCount = (int)round((mAvailableWidth + mColumnGap - 
mLeftGap - mRightGap) / (mColumnWidth + mColumnGap)-0.5f);
                 mColumnCount = columnCount > mColumnCount ? mColumnCount 
:columnCount;
                 if (mColumnCount <= 0) {
                     mColumnCount = COLUMN_COUNT_NORMAL;
                 }
-                mColumnWidth= ((mAvailableWidth + mColumnGap) / mColumnCount) 
- mColumnGap;
+                mColumnWidth= ((mAvailableWidth + mColumnGap - mLeftGap - 
mRightGap) / mColumnCount) - mColumnGap;
             }
 
+            std::string spanOffsets = calcSpanOffset();
+
             mIsPreCalculateCellWidth = true;
-            if(getColumnCount() > 0 || getColumnWidth() > 0 || mColumnCount > 
COLUMN_COUNT_NORMAL){
+            if (getColumnCount() > 0 || getColumnWidth() > 0 || mColumnCount > 
COLUMN_COUNT_NORMAL) {
                 attrs->insert(std::pair<std::string, 
std::string>(COLUMN_COUNT, to_string(mColumnCount)));
                 attrs->insert(std::pair<std::string, std::string>(COLUMN_GAP, 
to_string(mColumnGap)));
                 attrs->insert(std::pair<std::string, 
std::string>(COLUMN_WIDTH, to_string(mColumnWidth)));
             }
+            if (spanOffsets.length() > 0) {
+                attrs->insert(std::pair<std::string, 
std::string>(SPAN_OFFSETS, to_string(spanOffsets)));
+            }
         }
         return attrs;
     }
 
+    std::string calcSpanOffset() {
+        std::string spanOffsets;
+        if (mLeftGap > 0 || mRightGap > 0) {
+            spanOffsets.append("[");
+            for (int i = 0; i < mColumnCount; i++) {
+                float spanOffset = mLeftGap + i * ((mColumnWidth + mColumnGap) 
- (mAvailableWidth + mColumnGap) / mColumnCount);
+                spanOffsets.append(to_string(spanOffset));
+                if(i != mColumnCount - 1) {
+                    spanOffsets.append(",");
+                }
+            }
+            spanOffsets.append("]");
+        }
+        return spanOffsets;
+    }
+
     float getStyleWidth() {
       float width = getWebPxByWidth(getLayoutWidth(), 
GetRenderPage()->ViewPortWidth());
       if (isnan(width) || width <= 0){
@@ -278,6 +304,28 @@ namespace WeexCore {
       return (columnWidthValue > 0 && !isnan(columnWidthValue)) ? 
columnWidthValue : 0;
     }
 
+      float getLeftGap() {
+          std::string leftGap = GetAttr(LEFT_GAP);
+
+          if (leftGap.empty() || leftGap == AUTO) {
+              return 0;
+          }
+
+          float leftGapValue = getFloat(leftGap.c_str());
+          return (leftGapValue > 0 && !isnan(leftGapValue)) ? leftGapValue : 0;
+      }
+
+      float getRightGap() {
+          std::string rightGap = GetAttr(RIGHT_GAP);
+
+          if (rightGap.empty() || rightGap == AUTO) {
+              return 0;
+          }
+
+          float rightGapValue = getFloat(rightGap.c_str());
+          return (rightGapValue > 0 && !isnan(rightGapValue)) ? rightGapValue 
: 0;
+      }
+
     int getOrientation(){
       std::string direction = GetAttr(SCROLL_DIRECTION);
       if(HORIZONTAL == direction){

Reply via email to