Java Code Examples for com.alibaba.android.vlayout.VirtualLayoutManager#getLayoutHelpers()

The following examples show how to use com.alibaba.android.vlayout.VirtualLayoutManager#getLayoutHelpers() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TangramEngine.java    From Tangram-Android with MIT License 6 votes vote down vote up
/**
 * @param insertIdx the index to be inserted
 * @param groups a group list
 * @since 2.1.0
 */
public void insertBatchWith(int insertIdx, List<Card> groups) {
    VirtualLayoutManager layoutManager = getLayoutManager();
    if (groups != null && groups.size() > 0 && mGroupBasicAdapter != null && layoutManager != null) {
        List<LayoutHelper> layoutHelpers = layoutManager.getLayoutHelpers();
        final List<LayoutHelper> newLayoutHelpers = new ArrayList<>(layoutHelpers);
        List<LayoutHelper> insertedLayoutHelpers = new ArrayList<>();
        for (int i = 0, size = groups.size(); i < size; i++) {
            insertedLayoutHelpers.add(groups.get(i).getLayoutHelper());
        }
        if (insertIdx >= layoutHelpers.size()) {
            newLayoutHelpers.addAll(insertedLayoutHelpers);
        } else {
            newLayoutHelpers.addAll(insertIdx, insertedLayoutHelpers);
        }
        layoutManager.setLayoutHelpers(newLayoutHelpers);
        mGroupBasicAdapter.insertBatchComponents(insertIdx, groups);
    }
}
 
Example 2
Source File: TangramEngine.java    From Tangram-Android with MIT License 6 votes vote down vote up
/**
 * Replace card one by one. New one's children size should be equal with old one's children size.
 * @param oldOne
 * @param newOne
 * @since 2.1.0
 */
public void replace(Card oldOne, Card newOne) {
    VirtualLayoutManager layoutManager = getLayoutManager();
    if (oldOne != null && newOne != null && mGroupBasicAdapter != null && layoutManager != null) {
        List<LayoutHelper> layoutHelpers = layoutManager.getLayoutHelpers();
        int cardIdx = mGroupBasicAdapter.findCardIdxForCard(oldOne);
        if (layoutHelpers != null && cardIdx >= 0 && cardIdx < layoutHelpers.size()) {
            final List<LayoutHelper> newLayoutHelpers = new LinkedList<>();
            for (int i = 0, size = layoutHelpers.size(); i < size; i++) {
                LayoutHelper layoutHelper = layoutHelpers.get(i);
                if (i == cardIdx) {
                    layoutHelper = newOne.getLayoutHelper();
                }
                newLayoutHelpers.add(layoutHelper);
            }
            layoutManager.setLayoutHelpers(newLayoutHelpers);
            mGroupBasicAdapter.replaceComponent(oldOne, newOne);
        }
    }
}
 
Example 3
Source File: TangramEngine.java    From Tangram-Android with MIT License 6 votes vote down vote up
/**
 * @param insertIdx the index to be inserted
 * @param groups    a group list
 * @since 2.1.0
 */
public void insertBatchWith(int insertIdx, List<Card> groups) {
    VirtualLayoutManager layoutManager = getLayoutManager();
    if (groups != null && groups.size() > 0 && mGroupBasicAdapter != null && layoutManager != null) {
        List<LayoutHelper> layoutHelpers = layoutManager.getLayoutHelpers();
        final List<LayoutHelper> newLayoutHelpers = new ArrayList<>(layoutHelpers);
        List<LayoutHelper> insertedLayoutHelpers = new ArrayList<>();
        for (int i = 0, size = groups.size(); i < size; i++) {
            insertedLayoutHelpers.add(groups.get(i).getLayoutHelper());
        }
        if (insertIdx >= layoutHelpers.size()) {
            newLayoutHelpers.addAll(insertedLayoutHelpers);
        } else {
            newLayoutHelpers.addAll(insertIdx, insertedLayoutHelpers);
        }
        layoutManager.setLayoutHelpers(newLayoutHelpers);
        mGroupBasicAdapter.insertBatchComponents(insertIdx, groups);
    }
}
 
Example 4
Source File: TangramEngine.java    From Tangram-Android with MIT License 6 votes vote down vote up
/**
 * Replace card one by one. New one's children size should be equal with old one's children size.
 *
 * @param oldOne
 * @param newOne
 * @since 2.1.0
 */
