Search in sources :

Example 11 with BaseCell

use of com.tmall.wireless.tangram.structure.BaseCell in project Tangram-Android by alibaba.

the class TangramEngine method topPosition.

/**
 * {@inheritDoc}
 */
@Override
public void topPosition(Card card) {
    List<BaseCell> cells = card.getCells();
    if (cells.size() > 0) {
        BaseCell cell = cells.get(0);
        int pos = mGroupBasicAdapter.getComponents().indexOf(cell);
        if (pos > 0) {
            VirtualLayoutManager lm = getLayoutManager();
            View view = lm.findViewByPosition(pos);
            if (view != null) {
                int top = lm.getDecoratedTop(view);
                this.getContentView().scrollBy(0, top);
            } else
                this.getContentView().scrollToPosition(pos);
        }
    }
}
Also used : BaseCell(com.tmall.wireless.tangram.structure.BaseCell) VirtualLayoutManager(com.alibaba.android.vlayout.VirtualLayoutManager) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView)

Example 12 with BaseCell

use of com.tmall.wireless.tangram.structure.BaseCell in project Tangram-Android by alibaba.

the class TangramEngine method insertWith.

/**
 * NOTE new API
 * A high performance method to insert cells. TODO handle nested card
 * @param insertPosition the position to be inserted
 * @param list new cell data list
 */
public void insertWith(int insertPosition, List<BaseCell> list) {
    int newItemSize = list != null ? list.size() : 0;
    if (newItemSize > 0 && mGroupBasicAdapter != null) {
        if (insertPosition >= mGroupBasicAdapter.getItemCount()) {
            insertPosition = mGroupBasicAdapter.getItemCount() - 1;
        }
        BaseCell insertCell = mGroupBasicAdapter.getItemByPosition(insertPosition);
        int cardIdx = mGroupBasicAdapter.findCardIdxFor(insertPosition);
        Card card = mGroupBasicAdapter.getCardRange(cardIdx).second;
        card.addCells(card, card.getCells().indexOf(insertCell), list);
        List<LayoutHelper> layoutHelpers = getLayoutManager().getLayoutHelpers();
        if (layoutHelpers != null && cardIdx >= 0 && cardIdx < layoutHelpers.size()) {
            for (int i = 0, size = layoutHelpers.size(); i < size; i++) {
                LayoutHelper layoutHelper = layoutHelpers.get(i);
                int start = layoutHelper.getRange().getLower();
                int end = layoutHelper.getRange().getUpper();
                if (end < insertPosition) {
                // do nothing
                } else if (start <= insertPosition && insertPosition <= end) {
                    layoutHelper.setItemCount(layoutHelper.getItemCount() + newItemSize);
                    layoutHelper.setRange(start, end + newItemSize);
                } else if (insertPosition < start) {
                    layoutHelper.setRange(start + newItemSize, end + newItemSize);
                }
            }
            mGroupBasicAdapter.insertComponents(insertPosition, list);
        }
    }
}
Also used : BaseCell(com.tmall.wireless.tangram.structure.BaseCell) LayoutHelper(com.alibaba.android.vlayout.LayoutHelper) Card(com.tmall.wireless.tangram.dataparser.concrete.Card)

Example 13 with BaseCell

use of com.tmall.wireless.tangram.structure.BaseCell in project Tangram-Android by alibaba.

the class TangramEngine method removeBy.

/**
 * NOTE new API
 * Remove cell at target position. TODO handle nested card
 * @param position
 */
protected void removeBy(int position) {
    if (mGroupBasicAdapter != null) {
        if (position < mGroupBasicAdapter.getItemCount() && position >= 0) {
            BaseCell removeCell = mGroupBasicAdapter.getItemByPosition(position);
            removeBy(removeCell);
        }
    }
}
Also used : BaseCell(com.tmall.wireless.tangram.structure.BaseCell)

Example 14 with BaseCell

use of com.tmall.wireless.tangram.structure.BaseCell in project Tangram-Android by alibaba.

the class Card method createCell.

