Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

[WEEX-218][android] support leftGap and rightGap for waterfall recycle-list's orientation support update attrs #1031

Merged
merged 2 commits into from Mar 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -123,6 +123,8 @@ public interface Name {
String COLUMN_WIDTH= "columnWidth";
String COLUMN_COUNT= "columnCount";
String COLUMN_GAP= "columnGap";
String LEFT_GAP= "leftGap";
String RIGHT_GAP= "rightGap";
String SHOW_SCROLLBAR= "showScrollbar";
String OVERFLOW = "overflow";
String TYPE = "type";
Expand Down
Expand Up @@ -25,6 +25,7 @@
import com.taobao.weex.dom.flex.Spacing;
import com.taobao.weex.ui.component.WXBasicComponentType;
import com.taobao.weex.utils.WXLogUtils;
import com.taobao.weex.utils.WXUtils;
import com.taobao.weex.utils.WXViewUtils;

import java.util.ArrayList;
Expand All @@ -45,29 +46,44 @@ public class WXRecyclerDomObject extends WXDomObject{
private float mAvailableWidth = 0;
private boolean mIsPreCalculateCellWidth =false;

private float mLeftGap = 0;
private float mRightGap = 0;

private float[] spanOffsets;

/**cell-slot not on the tree */
private List<WXCellDomObject> cellList;


public float getAvailableWidth() {
return WXViewUtils.getRealPxByWidth(mAvailableWidth,getViewPortWidth());
return WXViewUtils.getRealPxByWidth(mAvailableWidth, getViewPortWidth());
}

public int getLayoutType(){
return getAttrs().getLayoutType();
}

public float getColumnGap() {
return WXViewUtils.getRealPxByWidth(mColumnGap,getViewPortWidth());
return WXViewUtils.getRealPxByWidth(mColumnGap, getViewPortWidth());
}


public int getColumnCount() {
return mColumnCount;
}

public float getColumnWidth() {
return WXViewUtils.getRealPxByWidth(mColumnWidth,getViewPortWidth());
}

public float getLeftGap() {
return WXViewUtils.getRealPxByWidth(mLeftGap,getViewPortWidth());
}

public float getRightGap() {
return WXViewUtils.getRealPxByWidth(mRightGap,getViewPortWidth());
}

@Override
public void add(WXDomObject child, int index) {
if(WXBasicComponentType.CELL_SLOT.equals(child.getType())
Expand Down Expand Up @@ -129,34 +145,40 @@ public float getStyleWidth() {
return width;
}




public void preCalculateCellWidth(){

if (getAttrs() != null) {
mColumnCount = getAttrs().getColumnCount();
mColumnWidth = getAttrs().getColumnWidth();
mColumnGap = getAttrs().getColumnGap();
mLeftGap = WXUtils.getFloat(getAttrs().get(Constants.Name.LEFT_GAP),0.0f);
mRightGap = WXUtils.getFloat(getAttrs().get(Constants.Name.RIGHT_GAP),0.0f);

mAvailableWidth = getStyleWidth()-getPadding().get(Spacing.LEFT)-getPadding().get(Spacing.RIGHT);
mAvailableWidth = WXViewUtils.getWebPxByWidth(mAvailableWidth,getViewPortWidth());

if (Constants.Value.AUTO == mColumnCount && Constants.Value.AUTO == mColumnWidth) {
mColumnCount = Constants.Value.COLUMN_COUNT_NORMAL;
} else if (Constants.Value.AUTO == mColumnWidth && Constants.Value.AUTO != mColumnCount) {
mColumnWidth = (mAvailableWidth - ((mColumnCount - 1) * mColumnGap)) / mColumnCount;
mColumnWidth = (mAvailableWidth - mLeftGap - mRightGap - ((mColumnCount - 1) * mColumnGap)) / mColumnCount;
mColumnWidth = mColumnWidth > 0 ? mColumnWidth :0;
} else if (Constants.Value.AUTO != mColumnWidth && Constants.Value.AUTO == mColumnCount) {
mColumnCount = Math.round((mAvailableWidth + mColumnGap) / (mColumnWidth + mColumnGap)-0.5f);
mColumnCount = mColumnCount > 0 ? mColumnCount :1;
if (mColumnCount <= 0)
mColumnCount = Constants.Value.COLUMN_COUNT_NORMAL;
mColumnWidth =((mAvailableWidth + mColumnGap) / mColumnCount) - mColumnGap;
mColumnWidth =((mAvailableWidth + mColumnGap - mLeftGap - mRightGap) / mColumnCount) - mColumnGap;
} else if(Constants.Value.AUTO != mColumnWidth && Constants.Value.AUTO != mColumnCount){
int columnCount = Math.round((mAvailableWidth + mColumnGap) / (mColumnWidth + mColumnGap)-0.5f);
int columnCount = Math.round((mAvailableWidth + mColumnGap - mLeftGap - mRightGap) / (mColumnWidth + mColumnGap)-0.5f);
mColumnCount = columnCount > mColumnCount ? mColumnCount :columnCount;
if (mColumnCount <= 0)
mColumnCount = Constants.Value.COLUMN_COUNT_NORMAL;
mColumnWidth= ((mAvailableWidth + mColumnGap) / mColumnCount) - mColumnGap;
mColumnWidth = ((mAvailableWidth + mColumnGap - mLeftGap - mRightGap) / mColumnCount) - mColumnGap;
}
calcSpanOffset();
mIsPreCalculateCellWidth = true;
if(WXEnvironment.isApkDebugable()) {
WXLogUtils.d("preCalculateCellWidth mColumnGap :" + mColumnGap + " mColumnWidth:" + mColumnWidth + " mColumnCount:" + mColumnCount);
Expand Down Expand Up @@ -238,6 +260,21 @@ public WXDomObject clone() {
return domObject;
}

public void calcSpanOffset(){
if(mLeftGap > 0 || mRightGap > 0){
if(spanOffsets == null || spanOffsets.length != mColumnCount){
spanOffsets = new float[mColumnCount];
}
for(int i=0; i<mColumnCount; i++){
spanOffsets[i] = mLeftGap + i*((mColumnWidth + mColumnGap) - (mAvailableWidth + mColumnGap)/mColumnCount);
}
}
}

public float[] getSpanOffsets() {
return spanOffsets;
}

public List<WXCellDomObject> getCellList() {
return cellList;
}
Expand Down
Expand Up @@ -114,6 +114,8 @@ public abstract class BasicListComponent<T extends ViewGroup & ListComponentView
protected int mColumnCount = 1;
protected float mColumnGap = 0;
protected float mColumnWidth = 0;
protected float mLeftGap = 0;
protected float mRightGap = 0;

private int mOffsetAccuracy = 10;
private Point mLastReport = new Point(-1, -1);
Expand Down
@@ -0,0 +1,71 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.taobao.weex.ui.component.list;

import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

import com.taobao.weex.dom.WXRecyclerDomObject;
import com.taobao.weex.ui.component.WXComponent;
import com.taobao.weex.utils.WXViewUtils;

/**
* Created by furture on 2018/2/12.
* recyclerview's height is layout width + column gap,
* so the the offset should include, leftGap and rightGap is via this
*/
public class GapItemDecoration extends RecyclerView.ItemDecoration {

private WXListComponent listComponent;

public GapItemDecoration(WXListComponent listComponent) {
this.listComponent = listComponent;
}

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
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;
}
}
}
}
Expand Up @@ -87,6 +87,9 @@ protected BounceRecyclerView generateListView(Context context, int orientation)
bounceRecyclerView.getSwipeLayout().setNestedScrollingEnabled(true);
}
}
if(mRecyclerDom != null && mRecyclerDom.getSpanOffsets() != null){
bounceRecyclerView.getInnerView().addItemDecoration(new GapItemDecoration(this));
}
return bounceRecyclerView;
}

Expand Down Expand Up @@ -135,6 +138,31 @@ private void updateRecyclerAttr(){
mColumnWidth = mRecyclerDom.getColumnWidth();
mPaddingLeft = mRecyclerDom.getPadding().get(Spacing.LEFT);
mPaddingRight = mRecyclerDom.getPadding().get(Spacing.RIGHT);
mLeftGap = mRecyclerDom.getLeftGap();
mRightGap = mRecyclerDom.getRightGap();
mRecyclerDom.preCalculateCellWidth();
}
}

