com.alibaba.android.vlayout.VirtualLayoutManager Java Examples

The following examples show how to use com.alibaba.android.vlayout.VirtualLayoutManager. 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: PojoAdapterBuilder.java    From Tangram-Android with MIT License 6 votes vote down vote up
@Override
public PojoGroupBasicAdapter newAdapter(
        @NonNull final Context context, @NonNull final VirtualLayoutManager layoutManager,
        @NonNull final ServiceManager serviceManager) {

    BaseCellBinderResolver componentBinderResolver = serviceManager.getService(BaseCellBinderResolver.class);
    BaseCardBinderResolver cardBinderResolver = serviceManager.getService(BaseCardBinderResolver.class);
    MVHelper mvHelper = serviceManager.getService(MVHelper.class);
    ViewManager viewManager = serviceManager.getService(ViewManager.class);

    final PojoGroupBasicAdapter adapter = new PojoGroupBasicAdapter(context, layoutManager, componentBinderResolver,
        cardBinderResolver, mvHelper, viewManager);


    return adapter;
}
 
Example #2
Source File: LayoutActivity.java    From YCRefreshView with Apache License 2.0 6 votes vote down vote up
private void initVLayout() {
    mAdapters = new LinkedList<>();
    //初始化
    //创建VirtualLayoutManager对象
    layoutManager = new VirtualLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);

    //设置回收复用池大小,(如果一屏内相同类型的 View 个数比较多,需要设置一个合适的大小,防止来回滚动时重新创建 View)
    RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
    recyclerView.setRecycledViewPool(viewPool);
    viewPool.setMaxRecycledViews(0, 20);

    //设置适配器
    delegateAdapter = new DelegateAdapter(layoutManager, true);
    recyclerView.setAdapter(delegateAdapter);

    //自定义各种不同适配器
    initAllTypeView();
    //设置适配器
    delegateAdapter.setAdapters(mAdapters);
}
 
Example #3
Source File: AbstractFullFillLayoutHelper.java    From vlayout with MIT License 6 votes vote down vote up
@Override
public void checkAnchorInfo(RecyclerView.State state, VirtualLayoutManager.AnchorInfoWrapper anchorInfo, LayoutManagerHelper helper) {
    if (anchorInfo.layoutFromEnd) {
        if (!hasFooter) {
            anchorInfo.position = getRange().getUpper();
        } else {
            //keep the previously calculated position
        }
    } else {
        if (!hasHeader) {
            anchorInfo.position = getRange().getLower();
        } else {
            //keep the previously calculated position
        }
    }
    mLayoutWithAnchor = true;
}
 
Example #4
Source File: VLayoutFragment.java    From AndroidSamples with Apache License 2.0 6 votes vote down vote up
private void initView() {
    layoutManager = new VirtualLayoutManager(mContext);
    mainView.setLayoutManager(layoutManager);
    // 实现类似padding的效果
    mainView.addItemDecoration(new RecyclerView.ItemDecoration() {
        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            outRect.set(0, 0, 0, 15);
        }
    });
    DelegateAdapter delegateAdapter = new DelegateAdapter(layoutManager, true);
    mainView.setAdapter(delegateAdapter);
    adapters = new LinkedList<>();

    initBanner();
    initSticky();
    initGrid();
    initLinear();
    delegateAdapter.setAdapters(adapters);
}
 
Example #5
Source File: PractiseFragment.java    From YCAudioPlayer with Apache License 2.0 6 votes vote down vote up
private void initVLayout() {
    mAdapters = new LinkedList<>();
    //初始化
    //创建VirtualLayoutManager对象
    layoutManager = new VirtualLayoutManager(activity);
    recyclerView.setLayoutManager(layoutManager);

    //设置回收复用池大小,(如果一屏内相同类型的 View 个数比较多,需要设置一个合适的大小,防止来回滚动时重新创建 View)
    RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
    recyclerView.setRecycledViewPool(viewPool);
    viewPool.setMaxRecycledViews(0, 20);

    //设置适配器
    delegateAdapter = new DelegateAdapter(layoutManager, true);
    recyclerView.setAdapter(delegateAdapter);

    //自定义各种不同适配器
    initAllTypeView();
    //设置适配器
    //delegateAdapter.setAdapters(mAdapters);
}
 