protected BaseCell createCell(@NonNull MVHelper resolver, @Nullable JSONObject cellData, boolean appended) {
    if (cellData != null) {
        BaseCell cell = null;
        String cellType = cellData.optString(Card.KEY_TYPE);
        if ((getMVHelper() != null && getMVHelper().resolver().getViewClass(cellType) != null) || Utils.isCard(cellData)) {
            if (resolver.resolver().isCompatibleType(cellType)) {
                cell = Utils.newInstance(resolver.resolver().getCellClass(cellType));
                // do not display when newInstance failed
                if (cell == null)
                    return null;
                // ensure service manager
                cell.serviceManager = serviceManager;
            } else {
                if (Utils.isCard(cellData)) {
                    switch(cellType) {
                        case TangramBuilder.TYPE_CONTAINER_FLOW:
                        case TangramBuilder.TYPE_CONTAINER_1C_FLOW:
                        case TangramBuilder.TYPE_CONTAINER_2C_FLOW:
                        case TangramBuilder.TYPE_CONTAINER_3C_FLOW:
                        case TangramBuilder.TYPE_CONTAINER_4C_FLOW:
                        case TangramBuilder.TYPE_CONTAINER_5C_FLOW:
                            CardResolver cardResolver = serviceManager.getService(CardResolver.class);
                            Card gridCard = cardResolver.create(cellType);
                            gridCard.serviceManager = serviceManager;
                            gridCard.parseWith(cellData, resolver);
                            addChildCard(gridCard);
                            break;
                        case TangramBuilder.TYPE_CONTAINER_BANNER:
                            cell = new BannerEntityCard();
                            break;
                        case TangramBuilder.TYPE_CONTAINER_SCROLL:
                            cell = new LinearScrollEntityCard();
                            break;
                    }
                    if (cell != null) {
                        cell.serviceManager = serviceManager;
                        cell.parent = this;
                        cell.parentId = id;
                    } else {
                        return null;
                    }
                } else {
                    cell = new BaseCell(cellType);
                    cell.serviceManager = serviceManager;
                    cell.parent = this;
                    cell.parentId = id;
                }
            }
            parseCell(resolver, cellData, cell, appended);
            cell.setStringType(cellType);
            return cell;
        } else {
            // support virtual view at layout
            BaseCellBinderResolver componentBinderResolver = serviceManager.getService(BaseCellBinderResolver.class);
            if (componentBinderResolver.has(cellType)) {
                cell = new BaseCell(cellType);
                cell.serviceManager = serviceManager;
                cell.parent = this;
                cell.parentId = id;
                parseCell(resolver, cellData, cell, appended);
                cell.setStringType(cellType);
                return cell;
            } else {
                return null;
            }
        }
    }
    return null;
}
Also used : BaseCell(com.tmall.wireless.tangram.structure.BaseCell) BannerEntityCard(com.tmall.wireless.tangram.structure.entitycard.BannerEntityCard) LinearScrollEntityCard(com.tmall.wireless.tangram.structure.entitycard.LinearScrollEntityCard) LinearScrollEntityCard(com.tmall.wireless.tangram.structure.entitycard.LinearScrollEntityCard) BannerEntityCard(com.tmall.wireless.tangram.structure.entitycard.BannerEntityCard)

Example 15 with BaseCell

use of com.tmall.wireless.tangram.structure.BaseCell in project Tangram-Android by alibaba.

the class Card method setCells.

public void setCells(@Nullable List<BaseCell> cells) {
    if (mPlaceholderCell != null)
        this.mCells.remove(mPlaceholderCell);
    oldMap.clear();
    pendingDeleteMap.clear();
    for (BaseCell cell : this.mCells) {
        oldMap.put(System.identityHashCode(cell), cell);
    }
    this.mCells.clear();
    if (cells != null) {
        for (BaseCell c : cells) {
            // noinspection unchecked
            this.addCellInternal(c, true);
        }
    }
    adjustPendingCells(true);
    newMap.clear();
    for (BaseCell cell : this.mCells) {
        newMap.put(System.identityHashCode(cell), cell);
    }
    for (int i = 0, size = oldMap.size(); i < size; i++) {
        int key = oldMap.keyAt(i);
        if (newMap.get(key) != null) {
            newMap.remove(key);
            pendingDeleteMap.put(key, true);
        }
    }
    for (int i = 0, size = pendingDeleteMap.size(); i < size; i++) {
        oldMap.remove(pendingDeleteMap.keyAt(i));
    }
    diffCells(newMap, oldMap);
    newMap.clear();
    oldMap.clear();
    pendingDeleteMap.clear();
    if (requirePlaceholderCell()) {
        this.mCells.add(mPlaceholderCell);
    }
}
Also used : BaseCell(com.tmall.wireless.tangram.structure.BaseCell)

Aggregations

BaseCell (com.tmall.wireless.tangram.structure.BaseCell)26 JSONObject (org.json.JSONObject)5 Card (com.tmall.wireless.tangram.dataparser.concrete.Card)3 JSONArray (org.json.JSONArray)3 JSONException (org.json.JSONException)3 Nullable (android.support.annotation.Nullable)2 View (android.view.View)2 LayoutHelper (com.alibaba.android.vlayout.LayoutHelper)2 VirtualLayoutManager (com.alibaba.android.vlayout.VirtualLayoutManager)2 MVHelper (com.tmall.wireless.tangram.MVHelper)2 BaseCellBinderResolver (com.tmall.wireless.tangram.dataparser.concrete.BaseCellBinderResolver)2 BannerEntityCard (com.tmall.wireless.tangram.structure.entitycard.BannerEntityCard)2 LinearScrollEntityCard (com.tmall.wireless.tangram.structure.entitycard.LinearScrollEntityCard)2 ArrayList (java.util.ArrayList)2 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 RecyclerView (android.support.v7.widget.RecyclerView)1 ViewGroup (android.view.ViewGroup)1 Range (com.alibaba.android.vlayout.Range)1 OnePlusNLayoutHelper (com.alibaba.android.vlayout.layout.OnePlusNLayoutHelper)1 Engine (com.tmall.wireless.tangram.Engine)1