Search in sources :

Example 11 with WXCell

use of com.taobao.weex.ui.component.list.WXCell in project incubator-weex by apache.

the class Statements method doBindingAttrsEventAndRenderChildNode.

/**
 * bind attrs and doRender component child
 */
private static void doBindingAttrsEventAndRenderChildNode(WXComponent component, WXDomObject domObject, CellRenderContext context, List<WXComponent> updates) {
    WXAttr attr = component.getDomObject().getAttrs();
    /**
     * sub component supported, sub component new stack
     */
    ArrayStack stack = context.stack;
    if (attr.get(ELUtils.IS_COMPONENT_ROOT) != null && WXUtils.getBoolean(attr.get(ELUtils.IS_COMPONENT_ROOT), false)) {
        if (attr.get(ELUtils.COMPONENT_PROPS) != null && attr.get(ELUtils.COMPONENT_PROPS) instanceof JSONObject) {
            String compoentId = (String) attr.get(CellDataManager.SUB_COMPONENT_TEMPLATE_ID);
            Object compoentData = null;
            if (!TextUtils.isEmpty(compoentId)) {
                String virtualComponentId = context.getRenderState().getVirtualComponentIds().get(component.getViewTreeKey());
                if (virtualComponentId == null) {
                    // none virtualComponentId, create and do attach
                    virtualComponentId = CellDataManager.createVirtualComponentId(context.templateList.getRef(), component.getViewTreeKey(), context.templateList.getItemId(context.position));
                    Map<String, Object> props = renderProps((JSONObject) attr.get(ELUtils.COMPONENT_PROPS), context.stack);
                    EventResult result = WXBridgeManager.getInstance().syncCallJSEventWithResult(WXBridgeManager.METHD_COMPONENT_HOOK_SYNC, component.getInstanceId(), null, compoentId, VirtualComponentLifecycle.LIFECYCLE, VirtualComponentLifecycle.CREATE, new Object[] { virtualComponentId, props }, null);
                    if (result != null && result.getResult() != null && result.getResult() instanceof Map) {
                        props.putAll((Map<? extends String, ?>) result.getResult());
                    }
                    compoentData = props;
                    context.getRenderState().getVirtualComponentIds().put(component.getViewTreeKey(), virtualComponentId);
                    context.templateList.getCellDataManager().createVirtualComponentData(context.position, virtualComponentId, compoentData);
                    // create virtual componentId
                    WXBridgeManager.getInstance().asyncCallJSEventVoidResult(WXBridgeManager.METHD_COMPONENT_HOOK_SYNC, component.getInstanceId(), null, virtualComponentId, VirtualComponentLifecycle.LIFECYCLE, VirtualComponentLifecycle.ATTACH, null);
                } else {
                    // get virtual component data check has dirty's update
                    compoentData = context.getRenderState().getVirtualComponentDatas().get(virtualComponentId);
                    if (context.getRenderState().isHasDataUpdate()) {
                        Map<String, Object> props = renderProps((JSONObject) attr.get(ELUtils.COMPONENT_PROPS), context.stack);
                        EventResult result = WXBridgeManager.getInstance().syncCallJSEventWithResult(WXBridgeManager.METHD_COMPONENT_HOOK_SYNC, component.getInstanceId(), null, virtualComponentId, VirtualComponentLifecycle.LIFECYCLE, VirtualComponentLifecycle.SYNSTATE, new Object[] { virtualComponentId, props }, null);
                        if (result != null && result.getResult() != null && result.getResult() instanceof Map) {
                            props.putAll((Map<? extends String, ?>) result.getResult());
                            context.templateList.getCellDataManager().updateVirtualComponentData(virtualComponentId, props);
                            compoentData = props;
                        }
                    }
                }
                component.getDomObject().getAttrs().put(CellDataManager.VIRTUAL_COMPONENT_ID, virtualComponentId);
            } else {
                // stateless component
                Map<String, Object> props = renderProps((JSONObject) attr.get(ELUtils.COMPONENT_PROPS), context.stack);
                compoentData = props;
            }
            // virtual component is new context
            context.stack = new ArrayStack();
            if (compoentData != null) {
                context.stack.push(compoentData);
            }
        }
    }
    /**
     * check node is render only once, if render once, and has rendered, just return
     */
    Object vonce = null;
    if (attr.getStatement() != null) {
        vonce = attr.getStatement().get(WXStatement.WX_ONCE);
    }
    if (vonce != null) {
        ArrayStack onceStack = context.getRenderState().getOnceComponentStates().get(component.getViewTreeKey());
        if (onceStack == null) {
            onceStack = context.templateList.copyStack(context, stack);
            context.getRenderState().getOnceComponentStates().put(component.getViewTreeKey(), onceStack);
        }
        context.stack = onceStack;
    }
    doRenderBindingAttrsAndEvent(component, domObject, context);
    if (component instanceof WXVContainer) {
        if (!domObject.isShow()) {
            if (!(component instanceof WXCell)) {
                return;
            }
        }
        WXVContainer container = (WXVContainer) component;
        for (int k = 0; k < container.getChildCount(); ) {
            WXComponent next = container.getChild(k);
            k += doRenderComponent(next, context, updates);
        }
    }
    if (stack != context.stack) {
        context.stack = stack;
    }
}
Also used : WXVContainer(com.taobao.weex.ui.component.WXVContainer) EventResult(com.taobao.weex.bridge.EventResult) JSONObject(com.alibaba.fastjson.JSONObject) WXComponent(com.taobao.weex.ui.component.WXComponent) WXDomObject(com.taobao.weex.dom.WXDomObject) JSONObject(com.alibaba.fastjson.JSONObject) ArrayStack(com.taobao.weex.el.parse.ArrayStack) HashMap(java.util.HashMap) ArrayMap(android.support.v4.util.ArrayMap) Map(java.util.Map) WXCell(com.taobao.weex.ui.component.list.WXCell) WXAttr(com.taobao.weex.dom.WXAttr)

