Search in sources :

Example 1 with LayoutHelper

use of com.alibaba.android.vlayout.LayoutHelper in project vlayout by alibaba.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
    final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.main_view);
    VirtualLayoutManager layoutManager = new VirtualLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
    // layoutManager.setReverseLayout(true);
    recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {

        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            outRect.set(10, 10, 10, 10);
        }
    });
    final List<LayoutHelper> helpers = new LinkedList<>();
    final GridLayoutHelper gridLayoutHelper = new GridLayoutHelper(4);
    gridLayoutHelper.setItemCount(25);
    final ScrollFixLayoutHelper scrollFixLayoutHelper = new ScrollFixLayoutHelper(FixLayoutHelper.TOP_RIGHT, 100, 100);
    helpers.add(DefaultLayoutHelper.newHelper(7));
    helpers.add(scrollFixLayoutHelper);
    helpers.add(DefaultLayoutHelper.newHelper(2));
    helpers.add(gridLayoutHelper);
    layoutManager.setLayoutHelpers(helpers);
    recyclerView.setAdapter(new VirtualLayoutAdapter(layoutManager) {

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MainViewHolder(new TextView(MainActivity.this));
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            VirtualLayoutManager.LayoutParams layoutParams = new VirtualLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 300);
            holder.itemView.setLayoutParams(layoutParams);
            ((TextView) holder.itemView).setText(Integer.toString(position));
            if (position == 7) {
                layoutParams.height = 60;
                layoutParams.width = 60;
            } else if (position > 35) {
                layoutParams.height = 200 + (position - 30) * 100;
            }
            if (position > 35) {
                holder.itemView.setBackgroundColor(0x66cc0000 + (position - 30) * 128);
            } else if (position % 2 == 0) {
                holder.itemView.setBackgroundColor(0xaa00ff00);
            } else {
                holder.itemView.setBackgroundColor(0xccff00ff);
            }
        }

        @Override
        public int getItemCount() {
            List<LayoutHelper> helpers = getLayoutHelpers();
            if (helpers == null) {
                return 0;
            }
            int count = 0;
            for (int i = 0, size = helpers.size(); i < size; i++) {
                count += helpers.get(i).getItemCount();
            }
            return count;
        }
    });
    new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {

        @Override
        public void run() {
            recyclerView.scrollToPosition(7);
            recyclerView.getAdapter().notifyDataSetChanged();
        }
    }, 6000);
}
Also used : Rect(android.graphics.Rect) ScrollFixLayoutHelper(com.alibaba.android.vlayout.layout.ScrollFixLayoutHelper) ViewGroup(android.view.ViewGroup) Handler(android.os.Handler) GridLayoutHelper(com.alibaba.android.vlayout.layout.GridLayoutHelper) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) View(android.view.View) LinkedList(java.util.LinkedList) VirtualLayoutAdapter(com.alibaba.android.vlayout.VirtualLayoutAdapter) RecyclerView(android.support.v7.widget.RecyclerView) DefaultLayoutHelper(com.alibaba.android.vlayout.layout.DefaultLayoutHelper) LayoutHelper(com.alibaba.android.vlayout.LayoutHelper) FixLayoutHelper(com.alibaba.android.vlayout.layout.FixLayoutHelper) ScrollFixLayoutHelper(com.alibaba.android.vlayout.layout.ScrollFixLayoutHelper) GridLayoutHelper(com.alibaba.android.vlayout.layout.GridLayoutHelper) TextView(android.widget.TextView) List(java.util.List) LinkedList(java.util.LinkedList) VirtualLayoutManager(com.alibaba.android.vlayout.VirtualLayoutManager)

Example 2 with LayoutHelper

use of com.alibaba.android.vlayout.LayoutHelper in project Tangram-Android by alibaba.

the class TangramEngine method insertWith.

/**
 * A high performance method to insert cells. Do not allowed to insert to an empty Tangram. TODO handle nested card
 * @param insertPosition the position to be inserted
 * @param list new cell data list
 * @since 2.1.0
 */
