Search in sources :

Example 6 with Card

use of com.tmall.wireless.tangram3.dataparser.concrete.Card in project Tangram-Android by alibaba.

the class TangramEngine method insertWith.

/**
 * A high performance method to insert cells. Do not allowed to insert to an empty Tangram. TODO handle nested card
 *
 * @param insertPosition the position to be inserted
 * @param list           new cell data list
 * @since 2.1.0
 */
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.tangram3.structure.BaseCell) LayoutHelper(com.alibaba.android.vlayout.LayoutHelper) Card(com.tmall.wireless.tangram3.dataparser.concrete.Card)

Example 7 with Card

use of com.tmall.wireless.tangram3.dataparser.concrete.Card in project Tangram-Android by alibaba.

the class TangramEngine method loadMoreCard.

public void loadMoreCard() {
    CardLoadSupport loadSupport = getService(CardLoadSupport.class);
    if (loadSupport == null) {
        return;
    }
    List<Card> groups = findGroups(new Predicate<Card>() {

        @Override
        public boolean isMatch(Card data) {
            return data.loadMore && data.hasMore && !data.loading && !TextUtils.isEmpty(data.load);
        }
    });
    if (groups.size() != 0) {
        loadSupport.loadMore(groups.get(groups.size() - 1));
        loadSupport.reactiveDoLoadMore(groups.get(groups.size() - 1));
    }
}
Also used : CardLoadSupport(com.tmall.wireless.tangram3.support.async.CardLoadSupport) Card(com.tmall.wireless.tangram3.dataparser.concrete.Card)

Example 8 with Card

use of com.tmall.wireless.tangram3.dataparser.concrete.Card in project Tangram-Android by alibaba.

the class TangramEngine method removeBy.

/**
 * Remove target cell. TODO handle nested card, cell in staggered, cell in onePlusN
 *
 * @param data
 * @since 2.1.0
 */
public void removeBy(BaseCell data) {
    VirtualLayoutManager layoutManager = getLayoutManager();
    if (data != null && mGroupBasicAdapter != null && layoutManager != null) {
        int removePosition = mGroupBasicAdapter.getPositionByItem(data);
        if (removePosition >= 0) {
            int cardIdx = mGroupBasicAdapter.findCardIdxFor(removePosition);
            Card card = mGroupBasicAdapter.getCardRange(cardIdx).second;
            card.removeCellSilently(data);
            List<LayoutHelper> layoutHelpers = layoutManager.getLayoutHelpers();
            LayoutHelper emptyLayoutHelper = null;
            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 < removePosition) {
                    // do nothing
                    } else if (start <= removePosition && removePosition <= end) {
                        int itemCount = layoutHelper.getItemCount() - 1;
                        if (itemCount > 0) {
                            layoutHelper.setItemCount(itemCount);
                            layoutHelper.setRange(start, end - 1);
                        } else {
                            emptyLayoutHelper = layoutHelper;
                        }
                    } else if (removePosition < start) {
                        layoutHelper.setRange(start - 1, end - 1);
                    }
                }
                if (emptyLayoutHelper != null) {
                    final List<LayoutHelper> newLayoutHelpers = new LinkedList<>(layoutHelpers);
                    newLayoutHelpers.remove(emptyLayoutHelper);
                    layoutManager.setLayoutHelpers(newLayoutHelpers);
                }
                mGroupBasicAdapter.removeComponent(data);
            }
        }
    }
}
Also used : LayoutHelper(com.alibaba.android.vlayout.LayoutHelper) VirtualLayoutManager(com.alibaba.android.vlayout.VirtualLayoutManager) LinkedList(java.util.LinkedList) Card(com.tmall.wireless.tangram3.dataparser.concrete.Card)

Example 9 with Card

use of com.tmall.wireless.tangram3.dataparser.concrete.Card in project Tangram-Android by alibaba.

the class TangramEngine method replace.

/**
 * Replace parent card's children. Cells' size should be equal with parent's children size.
 *
 * @param parent
 * @param cells
 * @since 2.1.0
 */