Example 12 with WXCell

use of com.taobao.weex.ui.component.list.WXCell in project incubator-weex by apache.

the class Statements method getComponentId.

public static String getComponentId(WXComponent component) {
    if (component instanceof WXCell || component == null) {
        return null;
    }
    WXDomObject domObject = (WXDomObject) component.getDomObject();
    WXAttr attr = domObject.getAttrs();
    if (attr.get(ELUtils.IS_COMPONENT_ROOT) != null && WXUtils.getBoolean(attr.get(ELUtils.IS_COMPONENT_ROOT), false)) {
        if (attr.get(ELUtils.COMPONENT_PROPS) != null && attr.get(ELUtils.COMPONENT_PROPS) instanceof JSONObject) {
            Object componentId = attr.get(CellDataManager.VIRTUAL_COMPONENT_ID);
            if (componentId == null) {
                return null;
            }
            return componentId.toString();
        }
    }
    return getComponentId(component.getParent());
}
Also used : WXDomObject(com.taobao.weex.dom.WXDomObject) JSONObject(com.alibaba.fastjson.JSONObject) WXDomObject(com.taobao.weex.dom.WXDomObject) JSONObject(com.alibaba.fastjson.JSONObject) WXCell(com.taobao.weex.ui.component.list.WXCell) WXAttr(com.taobao.weex.dom.WXAttr)

Example 13 with WXCell

use of com.taobao.weex.ui.component.list.WXCell in project incubator-weex by apache.

the class WXRecyclerTemplateList method calcContentOffset.

public int calcContentOffset(RecyclerView recyclerView) {
    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (layoutManager instanceof LinearLayoutManager) {
        int firstVisibleItemPosition = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
        int offset = 0;
        for (int i = 0; i < firstVisibleItemPosition; i++) {
            WXCell cell = getSourceTemplate(i);
            if (cell == null) {
                continue;
            }
            offset -= cell.getLayoutHeight();
        }
        if (layoutManager instanceof GridLayoutManager) {
            int spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
            offset = offset / spanCount;
        }
        View firstVisibleView = layoutManager.findViewByPosition(firstVisibleItemPosition);
        if (firstVisibleView != null) {
            offset += firstVisibleView.getTop();
        }
        return offset;
    } else if (layoutManager instanceof StaggeredGridLayoutManager) {
        int spanCount = ((StaggeredGridLayoutManager) layoutManager).getSpanCount();
        int firstVisibleItemPosition = ((StaggeredGridLayoutManager) layoutManager).findFirstVisibleItemPositions(null)[0];
        int offset = 0;
        for (int i = 0; i < firstVisibleItemPosition; i++) {
            WXCell cell = getSourceTemplate(i);
            if (cell == null) {
                continue;
            }
            offset -= cell.getLayoutHeight();
        }
        offset = offset / spanCount;
        View firstVisibleView = layoutManager.findViewByPosition(firstVisibleItemPosition);
        if (firstVisibleView != null) {
            offset += firstVisibleView.getTop();
        }
        return offset;
    }
    return -1;
}
Also used : GridLayoutManager(android.support.v7.widget.GridLayoutManager) StaggeredGridLayoutManager(android.support.v7.widget.StaggeredGridLayoutManager) WXRecyclerView(com.taobao.weex.ui.view.listview.WXRecyclerView) RecyclerView(android.support.v7.widget.RecyclerView) BounceRecyclerView(com.taobao.weex.ui.view.refresh.wrapper.BounceRecyclerView) StaggeredGridLayoutManager(android.support.v7.widget.StaggeredGridLayoutManager) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) WXRecyclerView(com.taobao.weex.ui.view.listview.WXRecyclerView) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) BounceRecyclerView(com.taobao.weex.ui.view.refresh.wrapper.BounceRecyclerView) Point(android.graphics.Point) WXCell(com.taobao.weex.ui.component.list.WXCell)

Example 14 with WXCell

use of com.taobao.weex.ui.component.list.WXCell in project incubator-weex by apache.

the class WXRecyclerTemplateList method addChild.