public void replace(Card oldOne, Card newOne) {
    VirtualLayoutManager layoutManager = getLayoutManager();
    if (oldOne != null && newOne != null && mGroupBasicAdapter != null && layoutManager != null) {
        List<LayoutHelper> layoutHelpers = layoutManager.getLayoutHelpers();
        int cardIdx = mGroupBasicAdapter.findCardIdxForCard(oldOne);
        if (layoutHelpers != null && cardIdx >= 0 && cardIdx < layoutHelpers.size()) {
            final List<LayoutHelper> newLayoutHelpers = new LinkedList<>();
            for (int i = 0, size = layoutHelpers.size(); i < size; i++) {
                LayoutHelper layoutHelper = layoutHelpers.get(i);
                if (i == cardIdx) {
                    layoutHelper = newOne.getLayoutHelper();
                }
                newLayoutHelpers.add(layoutHelper);
            }
            layoutManager.setLayoutHelpers(newLayoutHelpers);
            mGroupBasicAdapter.replaceComponent(oldOne, newOne);
        }
    }
}
 
Example 5
Source File: TangramEngine.java    From Tangram-Android with MIT License 5 votes vote down vote up
/**
 * Remove target cell. TODO handle nested card, cell in staggered, cell in onePlusN
 * @param data
 * @since 2.1.0
 */
protected 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);
            }
        }
    }
}
 
Example 6
Source File: TangramEngine.java    From Tangram-Android with MIT License 5 votes vote down vote up
/**
 * Remove all cells in a card.
 * @param group
 * @since 2.1.0
 */
protected void removeBatchBy(Card group) {
    VirtualLayoutManager layoutManager = getLayoutManager();
    if (group != null && mGroupBasicAdapter != null && layoutManager != null) {
        int cardIdx = mGroupBasicAdapter.findCardIdxForCard(group);
        List<LayoutHelper> layoutHelpers = layoutManager.getLayoutHelpers();
        LayoutHelper emptyLayoutHelper = null;
        int removeItemCount = 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) {
                    removeItemCount = layoutHelper.getItemCount();
                    emptyLayoutHelper = layoutHelper;
                } else {
                    layoutHelper.setRange(start - removeItemCount, end - removeItemCount);
                }
            }
            if (emptyLayoutHelper != null) {
                final List<LayoutHelper> newLayoutHelpers = new LinkedList<>(layoutHelpers);
                newLayoutHelpers.remove(emptyLayoutHelper);
                layoutManager.setLayoutHelpers(newLayoutHelpers);
            }
            mGroupBasicAdapter.removeComponents(group);
        }
    }
}
 
Example 7
Source File: TangramEngine.java    From Tangram-Android with MIT License 5 votes vote down vote up
/**
 * 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);
            }
        }
    }
}
 
Example 8
Source File: TangramEngine.java    From Tangram-Android with MIT License 5 votes vote down vote up
/**
 * 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);
            }
        }
    }
}
 
Example 9
Source File: TangramEngine.java    From Tangram-Android with MIT License 5 votes vote down vote up
/**
 * Remove all cells in a card.
 *
 * @param group
 * @since 2.1.0
 */
public void removeBatchBy(Card group) {
    VirtualLayoutManager layoutManager = getLayoutManager();
    if (group != null && mGroupBasicAdapter != null && layoutManager != null) {
        int cardIdx = mGroupBasicAdapter.findCardIdxForCard(group);
        List<LayoutHelper> layoutHelpers = layoutManager.getLayoutHelpers();
        LayoutHelper emptyLayoutHelper = null;
        int removeItemCount = 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) {
                    removeItemCount = layoutHelper.getItemCount();
                    emptyLayoutHelper = layoutHelper;
                } else {
                    layoutHelper.setRange(start - removeItemCount, end - removeItemCount);
                }
            }
            if (emptyLayoutHelper != null) {
                final List<LayoutHelper> newLayoutHelpers = new LinkedList<>(layoutHelpers);
                newLayoutHelpers.remove(emptyLayoutHelper);
                layoutManager.setLayoutHelpers(newLayoutHelpers);
            }
            mGroupBasicAdapter.removeComponents(group);
        }
    }
}
 
Example 10
Source File: TangramEngine.java    From Tangram-Android with MIT License 5 votes vote down vote up
/**
 * 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);
            }
        }
    }
}