Example #6
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 #7
Source File: DebugActivity.java    From vlayout with MIT License 6 votes vote down vote up
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
    RecyclerView recyclerView = (RecyclerView)findViewById(R.id.main_view);
    VirtualLayoutManager virtualLayoutManager = new VirtualLayoutManager(this);
    DelegateAdapter delegateAdapter = new DelegateAdapter(virtualLayoutManager);
    List<Adapter> adapterList = new ArrayList<>();
    adapterList.add(new SubAdapter(new LinearLayoutHelper(20), 20));
    adapterList.add(new SubAdapter(new StickyLayoutHelper(true), 1));
    adapterList.add(new SubAdapter(new LinearLayoutHelper(20), 20));
    adapterList.add(new SubAdapter(new GridLayoutHelper(4), 80));
    // adapterList.add(new SubAdapter(new FixLayoutHelper(0, 0), 1));
    adapterList.add(new SubAdapter(new FixLayoutHelper(TOP_RIGHT, 0, 0), 1));
    delegateAdapter.addAdapters(adapterList);
    recyclerView.setLayoutManager(virtualLayoutManager);
    recyclerView.setAdapter(delegateAdapter);
}
 
Example #8
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 #9
Source File: StaggeredGridLayoutHelper.java    From vlayout with MIT License 6 votes vote down vote up
/**
 * Checks for gaps if we've reached to the top of the list.
 * <p/>
 * Intermediate gaps created by full span items are tracked via mLaidOutInvalidFullSpan field.
 */
private View hasGapsToFix(VirtualLayoutManager layoutManager, final int position, final int alignLine) {
    View view = layoutManager.findViewByPosition(position);

    if (view == null) {
        return null;
    }


    BitSet mSpansToCheck = new BitSet(mNumLanes);
    mSpansToCheck.set(0, mNumLanes, true);

    if (mSpans != null) {
        for (int i = 0, size = mSpans.length; i < size; i++) {
            Span span = mSpans[i];
            if (span.mViews.size() != 0 && checkSpanForGap(span, layoutManager, alignLine)) {
                return layoutManager.getReverseLayout() ? span.mViews.get(span.mViews.size() - 1) : span.mViews.get(0);
            }
        }
    }

    // everything looks good
    return null;
}
 
Example #10
Source File: StaggeredGridLayoutHelper.java    From vlayout with MIT License 6 votes vote down vote up
@Override
public void onRefreshLayout(RecyclerView.State state, VirtualLayoutManager.AnchorInfoWrapper anchorInfo, LayoutManagerHelper helper) {
    super.onRefreshLayout(state, anchorInfo, helper);
    ensureLanes();

    if (isOutOfRange(anchorInfo.position)) {
        if (BuildConfig.DEBUG) {
            Log.d(TAG, "onRefreshLayout span.clear()");
        }
        if (mSpans != null) {
            for (int i = 0, size = mSpans.length; i < size; i++) {
                Span span = mSpans[i];
                span.clear();
            }
        }
    }
}
 
Example #11
Source File: StickyLayoutHelper.java    From vlayout with MIT License 6 votes vote down vote up
/**
 *
 * get extra top offset when there are multi-sticky views at the top of current view
 * in order to avoid views overlap
 * the offset value is equal to total sticky views' height.
 * @param helper
 */
private int getExtraTopOffset(LayoutManagerHelper helper) {
    if (mStackable == null || !mStackable.enable()) {
        return 0;
    }

    int offset = 0;
    if (helper instanceof VirtualLayoutManager){
        List<LayoutHelper> helperList = ((VirtualLayoutManager) helper).getLayoutHelpers();
        for (LayoutHelper helperItem : helperList) {
            if (helperItem.isFixLayout()) {
                if (helperItem.getRange().getUpper() < this.getRange().getLower()) {
                    View view = helperItem.getFixedView();
                    if (view != null) {
                        offset += view.getHeight();
                    }
                }
            }
        }

    }

    return offset;
}
 
