use of com.tmall.wireless.tangram3.structure.BaseCell 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);
}
}
}
}
use of com.tmall.wireless.tangram3.structure.BaseCell 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);
}
}
}
}
use of com.tmall.wireless.tangram3.structure.BaseCell 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));
}
}
}
use of com.tmall.wireless.tangram3.structure.BaseCell in project Tangram-Android by alibaba.
the class MVHelper method isValid.
public boolean isValid(BaseCell cell, ServiceManager serviceManager) {
boolean ret = cell.isValid();
if (serviceManager != null) {
BaseCellBinderResolver componentBinderResolver = serviceManager.getService(BaseCellBinderResolver.class);
ret = ret && (componentBinderResolver.has(cell.stringType) || cell.componentInfo != null && renderManager.getRenderService(cell.componentInfo.getType()) != null);
CellSupport cellSupport = serviceManager.getService(CellSupport.class);
if (cellSupport != null) {
ret = cellSupport.isValid(cell) && ret;
}
}
return ret;
}
use of com.tmall.wireless.tangram3.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);
}
}
Aggregations