use of com.taobao.weex.ui.component.WXComponent in project incubator-weex by apache.
the class Statements method copyComponentTree.
/**
* recursive copy component,
*/
private static final WXComponent copyComponentTree(WXComponent source, WXVContainer parent) {
WXDomObject node = (WXDomObject) source.getDomObject();
WXComponent component = WXComponentFactory.newInstance(source.getInstance(), node, parent);
if (source instanceof WXVContainer) {
WXVContainer container = (WXVContainer) source;
WXVContainer childParent = (WXVContainer) component;
WXDomObject childParentNode = (WXDomObject) childParent.getDomObject();
int count = container.getChildCount();
for (int i = 0; i < count; ++i) {
WXComponent child = container.getChild(i);
if (child != null) {
WXComponent targetChild = copyComponentTree(child, childParent);
childParent.addChild(targetChild);
childParentNode.add((WXDomObject) targetChild.getDomObject(), -1);
}
}
}
// copy info need be sync
if (source.isWaste()) {
component.setWaste(true);
}
return component;
}
use of com.taobao.weex.ui.component.WXComponent in project incubator-weex by apache.
the class BasicListComponent method onCreateViewHolder.
/**
* Create an instance of {@link ListBaseViewHolder} for the given viewType (not for the given
* index). This markComponentUsable();method will look up for the first component that fits the viewType requirement and
* doesn't be used. Then create the certain type of view, detach the view f[rom the component.
*
* @param parent the ViewGroup into which the new view will be inserted
* @param viewType the type of the new view
* @return the created view holder.
*/
@Override
public ListBaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (mChildren != null) {
if (mViewTypes == null)
return createVHForFakeComponent(viewType);
ArrayList<WXComponent> mTypes = mViewTypes.get(viewType);
checkRecycledViewPool(viewType);
if (mTypes == null)
return createVHForFakeComponent(viewType);
for (int i = 0; i < mTypes.size(); i++) {
WXComponent component = mTypes.get(i);
if (component == null || component.isUsing()) {
continue;
}
if (component.getDomObject() != null && component.getDomObject().isFixed()) {
return createVHForFakeComponent(viewType);
} else {
if (component instanceof WXCell) {
if (component.getRealView() != null) {
return new ListBaseViewHolder(component, viewType);
} else {
((WXCell) component).lazy(false);
component.createView();
component.applyLayoutAndEvent(component);
return new ListBaseViewHolder(component, viewType);
}
} else if (component instanceof WXBaseRefresh) {
return createVHForRefreshComponent(viewType);
} else {
WXLogUtils.e(TAG, "List cannot include element except cell、header、fixed、refresh and loading");
return createVHForFakeComponent(viewType);
}
}
}
}
if (WXEnvironment.isApkDebugable()) {
WXLogUtils.e(TAG, "Cannot find request viewType: " + viewType);
}
return createVHForFakeComponent(viewType);
}
use of com.taobao.weex.ui.component.WXComponent in project incubator-weex by apache.
the class BasicListComponent method scrollTo.
@Override
public void scrollTo(WXComponent component, Map<String, Object> options) {
float offsetFloat = 0;
boolean smooth = true;
if (options != null) {
String offsetStr = options.get(Constants.Name.OFFSET) == null ? "0" : options.get(Constants.Name.OFFSET).toString();
smooth = WXUtils.getBoolean(options.get(Constants.Name.ANIMATED), true);
if (offsetStr != null) {
try {
offsetFloat = WXViewUtils.getRealPxByWidth(Float.parseFloat(offsetStr), getInstance().getInstanceViewPortWidth());
} catch (Exception e) {
WXLogUtils.e("Float parseFloat error :" + e.getMessage());
}
}
}
final int offset = (int) offsetFloat;
T bounceRecyclerView = getHostView();
if (bounceRecyclerView == null) {
return;
}
WXComponent parent = component;
WXCell cell = null;
while (parent != null) {
if (parent instanceof WXCell) {
cell = (WXCell) parent;
break;
}
parent = parent.getParent();
}
if (cell != null) {
final int pos = mChildren.indexOf(cell);
if (pos == -1) {
// Invalid position
return;
}
final WXRecyclerView view = bounceRecyclerView.getInnerView();
view.scrollTo(smooth, pos, offset, getOrientation());
}
}
use of com.taobao.weex.ui.component.WXComponent in project incubator-weex by apache.
the class BasicListComponent method findComponentByAnchorName.
@Nullable
private WXComponent findComponentByAnchorName(@NonNull WXComponent root, @NonNull String anchorName) {
long start = 0;
if (WXEnvironment.isApkDebugable()) {
start = System.currentTimeMillis();
}
Deque<WXComponent> deque = new ArrayDeque<>();
deque.add(root);
while (!deque.isEmpty()) {
WXComponent curComponent = deque.removeFirst();
ImmutableDomObject object = curComponent.getDomObject();
if (object != null) {
String isAnchorSet = WXUtils.getString(object.getAttrs().get(anchorName), null);
// hit
if (isAnchorSet != null && isAnchorSet.equals("true")) {
if (WXEnvironment.isApkDebugable()) {
WXLogUtils.d("dragPerf", "findComponentByAnchorName time: " + (System.currentTimeMillis() - start) + "ms");
}
return curComponent;
}
}
if (curComponent instanceof WXVContainer) {
WXVContainer container = (WXVContainer) curComponent;
for (int i = 0, len = container.childCount(); i < len; i++) {
WXComponent child = container.getChild(i);
deque.add(child);
}
}
}
if (WXEnvironment.isApkDebugable()) {
WXLogUtils.d("dragPerf", "findComponentByAnchorName elapsed time: " + (System.currentTimeMillis() - start) + "ms");
}
return null;
}
use of com.taobao.weex.ui.component.WXComponent in project incubator-weex by apache.
the class BasicListComponent method onBindViewHolder.
/**
* Bind the component of the position to the holder. Then flush the view.
*
* @param holder viewHolder, which holds reference to the view
* @param position position of component in list
*/
@Override
public void onBindViewHolder(final ListBaseViewHolder holder, int position) {
if (holder == null)
return;
holder.setComponentUsing(true);
WXComponent component = getChild(position);
if (component == null || (component instanceof WXRefresh) || (component instanceof WXLoading) || (component.getDomObject() != null && component.getDomObject().isFixed())) {
if (WXEnvironment.isApkDebugable()) {
WXLogUtils.d(TAG, "Bind WXRefresh & WXLoading " + holder);
}
if (component instanceof WXBaseRefresh && holder.getView() != null && component.getDomObject() != null && (component.getDomObject().getAttrs().get("holderBackground") != null)) {
Object holderBackground = component.getDomObject().getAttrs().get("holderBackground");
int color = WXResourceUtils.getColor(holderBackground.toString(), Color.WHITE);
holder.getView().setBackgroundColor(color);
holder.getView().setVisibility(View.VISIBLE);
holder.getView().postInvalidate();
}
return;
}
if (holder.getComponent() != null && holder.getComponent() instanceof WXCell) {
if (holder.isRecycled()) {
holder.bindData(component);
component.onRenderFinish(STATE_UI_FINISH);
}
if (mDragHelper == null || !mDragHelper.isDraggable()) {
return;
}
mTriggerType = (mTriggerType == null) ? DEFAULT_TRIGGER_TYPE : mTriggerType;
WXCell cell = (WXCell) holder.getComponent();
boolean isExcluded = DEFAULT_EXCLUDED;
if (cell.getDomObject() != null) {
WXAttr cellAttrs = cell.getDomObject().getAttrs();
isExcluded = WXUtils.getBoolean(cellAttrs.get(EXCLUDED), DEFAULT_EXCLUDED);
}
mDragHelper.setDragExcluded(holder, isExcluded);
// NOTICE: event maybe consumed by other views
if (DragTriggerType.PAN.equals(mTriggerType)) {
mDragHelper.setLongPressDragEnabled(false);
WXComponent anchorComponent = findComponentByAnchorName(cell, DRAG_ANCHOR);
if (anchorComponent != null && anchorComponent.getHostView() != null && !isExcluded) {
View anchor = anchorComponent.getHostView();
anchor.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
mDragHelper.startDrag(holder);
}
return true;
}
});
} else {
if (WXEnvironment.isApkDebugable()) {
if (!isExcluded) {
WXLogUtils.e(TAG, "[error] onBindViewHolder: the anchor component or view is not found");
} else {
WXLogUtils.d(TAG, "onBindViewHolder: position " + position + " is drag excluded");
}
}
}
} else if (DragTriggerType.LONG_PRESS.equals(mTriggerType)) {
mDragHelper.setLongPressDragEnabled(true);
}
}
}
Aggregations