@Override
public void addChild(WXComponent child, int index) {
    /**
     *  dom object in component is not tree, build tree
     */
    if (!(child instanceof WXCell)) {
        super.addChild(child, index);
    }
    if (child instanceof WXBaseRefresh) {
        return;
    }
    if (child instanceof WXCell) {
        if (child.getDomObject() != null && child.getDomObject().getAttrs() != null) {
            Object templateId = child.getDomObject().getAttrs().get(Constants.Name.Recycler.SLOT_TEMPLATE_CASE);
            String key = WXUtils.getString(templateId, null);
            if (getDomObject().getAttrs().containsKey(Constants.Name.Recycler.LIST_DATA_TEMPLATE_SWITCH_KEY)) {
                if (defaultTemplateCell == null) {
                    defaultTemplateCell = (WXCell) child;
                    if (!TextUtils.isEmpty(key)) {
                        defaultTemplateKey = key;
                    } else {
                        key = defaultTemplateKey;
                        child.getDomObject().getAttrs().put(Constants.Name.Recycler.SLOT_TEMPLATE_CASE, key);
                    }
                }
            } else {
                if (defaultTemplateCell == null || child.getDomObject().getAttrs().containsKey(Constants.Name.Recycler.SLOT_TEMPLATE_DEFAULT)) {
                    defaultTemplateCell = (WXCell) child;
                    if (!TextUtils.isEmpty(key)) {
                        defaultTemplateKey = key;
                    } else {
                        key = defaultTemplateKey;
                        child.getDomObject().getAttrs().put(Constants.Name.Recycler.SLOT_TEMPLATE_CASE, key);
                    }
                }
            }
            if (key != null) {
                if (child.getDomObject() instanceof WXCellDomObject && getDomObject() instanceof WXRecyclerDomObject) {
                    WXCellDomObject domObject = (WXCellDomObject) child.getDomObject();
                    domObject.setRecyclerDomObject((WXRecyclerDomObject) getDomObject());
                }
                mTemplateSources.put(key, (WXCell) child);
                renderTemplateCellWithData((WXCell) child);
                if (mTemplateViewTypes.get(key) == null) {
                    mTemplateViewTypes.put(key, mTemplateViewTypes.size());
                }
            }
        }
        notifyUpdateList();
    }
}
Also used : WXBaseRefresh(com.taobao.weex.ui.component.WXBaseRefresh) WXRecyclerDomObject(com.taobao.weex.dom.WXRecyclerDomObject) WXDomObject(com.taobao.weex.dom.WXDomObject) JSONObject(com.alibaba.fastjson.JSONObject) WXCellDomObject(com.taobao.weex.dom.WXCellDomObject) WXRecyclerDomObject(com.taobao.weex.dom.WXRecyclerDomObject) WXCellDomObject(com.taobao.weex.dom.WXCellDomObject) WXCell(com.taobao.weex.ui.component.list.WXCell)

Example 15 with WXCell

use of com.taobao.weex.ui.component.list.WXCell in project incubator-weex by apache.

the class WXRecyclerTemplateList method copyComponentFromSourceCell.

/**
 * copy cell component from source, init render data, and return source
 * if none data, return null
 */
public WXComponent copyComponentFromSourceCell(WXCell cell) {
    renderTemplateCellWithData(cell);
    WXCell component = (WXCell) Statements.copyComponentTree(cell);
    if (component.getDomObject() instanceof WXCellDomObject && getDomObject() instanceof WXRecyclerDomObject) {
        WXCellDomObject domObject = (WXCellDomObject) component.getDomObject();
        domObject.setRecyclerDomObject((WXRecyclerDomObject) getDomObject());
    }
    component.setRenderData(cell.getRenderData());
    return component;
}
Also used : WXRecyclerDomObject(com.taobao.weex.dom.WXRecyclerDomObject) WXCellDomObject(com.taobao.weex.dom.WXCellDomObject) WXCell(com.taobao.weex.ui.component.list.WXCell)

Aggregations

WXCell (com.taobao.weex.ui.component.list.WXCell)19 WXDomObject (com.taobao.weex.dom.WXDomObject)7 WXRecyclerView (com.taobao.weex.ui.view.listview.WXRecyclerView)7 JSONObject (com.alibaba.fastjson.JSONObject)6 Point (android.graphics.Point)5 View (android.view.View)5 WXCellDomObject (com.taobao.weex.dom.WXCellDomObject)5 WXRecyclerDomObject (com.taobao.weex.dom.WXRecyclerDomObject)4 WXComponent (com.taobao.weex.ui.component.WXComponent)4 BounceRecyclerView (com.taobao.weex.ui.view.refresh.wrapper.BounceRecyclerView)4 RecyclerView (android.support.v7.widget.RecyclerView)3 WXAttr (com.taobao.weex.dom.WXAttr)3 ListComponentView (com.taobao.weex.ui.component.list.ListComponentView)3 ArrayMap (android.support.v4.util.ArrayMap)2 ViewGroup (android.view.ViewGroup)2 AppearanceHelper (com.taobao.weex.ui.component.AppearanceHelper)2 WXDiv (com.taobao.weex.ui.component.WXDiv)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2