use of com.tmall.wireless.tangram3.structure.BaseCell in project Tangram-Android by alibaba.
the class PojoGroupBasicAdapter method replaceComponent.
@Override
public void replaceComponent(List<BaseCell> oldComponent, List<BaseCell> newComponent) {
if (mData != null && oldComponent != null && newComponent != null && oldComponent.size() > 0 && newComponent.size() > 0) {
int index = mData.indexOf(oldComponent.get(0));
if (index >= 0) {
if (mCards != null) {
List<Pair<Range<Integer>, Card>> newCards = new ArrayList<>();
int diff = 0;
for (int i = 0, size = mCards.size(); i < size; i++) {
Pair<Range<Integer>, Card> pair = mCards.get(i);
int start = pair.first.getLower();
int end = pair.first.getUpper();
if (end <= index) {
// do nothing
newCards.add(pair);
} else if (start <= index && index < end) {
diff = newComponent.size() - oldComponent.size();
Pair<Range<Integer>, Card> newPair = new Pair<>(Range.create(start, end + diff), pair.second);
newCards.add(newPair);
} else if (index <= start) {
Pair<Range<Integer>, Card> newPair = new Pair<>(Range.create(start + diff, end + diff), pair.second);
newCards.add(newPair);
}
}
mCards.clear();
mCards.addAll(newCards);
}
for (int i = 0, size = oldComponent.size(); i < size; i++) {
BaseCell cell = oldComponent.get(i);
if (cell != null) {
cell.removed();
}
}
for (int i = 0, size = newComponent.size(); i < size; i++) {
BaseCell cell = newComponent.get(i);
if (cell != null) {
cell.added();
}
}
mData.removeAll(oldComponent);
mData.addAll(index, newComponent);
int oldSize = oldComponent.size();
int newSize = newComponent.size();
notifyItemRangeChanged(index, Math.max(oldSize, newSize));
}
}
}
use of com.tmall.wireless.tangram3.structure.BaseCell in project Tangram-Android by alibaba.
the class PojoGroupBasicAdapter method onBindViewHolder.
@Override
public void onBindViewHolder(BinderViewHolder<BaseCell, ? extends View> holder, int position) {
super.onBindViewHolder(holder, position);
int idx = findCardIdxFor(position);
if (idx >= 0) {
Pair<Range<Integer>, Card> pair = mCards.get(idx);
pair.second.onBindCell(position - pair.first.getLower(), position, mLastBindPosition < 0 || mLastBindPosition < position);
PageDetectorSupport pageDetectorSupport = pair.second.serviceManager.getService(PageDetectorSupport.class);
if (pageDetectorSupport != null) {
pageDetectorSupport.onBindItem(position, mLastBindPosition < 0 || mLastBindPosition < position, getItemByPosition(position));
}
}
mLastBindPosition = position;
}
use of com.tmall.wireless.tangram3.structure.BaseCell in project Tangram-Android by alibaba.
the class LinearScrollView method postBindView.
@Override
public void postBindView(BaseCell cell) {
if (lSCell == null) {
return;
}
recyclerView.setRecycledViewPool(lSCell.getRecycledViewPool());
recyclerView.removeItemDecoration(itemDecoration);
if (lSCell.hGap > 0) {
itemDecoration = new RecyclerView.ItemDecoration() {
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
outRect.set(0, 0, 0, 0);
int cellCount = lSCell.cells.size();
int viewIndex = (int) view.getTag(R.id.TANGRAM_LINEAR_SCROLL_POS);
int rowCount = (int) (lSCell.cells.size() * 1.0f / lSCell.maxRows + 0.5f);
boolean isLastCellInRow = false;
if ((viewIndex + 1) % rowCount == 0) {
// the last cell in every row
isLastCellInRow = true;
}
if (viewIndex == (cellCount - 1)) {
// the last cell in cells
isLastCellInRow = true;
}
if (viewIndex % rowCount == 0) {
// first view only set right
outRect.right = (int) (lSCell.hGap / 2);
} else if (isLastCellInRow) {
// last view only set left
outRect.left = (int) (lSCell.hGap / 2);
if (lSCell.maxRows > 1 && (cellCount % lSCell.maxRows == 1) && (viewIndex == cellCount - 1)) {
// the last cell in penultimate row
outRect.right = (int) (lSCell.hGap / 2);
}
} else {
outRect.left = (int) (lSCell.hGap / 2);
outRect.right = (int) (lSCell.hGap / 2);
}
}
};
recyclerView.addItemDecoration(itemDecoration);
}
float[] starts = null;
if (lSCell.cells != null && lSCell.cells.size() > 0) {
int maxRowCount = lSCell.cells.size();
if (lSCell.maxRows > 1) {
maxRowCount = (int) (maxRowCount * 1.0f / lSCell.maxRows + 0.5f);
}
starts = new float[maxRowCount];
for (int i = 0; i < maxRowCount; i++) {
int rowIndex = lSCell.maxRows * i;
int mapperIndex = lSCell.getMapperPosition(rowIndex);
starts[i] = totalDistance;
BaseCell bc = lSCell.cells.get(mapperIndex);
if (bc.style != null && bc.style.margin.length > 0) {
totalDistance = totalDistance + bc.style.margin[1] + bc.style.margin[3];
}
totalDistance += lSCell.pageWidth;
if (i > 0 && lSCell.hGap > 0) {
totalDistance += lSCell.hGap;
}
}
}
totalDistance -= getScreenWidth();
// calculate height of recycler view.
ViewGroup.LayoutParams lp = recyclerView.getLayoutParams();
if (!Double.isNaN(lSCell.pageHeight)) {
if (lSCell.maxRows == 0) {
lp.height = (int) (lSCell.pageHeight + 0.5f);
} else {
lp.height = (int) (lSCell.pageHeight * lSCell.maxRows + 0.5f);
}
if (lSCell.maxRows > 1 && lSCell.vGap > 0) {
lp.height += (int) ((lSCell.maxRows - 1) * lSCell.vGap + 0.5f);
}
}
recyclerView.setLayoutParams(lp);
recyclerView.setAdapter(lSCell.adapter);
if (lSCell.hasIndicator && totalDistance > 0) {
setViewColor(indicator, lSCell.indicatorColor);
setViewColor(indicatorContainer, lSCell.defaultIndicatorColor);
setIndicatorMeasure(indicator, (int) Math.round(lSCell.indicatorWidth), (int) Math.round(lSCell.indicatorHeight), 0, lSCell.indicatorRadius);
setIndicatorMeasure(indicatorContainer, (int) Math.round(lSCell.defaultIndicatorWidth), (int) Math.round(lSCell.indicatorHeight), (int) Math.round(lSCell.indicatorMargin), lSCell.indicatorRadius);
indicatorContainer.setVisibility(VISIBLE);
} else {
indicatorContainer.setVisibility(GONE);
}
recyclerView.addOnScrollListener(onScrollListener);
setBackgroundColor(lSCell.bgColor);
if (backgroundView != null) {
if (backgroundView instanceof ImageView) {
ImageUtils.doLoadImageUrl((ImageView) backgroundView, lSCell.nativeBackgroundImage);
}
}
if (lSCell.retainScrollState && starts != null) {
GridLayoutManager lm = (GridLayoutManager) recyclerView.getLayoutManager();
int position = computeFirstCompletelyVisibleItemPositionForScrolledX(starts);
lm.scrollToPositionWithOffset(position * lSCell.maxRows, (int) (starts[position] - lSCell.currentDistance));
}
if (lSCell.scrollMarginLeft > 0 || lSCell.scrollMarginRight > 0) {
setPadding(lSCell.scrollMarginLeft, 0, lSCell.scrollMarginRight, 0);
setClipToPadding(false);
setClipChildren(false);
} else {
setPadding(0, 0, 0, 0);
setClipToPadding(true);
setClipChildren(true);
}
recycleView(lSCell);
bindHeaderView(lSCell.mHeader);
bindFooterView(lSCell.mFooter);
}
use of com.tmall.wireless.tangram3.structure.BaseCell in project Tangram-Android by alibaba.
the class LinearScrollView method getViewFromRecycler.
private View getViewFromRecycler(@NonNull BaseCell cell) {
GroupBasicAdapter adapter = cell.serviceManager.getService(GroupBasicAdapter.class);
RecyclerView.RecycledViewPool pool = cell.serviceManager.getService(RecyclerView.RecycledViewPool.class);
int itemViewType = adapter.getItemType(cell);
BinderViewHolder holder = (BinderViewHolder) pool.getRecycledView(itemViewType);
if (holder == null) {
holder = (BinderViewHolder) adapter.createViewHolder(this, itemViewType);
}
holder.bind(cell);
mViewHolders.add(holder);
return holder.itemView;
}
use of com.tmall.wireless.tangram3.structure.BaseCell in project Tangram-Android by alibaba.
the class BannerView method getHeaderViewFromRecycler.
private View getHeaderViewFromRecycler(@NonNull BaseCell cell) {
GroupBasicAdapter adapter = cell.serviceManager.getService(GroupBasicAdapter.class);
RecyclerView.RecycledViewPool pool = cell.serviceManager.getService(RecyclerView.RecycledViewPool.class);
int itemViewType = adapter.getItemType(cell);
BinderViewHolder holder = (BinderViewHolder) pool.getRecycledView(itemViewType);
if (holder == null) {
holder = (BinderViewHolder) adapter.createViewHolder(this, itemViewType);
}
holder.bind(cell);
mHeaderViewHolders.add(holder);
return holder.itemView;
}
Aggregations