Example #12
Source File: OnePlusNLayoutHelper.java    From vlayout with MIT License 6 votes vote down vote up
private int handleHeader(View header, LayoutStateWrapper layoutState, LayoutChunkResult result, LayoutManagerHelper helper,
    boolean layoutInVertical, int parentWidth, int parentHeight, int parentHPadding, int parentVPadding) {
    if (header == null) {
        return 0;
    }
    OrientationHelperEx orientationHelper = helper.getMainOrientationHelper();

    final VirtualLayoutManager.LayoutParams lp = (LayoutParams) header.getLayoutParams();


    // fill width
    int widthSpec = helper.getChildMeasureSpec(parentWidth - parentHPadding,
        layoutInVertical ? MATCH_PARENT : lp.width, !layoutInVertical);
    int heightSpec = helper.getChildMeasureSpec(parentHeight - parentVPadding,
        layoutInVertical ? lp.height : MeasureSpec.EXACTLY, layoutInVertical);
    helper.measureChildWithMargins(header, widthSpec, heightSpec);
    return orientationHelper.getDecoratedMeasurement(header);
}
 
Example #13
Source File: OnePlusNLayoutHelper.java    From vlayout with MIT License 6 votes vote down vote up
private int handleFooter(View footer, LayoutStateWrapper layoutState, LayoutChunkResult result, LayoutManagerHelper helper,
    boolean layoutInVertical, int parentWidth, int parentHeight, int parentHPadding, int parentVPadding) {
    if (footer == null) {
        return 0;
    }

    OrientationHelperEx orientationHelper = helper.getMainOrientationHelper();

    final VirtualLayoutManager.LayoutParams lp = (LayoutParams) footer.getLayoutParams();

    // fill width
    int widthSpec = helper.getChildMeasureSpec(parentWidth - parentHPadding,
        layoutInVertical ? MATCH_PARENT : lp.width, !layoutInVertical);
    int heightSpec = helper.getChildMeasureSpec(parentHeight - parentVPadding,
        layoutInVertical ? lp.height : MeasureSpec.EXACTLY, layoutInVertical);
    helper.measureChildWithMargins(footer, widthSpec, heightSpec);
    return orientationHelper.getDecoratedMeasurement(footer);
}
 
Example #14
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 #15
Source File: BaseTangramEngine.java    From Tangram-Android with MIT License 6 votes vote down vote up
@Override
public int onGetChildDrawingOrder(int childCount, int i) {
    if (zIndex.length < childCount) {
        zIndex = doubleIndex(zIndex);
        viewIndex = doubleIndex(viewIndex);
    }
    // check all current zIndex
    for (int j = 0; j < childCount; j++) {
        View child = mContentView.getChildAt(j);
        if (child != null) {
            VirtualLayoutManager.LayoutParams layoutParams = (LayoutParams)child.getLayoutParams();
            zIndex[j] = layoutParams.zIndex;
        } else {
            zIndex[j] = 0;
        }
        viewIndex[j] = j;
    }
    // reorder drawing by zIndex
    bubbleSort(zIndex, viewIndex, childCount);

    int result = viewIndex[i];
    clearIndex(zIndex);
    clearIndex(viewIndex);
    return  result;
}
 
Example #16
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 #17
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 #18
Source File: GroupBasicAdapter.java    From Tangram-Android with MIT License 5 votes vote down vote up
public GroupBasicAdapter(@NonNull final Context context, @NonNull final VirtualLayoutManager layoutManager,
                         @NonNull ControlBinderResolver<? extends ControlBinder<C, ? extends View>> cellBinderResolver,
                         @NonNull LayoutBinderResolver<L, ? extends LayoutBinder<L>> cardBinderResolver) {
    super(layoutManager);

    mContext = Preconditions.checkNotNull(context, "context should not be null");

    mCompBinderResolver = Preconditions.checkNotNull(cellBinderResolver, "componentBinderResolver should not be null");
    mCardBinderResolver = Preconditions.checkNotNull(cardBinderResolver, "layoutBinderResolver should not be null");
}
 