public void insertWith(int insertPosition, List<BaseCell> list) {
    int newItemSize = list != null ? list.size() : 0;
    if (newItemSize > 0 && mGroupBasicAdapter != null) {
        if (insertPosition >= mGroupBasicAdapter.getItemCount()) {
            insertPosition = mGroupBasicAdapter.getItemCount() - 1;
        }
        BaseCell insertCell = mGroupBasicAdapter.getItemByPosition(insertPosition);
        int cardIdx = mGroupBasicAdapter.findCardIdxFor(insertPosition);
        Card card = mGroupBasicAdapter.getCardRange(cardIdx).second;
        card.addCells(card, card.getCells().indexOf(insertCell), list);
        List<LayoutHelper> layoutHelpers = getLayoutManager().getLayoutHelpers();
        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 < insertPosition) {
                // do nothing
                } else if (start <= insertPosition && insertPosition <= end) {
                    layoutHelper.setItemCount(layoutHelper.getItemCount() + newItemSize);
                    layoutHelper.setRange(start, end + newItemSize);
                } else if (insertPosition < start) {
                    layoutHelper.setRange(start + newItemSize, end + newItemSize);
                }
            }
            mGroupBasicAdapter.insertComponents(insertPosition, list);
        }
    }
}
Also used : BaseCell(com.tmall.wireless.tangram.structure.BaseCell) LayoutHelper(com.alibaba.android.vlayout.LayoutHelper) Card(com.tmall.wireless.tangram.dataparser.concrete.Card)

Example 3 with LayoutHelper

use of com.alibaba.android.vlayout.LayoutHelper in project Tangram-Android by alibaba.

the class TangramEngine method insertBatchWith.