public void replace(Card parent, List<BaseCell> cells) {
    VirtualLayoutManager layoutManager = getLayoutManager();
    if (parent != null && cells != null && cells.size() > 0 && mGroupBasicAdapter != null && layoutManager != null) {
        Card card = parent;
        List<BaseCell> oldChildren = new ArrayList<>(parent.getCells());
        if (oldChildren.size() == cells.size()) {
            card.setCells(cells);
            mGroupBasicAdapter.replaceComponent(oldChildren, cells);
        } else {
            List<LayoutHelper> layoutHelpers = layoutManager.getLayoutHelpers();
            int cardIdx = mGroupBasicAdapter.findCardIdxForCard(parent);
            int diff = 0;
            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 (i < cardIdx) {
                    // do nothing
                    } else if (i == cardIdx) {
                        diff = cells.size() - layoutHelper.getItemCount();
                        layoutHelper.setItemCount(cells.size());
                        layoutHelper.setRange(start, end + diff);
                    } else {
                        layoutHelper.setRange(start + diff, end + diff);
                    }
                }
                card.setCells(cells);
                mGroupBasicAdapter.replaceComponent(oldChildren, cells);
            }
        }
    }
}
Also used : BaseCell(com.tmall.wireless.tangram3.structure.BaseCell) ArrayList(java.util.ArrayList) LayoutHelper(com.alibaba.android.vlayout.LayoutHelper) VirtualLayoutManager(com.alibaba.android.vlayout.VirtualLayoutManager) Card(com.tmall.wireless.tangram3.dataparser.concrete.Card)

Example 10 with Card

use of com.tmall.wireless.tangram3.dataparser.concrete.Card in project Tangram-Android by alibaba.

the class TangramEngine method replace.

/**
 * Replace cell one by one.
 *
 * @param oldOne
 * @param newOne
 * @since 2.1.0
 */
public void replace(BaseCell oldOne, BaseCell newOne) {
    VirtualLayoutManager layoutManager = getLayoutManager();
    if (oldOne != null && newOne != null && mGroupBasicAdapter != null && layoutManager != null) {
        int replacePosition = mGroupBasicAdapter.getPositionByItem(oldOne);
        if (replacePosition >= 0) {
            int cardIdx = mGroupBasicAdapter.findCardIdxFor(replacePosition);
            Card card = mGroupBasicAdapter.getCardRange(cardIdx).second;
            card.replaceCell(oldOne, newOne);
            mGroupBasicAdapter.replaceComponent(Arrays.asList(oldOne), Arrays.asList(newOne));
        }
    }
}
Also used : VirtualLayoutManager(com.alibaba.android.vlayout.VirtualLayoutManager) Card(com.tmall.wireless.tangram3.dataparser.concrete.Card)

Aggregations

BaseCell (com.tmall.wireless.tangram3.structure.BaseCell)15 Card (com.tmall.wireless.tangram3.dataparser.concrete.Card)11 Range (com.alibaba.android.vlayout.Range)6 ArrayList (java.util.ArrayList)6 NonNull (android.support.annotation.NonNull)5 GridCard (com.tmall.wireless.tangram3.structure.card.GridCard)5 OnePlusNCard (com.tmall.wireless.tangram3.structure.card.OnePlusNCard)5 RecyclerView (android.support.v7.widget.RecyclerView)4 VirtualLayoutManager (com.alibaba.android.vlayout.VirtualLayoutManager)4 JSONObject (com.alibaba.fastjson.JSONObject)4 View (android.view.View)3 LayoutHelper (com.alibaba.android.vlayout.LayoutHelper)3 JSONArray (com.alibaba.fastjson.JSONArray)3 BannerCard (com.tmall.wireless.tangram3.structure.card.BannerCard)3 FixCard (com.tmall.wireless.tangram3.structure.card.FixCard)3 FixLinearScrollCard (com.tmall.wireless.tangram3.structure.card.FixLinearScrollCard)3 LinearScrollCard (com.tmall.wireless.tangram3.structure.card.LinearScrollCard)3 SlideCard (com.tmall.wireless.tangram3.structure.card.SlideCard)3 StaggeredCard (com.tmall.wireless.tangram3.structure.card.StaggeredCard)3 StickyCard (com.tmall.wireless.tangram3.structure.card.StickyCard)3