@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());
}
}

Expand Down Expand Up @@ -223,6 +251,10 @@ public void run() {
}
}

public WXRecyclerDomObject getRecyclerDom() {
return mRecyclerDom;
}

public void remove(WXComponent child, boolean destroy) {
super.remove(child, destroy);
if (child instanceof WXLoading) {
Expand All @@ -231,4 +263,6 @@ public void remove(WXComponent child, boolean destroy) {
getHostView().removeHeaderView(child);
}
}


}
Expand Up @@ -726,6 +726,11 @@ protected boolean setProperty(String key, Object param) {
boolean scrollable = WXUtils.getBoolean(param, true);
setScrollable(scrollable);
return true;
case Constants.Name.SCROLL_DIRECTION:
if(param != null) {
setScrollDirection(param.toString());
}
return true;
case Constants.Name.SHOW_SCROLLBAR:
Boolean result = WXUtils.getBoolean(param,null);
if (result != null)
Expand Down Expand Up @@ -757,6 +762,19 @@ private void updateRecyclerAttr(){
mColumnWidth = mDomObject.getColumnWidth();
mPaddingLeft =mDomObject.getPadding().get(Spacing.LEFT);
mPaddingRight =mDomObject.getPadding().get(Spacing.RIGHT);

}


@WXComponentProp(name = Constants.Name.SCROLL_DIRECTION)
public void setScrollDirection(String direction){
if(orientation != mDomObject.getOrientation()) {
orientation = mDomObject.getOrientation();
updateRecyclerAttr();
WXRecyclerView wxRecyclerView = getHostView().getInnerView();
wxRecyclerView.initView(getContext(), mLayoutType,mColumnCount,mColumnGap, getOrientation());

}
}

@WXComponentProp(name = Constants.Name.COLUMN_WIDTH)
Expand Down