/**
 * @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);
    }
}
Also used : ArrayList(java.util.ArrayList) LayoutHelper(com.alibaba.android.vlayout.LayoutHelper) VirtualLayoutManager(com.alibaba.android.vlayout.VirtualLayoutManager)

Example 4 with LayoutHelper

use of com.alibaba.android.vlayout.LayoutHelper 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);
            }
        }
    }
}
Also used : BaseCell(com.tmall.wireless.tangram.structure.BaseCell) ArrayList(java.util.ArrayList) LayoutHelper(com.alibaba.android.vlayout.LayoutHelper) VirtualLayoutManager(com.alibaba.android.vlayout.VirtualLayoutManager) Card(com.tmall.wireless.tangram.dataparser.concrete.Card)

Example 5 with LayoutHelper

use of com.alibaba.android.vlayout.LayoutHelper in project Tangram-Android by alibaba.

the class Card method getLayoutHelper.

@Nullable
public final LayoutHelper getLayoutHelper() {
    LayoutHelper helper = convertLayoutHelper(mLayoutHelper);
    // bind style to helper
    if (style != null && helper != null) {
        helper.setZIndex(style.zIndex);
        if (helper instanceof BaseLayoutHelper) {
            BaseLayoutHelper baseHelper = (BaseLayoutHelper) helper;
            baseHelper.setBgColor(style.bgColor);
            if (!TextUtils.isEmpty(style.bgImgUrl)) {
                if (serviceManager != null && serviceManager.getService(CardSupport.class) != null) {
                    final CardSupport support = serviceManager.getService(CardSupport.class);
                    baseHelper.setLayoutViewBindListener(new BindListener(style) {

                        @Override
                        public void onBind(View layoutView, BaseLayoutHelper baseLayoutHelper) {
                            support.onBindBackgroundView(layoutView, Card.this);
                        }
                    });
                    baseHelper.setLayoutViewUnBindListener(new UnbindListener(style) {

                        @Override
                        public void onUnbind(View layoutView, BaseLayoutHelper baseLayoutHelper) {
                            support.onUnbindBackgroundView(layoutView, Card.this);
                        }
                    });
                } else {
                    baseHelper.setLayoutViewBindListener(new BindListener(style));
                    baseHelper.setLayoutViewUnBindListener(new UnbindListener(style));
                }
            } else {
                baseHelper.setLayoutViewBindListener(null);
                baseHelper.setLayoutViewUnBindListener(null);
            }
            if (!Float.isNaN(style.aspectRatio)) {
            // ((BaseLayoutHelper) helper).setAspectRatio(style.aspectRatio);
            }
        }
        if (helper instanceof FixAreaLayoutHelper) {
            FixAreaLayoutHelper fixHelper = (FixAreaLayoutHelper) helper;
            boolean hasCustomAnimatorHelper = false;
            if (serviceManager != null && serviceManager.getService(CardSupport.class) != null) {
                CardSupport support = serviceManager.getService(CardSupport.class);
                FixAreaLayoutHelper.FixViewAnimatorHelper viewAnimatorHelper = support.onGetFixViewAppearAnimator(Card.this);
                if (viewAnimatorHelper != null) {
                    hasCustomAnimatorHelper = true;
                    fixHelper.setFixViewAnimatorHelper(viewAnimatorHelper);
                }
            }
            if (!hasCustomAnimatorHelper) {
                final int duration = style.extras != null ? style.extras.optInt(Style.KEY_ANIMATION_DURATION) : 0;
                if (duration > 0) {
                    fixHelper.setFixViewAnimatorHelper(new FixAreaLayoutHelper.FixViewAnimatorHelper() {

                        @Override
                        public ViewPropertyAnimator onGetFixViewAppearAnimator(View fixView) {
                            int height = fixView.getMeasuredHeight();
                            fixView.setTranslationY(-height);
                            return fixView.animate().translationYBy(height).setDuration(duration);
                        }

                        @Override
                        public ViewPropertyAnimator onGetFixViewDisappearAnimator(View fixView) {
                            int height = fixView.getMeasuredHeight();
                            return fixView.animate().translationYBy(-height).setDuration(duration);
                        }
                    });
                }
            }
        }
        if (helper instanceof MarginLayoutHelper) {
            ((MarginLayoutHelper) helper).setMargin(style.margin[Style.MARGIN_LEFT_INDEX], style.margin[Style.MARGIN_TOP_INDEX], style.margin[Style.MARGIN_RIGHT_INDEX], style.margin[Style.MARGIN_BOTTOM_INDEX]);
            ((MarginLayoutHelper) helper).setPadding(style.padding[Style.MARGIN_LEFT_INDEX], style.padding[Style.MARGIN_TOP_INDEX], style.padding[Style.MARGIN_RIGHT_INDEX], style.padding[Style.MARGIN_BOTTOM_INDEX]);
        }
    }
    if (mRetainLayout) {
        mLayoutHelper = helper;
    }
    return helper;
}
Also used : MarginLayoutHelper(com.alibaba.android.vlayout.layout.MarginLayoutHelper) FixAreaLayoutHelper(com.alibaba.android.vlayout.layout.FixAreaLayoutHelper) ImageView(android.widget.ImageView) View(android.view.View) ViewPropertyAnimator(android.view.ViewPropertyAnimator) CardSupport(com.tmall.wireless.tangram.support.CardSupport) BaseLayoutHelper(com.alibaba.android.vlayout.layout.BaseLayoutHelper) LayoutHelper(com.alibaba.android.vlayout.LayoutHelper) MarginLayoutHelper(com.alibaba.android.vlayout.layout.MarginLayoutHelper) FixAreaLayoutHelper(com.alibaba.android.vlayout.layout.FixAreaLayoutHelper) BaseLayoutHelper(com.alibaba.android.vlayout.layout.BaseLayoutHelper) Nullable(android.support.annotation.Nullable)

Aggregations

LayoutHelper (com.alibaba.android.vlayout.LayoutHelper)26 VirtualLayoutManager (com.alibaba.android.vlayout.VirtualLayoutManager)13 LinkedList (java.util.LinkedList)9 View (android.view.View)7 Nullable (android.support.annotation.Nullable)6 ArrayList (java.util.ArrayList)6 RecyclerView (android.support.v7.widget.RecyclerView)5 FixLayoutHelper (com.alibaba.android.vlayout.layout.FixLayoutHelper)3 Card (com.tmall.wireless.tangram.dataparser.concrete.Card)3 Card (com.tmall.wireless.tangram3.dataparser.concrete.Card)3 NonNull (android.support.annotation.NonNull)2 ViewPropertyAnimator (android.view.ViewPropertyAnimator)2 ImageView (android.widget.ImageView)2 BaseLayoutHelper (com.alibaba.android.vlayout.layout.BaseLayoutHelper)2 FixAreaLayoutHelper (com.alibaba.android.vlayout.layout.FixAreaLayoutHelper)2 GridLayoutHelper (com.alibaba.android.vlayout.layout.GridLayoutHelper)2 MarginLayoutHelper (com.alibaba.android.vlayout.layout.MarginLayoutHelper)2 RangeGridLayoutHelper (com.alibaba.android.vlayout.layout.RangeGridLayoutHelper)2 BaseCell (com.tmall.wireless.tangram.structure.BaseCell)2 BaseCell (com.tmall.wireless.tangram3.structure.BaseCell)2