Example #19
Source File: VLayoutFragment.java    From AndroidSamples with Apache License 2.0 5 votes vote down vote up
private void initBanner() {
    adapters.add(new SubAdapter(mContext, new LinearLayoutHelper(), 1,
            new VirtualLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ConvertUtils.dp2px(200))) {
        @Override
        public int getItemViewType(int position) {
            return 1;
        }
    });
}
 
Example #20
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 #21
Source File: SwipeItemTouchListener.java    From Tangram-Android with MIT License 5 votes vote down vote up
public SwipeItemTouchListener(Context context, GroupBasicAdapter groupBasicAdapter, RecyclerView recyclerView) {
    this.mGroupBasicAdapter = groupBasicAdapter;
    this.recyclerView = recyclerView;
    this.recyclerView.addOnScrollListener(scrollListener);
    this.layoutManager = (VirtualLayoutManager) recyclerView.getLayoutManager();
    mSwipeGestureDector = new GestureDetectorCompat(context, new SwipeGestureListener());
    mChildList = new ArrayList<>();
}
 
Example #22
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 #23
Source File: TangramEngine.java    From Tangram-Android with MIT License 5 votes vote down vote up
/**
 * 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));
        }
    }
}
 
Example #24
Source File: PojoGroupBasicAdapter.java    From Tangram-Android with MIT License 5 votes vote down vote up
PojoGroupBasicAdapter(@NonNull Context context, @NonNull VirtualLayoutManager layoutManager,
                      @NonNull BaseCellBinderResolver componentBinderResolver,
                      @NonNull BaseCardBinderResolver cardBinderResolver,
                      @NonNull MVHelper mvHelper) {
    super(context, layoutManager, componentBinderResolver, cardBinderResolver);
    this.mMvHelper = mvHelper;
    //if true, this adapter's items have stable IDs, but BaseCell.objectId return 0. So not use.
    //setHasStableIds(true);
}
 
Example #25
Source File: GroupBasicAdapter.java    From Tangram-Android with MIT License 5 votes vote down vote up
public GroupBasicAdapter(@NonNull final Context context, @NonNull final VirtualLayoutManager layoutManager,
                         @NonNull ControlBinderResolver<? extends ControlBinder<C, ? extends View>> cellBinderResolver,
                         @NonNull LayoutBinderResolver<L, ? extends LayoutBinder<L>> cardBinderResolver) {
    super(layoutManager);

    mContext = Preconditions.checkNotNull(context, "context should not be null");

    mCompBinderResolver = Preconditions.checkNotNull(cellBinderResolver, "componentBinderResolver should not be null");
    mCardBinderResolver = Preconditions.checkNotNull(cardBinderResolver, "layoutBinderResolver should not be null");
}
 
Example #26
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 #27
Source File: TangramEngine.java    From Tangram-Android with MIT License 5 votes vote down vote up
/**
 * 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));
        }
    }
}
 
Example #28
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 #29
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 #30
Source File: SwipeItemTouchListener.java    From Tangram-Android with MIT License 5 votes vote down vote up
public SwipeItemTouchListener(Context context, GroupBasicAdapter groupBasicAdapter, RecyclerView recyclerView) {
    this.mGroupBasicAdapter = groupBasicAdapter;
    this.recyclerView = recyclerView;
    this.recyclerView.addOnScrollListener(scrollListener);
    this.layoutManager = (VirtualLayoutManager) recyclerView.getLayoutManager();
    mSwipeGestureDector = new GestureDetectorCompat(context, new SwipeGestureListener());
    mChildList = new ArrayList<>();
}