Search in sources :

Example 6 with AppearanceHelper

use of com.taobao.weex.ui.component.AppearanceHelper in project incubator-weex by apache.

the class WXRecyclerTemplateList method notifyAppearStateChange.

/**
 * first fire appear event.
 */
@Override
public void notifyAppearStateChange(int firstVisible, int lastVisible, int directionX, int directionY) {
    if (mAppearHelpers == null || mAppearHelpers.size() <= 0) {
        return;
    }
    String direction = directionY > 0 ? Constants.Value.DIRECTION_UP : directionY < 0 ? Constants.Value.DIRECTION_DOWN : null;
    if (getOrientation() == Constants.Orientation.HORIZONTAL && directionX != 0) {
        direction = directionX > 0 ? Constants.Value.DIRECTION_LEFT : Constants.Value.DIRECTION_RIGHT;
    }
    RecyclerView recyclerView = getHostView().getInnerView();
    for (int position = firstVisible; position <= lastVisible; position++) {
        int type = getItemViewType(position);
        List<AppearanceHelper> helpers = mAppearHelpers.get(type);
        if (helpers == null) {
            continue;
        }
        for (AppearanceHelper helper : helpers) {
            if (!helper.isWatch()) {
                continue;
            }
            TemplateViewHolder itemHolder = (TemplateViewHolder) recyclerView.findViewHolderForAdapterPosition(position);
            if (itemHolder == null || itemHolder.getComponent() == null) {
                break;
            }
            List<WXComponent> childListeners = findChildListByRef(itemHolder.getComponent(), helper.getAwareChild().getRef());
            if (childListeners == null || childListeners.size() == 0) {
                break;
            }
            Map<String, Map<Integer, List<Object>>> disAppearList = mDisAppearWatchList.get(position);
            if (disAppearList == null) {
                disAppearList = new ArrayMap<>();
                mDisAppearWatchList.put(position, disAppearList);
            }
            Map<Integer, List<Object>> componentDisAppearList = disAppearList.get(helper.getAwareChild().getRef());
            if (componentDisAppearList == null) {
                componentDisAppearList = new ArrayMap<>();
                disAppearList.put(helper.getAwareChild().getRef(), componentDisAppearList);
            }
            for (int m = 0; m < childListeners.size(); m++) {
                WXComponent childLisener = childListeners.get(m);
                if (childLisener.getHostView() == null) {
                    continue;
                }
                boolean appear = helper.isViewVisible(childLisener.getHostView());
                int key = childLisener.getHostView().hashCode();
                if (appear) {
                    if (!componentDisAppearList.containsKey(key)) {
                        childLisener.notifyAppearStateChange(Constants.Event.APPEAR, direction);
                        List<Object> eventArgs = null;
                        if (childLisener.getDomObject().getEvents() != null && childLisener.getDomObject().getEvents().getEventBindingArgsValues() != null && childLisener.getDomObject().getEvents().getEventBindingArgsValues().get(Constants.Event.DISAPPEAR) != null) {
                            eventArgs = childLisener.getDomObject().getEvents().getEventBindingArgsValues().get(Constants.Event.DISAPPEAR);
                        }
                        componentDisAppearList.put(key, eventArgs);
                    }
                } else {
                    if (componentDisAppearList.containsKey(key)) {
                        childLisener.notifyAppearStateChange(Constants.Event.DISAPPEAR, direction);
                        componentDisAppearList.remove(key);
                    }
                }
            }
        }
    }
    // handle disappear event, out of position
    int count = getItemCount();
    for (int position = 0; position < count; position++) {
        if (position >= firstVisible && position <= lastVisible) {
            position = lastVisible + 1;
            continue;
        }
        Map<String, Map<Integer, List<Object>>> map = mDisAppearWatchList.get(position);
        if (map == null) {
            continue;
        }
        WXCell template = mTemplateSources.get(getTemplateKey(position));
        if (template == null) {
            return;
        }
        Set<Map.Entry<String, Map<Integer, List<Object>>>> cellWatcherEntries = map.entrySet();
        for (Map.Entry<String, Map<Integer, List<Object>>> cellWatcherEntry : cellWatcherEntries) {
            String ref = cellWatcherEntry.getKey();
            WXComponent component = findChildByRef(template, ref);
            if (component == null) {
                continue;
            }
            Map<Integer, List<Object>> eventWatchers = cellWatcherEntry.getValue();
            if (eventWatchers == null || eventWatchers.size() == 0) {
                continue;
            }
            WXEvent events = component.getDomObject().getEvents();
            Set<Map.Entry<Integer, List<Object>>> eventWatcherEntries = eventWatchers.entrySet();
            for (Map.Entry<Integer, List<Object>> eventWatcherEntry : eventWatcherEntries) {
                events.putEventBindingArgsValue(Constants.Event.DISAPPEAR, eventWatcherEntry.getValue());
                component.notifyAppearStateChange(Constants.Event.DISAPPEAR, direction);
            }
            eventWatchers.clear();
        }
        mDisAppearWatchList.remove(position);
    }
}
Also used : WXComponent(com.taobao.weex.ui.component.WXComponent) Point(android.graphics.Point) WXCell(com.taobao.weex.ui.component.list.WXCell) WXEvent(com.taobao.weex.dom.WXEvent) WXRecyclerView(com.taobao.weex.ui.view.listview.WXRecyclerView) RecyclerView(android.support.v7.widget.RecyclerView) BounceRecyclerView(com.taobao.weex.ui.view.refresh.wrapper.BounceRecyclerView) WXDomObject(com.taobao.weex.dom.WXDomObject) JSONObject(com.alibaba.fastjson.JSONObject) WXCellDomObject(com.taobao.weex.dom.WXCellDomObject) WXRecyclerDomObject(com.taobao.weex.dom.WXRecyclerDomObject) List(java.util.List) ArrayList(java.util.ArrayList) ArrayMap(android.support.v4.util.ArrayMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) AppearanceHelper(com.taobao.weex.ui.component.AppearanceHelper)

Aggregations

Point (android.graphics.Point)6 AppearanceHelper (com.taobao.weex.ui.component.AppearanceHelper)6 WXComponent (com.taobao.weex.ui.component.WXComponent)5 ArrayMap (android.support.v4.util.ArrayMap)3 RecyclerView (android.support.v7.widget.RecyclerView)3 WXRecyclerView (com.taobao.weex.ui.view.listview.WXRecyclerView)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 View (android.view.View)2 WXCell (com.taobao.weex.ui.component.list.WXCell)2 BounceRecyclerView (com.taobao.weex.ui.view.refresh.wrapper.BounceRecyclerView)2 ImageView (android.widget.ImageView)1 JSONObject (com.alibaba.fastjson.JSONObject)1 WXCellDomObject (com.taobao.weex.dom.WXCellDomObject)1 WXDomObject (com.taobao.weex.dom.WXDomObject)1 WXEvent (com.taobao.weex.dom.WXEvent)1 WXRecyclerDomObject (com.taobao.weex.dom.WXRecyclerDomObject)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1