Search in sources :

Example 76 with LayoutParams

use of android.support.v7.widget.RecyclerView.LayoutParams in project vlayout by alibaba.

the class OnePlusNLayoutActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
    mFirstText = (TextView) findViewById(R.id.first);
    mLastText = (TextView) findViewById(R.id.last);
    mCountText = (TextView) findViewById(R.id.count);
    mTotalOffsetText = (TextView) findViewById(R.id.total_offset);
    final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.main_view);
    findViewById(R.id.jump).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            EditText position = (EditText) findViewById(R.id.position);
            if (!TextUtils.isEmpty(position.getText())) {
                try {
                    int pos = Integer.parseInt(position.getText().toString());
                    recyclerView.scrollToPosition(pos);
                } catch (Exception e) {
                    Log.e("VlayoutActivity", e.getMessage(), e);
                }
            } else {
                recyclerView.requestLayout();
            }
        }
    });
    final VirtualLayoutManager layoutManager = new VirtualLayoutManager(this);
    recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int scrollState) {
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int i, int i2) {
            mFirstText.setText("First: " + layoutManager.findFirstVisibleItemPosition());
            mLastText.setText("Existing: " + MainViewHolder.existing + " Created: " + MainViewHolder.createdTimes);
            mCountText.setText("Count: " + recyclerView.getChildCount());
            mTotalOffsetText.setText("Total Offset: " + layoutManager.getOffsetToStart());
        }
    });
    recyclerView.setLayoutManager(layoutManager);
    // layoutManager.setReverseLayout(true);
    RecyclerView.ItemDecoration itemDecoration = new RecyclerView.ItemDecoration() {

        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            int position = ((LayoutParams) view.getLayoutParams()).getViewPosition();
            outRect.set(4, 4, 4, 4);
        }
    };
    final RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
    recyclerView.setRecycledViewPool(viewPool);
    // recyclerView.addItemDecoration(itemDecoration);
    viewPool.setMaxRecycledViews(0, 20);
    final DelegateAdapter delegateAdapter = new DelegateAdapter(layoutManager, true);
    recyclerView.setAdapter(delegateAdapter);
    List<DelegateAdapter.Adapter> adapters = new LinkedList<>();
    if (BANNER_LAYOUT) {
        adapters.add(new SubAdapter(this, new LinearLayoutHelper(), 1) {

            @Override
            public void onViewRecycled(MainViewHolder holder) {
                if (holder.itemView instanceof ViewPager) {
                    ((ViewPager) holder.itemView).setAdapter(null);
                }
            }

            @Override
            public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                if (viewType == 1)
                    return new MainViewHolder(LayoutInflater.from(OnePlusNLayoutActivity.this).inflate(R.layout.view_pager, parent, false));
                return super.onCreateViewHolder(parent, viewType);
            }

            @Override
            public int getItemViewType(int position) {
                return 1;
            }

            @Override
            protected void onBindViewHolderWithOffset(MainViewHolder holder, int position, int offsetTotal) {
            }

            @Override
            public void onBindViewHolder(MainViewHolder holder, int position) {
                if (holder.itemView instanceof ViewPager) {
                    ViewPager viewPager = (ViewPager) holder.itemView;
                    viewPager.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 200));
                    // from position to get adapter
                    viewPager.setAdapter(new PagerAdapter(this, viewPool));
                }
            }
        });
    }
    if (GRID_LAYOUT) {
        GridLayoutHelper layoutHelper;
        layoutHelper = new GridLayoutHelper(4);
        layoutHelper.setMargin(0, 10, 0, 10);
        layoutHelper.setHGap(3);
        layoutHelper.setAspectRatio(4f);
        adapters.add(new SubAdapter(this, layoutHelper, 8));
    }
    if (HORIZONTAL_SCROLL_LAYOUT) {
    }
    if (GRID_LAYOUT) {
        GridLayoutHelper layoutHelper;
        layoutHelper = new GridLayoutHelper(2);
        layoutHelper.setMargin(0, 10, 0, 10);
        layoutHelper.setHGap(3);
        layoutHelper.setAspectRatio(3f);
        adapters.add(new SubAdapter(this, layoutHelper, 2));
    }
    if (ONEN_LAYOUT) {
        OnePlusNLayoutHelper helper = new OnePlusNLayoutHelper();
        helper.setBgColor(0xff876384);
        helper.setMargin(10, 10, 10, 10);
        helper.setPadding(10, 10, 10, 10);
        adapters.add(new SubAdapter(this, helper, 3) {

            @Override
            public void onBindViewHolder(MainViewHolder holder, int position) {
                super.onBindViewHolder(holder, position);
            //                    LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 300);
            //                    layoutParams.leftMargin = 10;
            //                    layoutParams.topMargin = 10;
            //                    layoutParams.rightMargin = 10;
            //                    layoutParams.bottomMargin = 10;
            //                    holder.itemView.setLayoutParams(layoutParams);
            }
        });
    }
    if (ONEN_LAYOUT) {
        OnePlusNLayoutHelper helper = new OnePlusNLayoutHelper();
        helper.setBgColor(0xff876384);
        helper.setMargin(0, 10, 0, 10);
        adapters.add(new SubAdapter(this, helper, 4));
    }
    if (ONEN_LAYOUT) {
        OnePlusNLayoutHelper helper = new OnePlusNLayoutHelper();
        helper.setBgColor(0xff876384);
        helper.setMargin(0, 10, 0, 10);
        adapters.add(new SubAdapter(this, helper, 5));
    }
    if (ONEN_LAYOUT) {
        OnePlusNLayoutHelperEx helper = new OnePlusNLayoutHelperEx();
        helper.setBgColor(0xff876384);
        helper.setMargin(0, 10, 0, 10);
        adapters.add(new SubAdapter(this, helper, 5));
    }
    if (ONEN_LAYOUT) {
        OnePlusNLayoutHelperEx helper = new OnePlusNLayoutHelperEx();
        helper.setBgColor(0xff876384);
        helper.setMargin(0, 10, 0, 10);
        helper.setColWeights(new float[] { 40f, 45f, 15f, 60f, 0f });
        adapters.add(new SubAdapter(this, helper, 5));
    }
    if (ONEN_LAYOUT) {
        OnePlusNLayoutHelperEx helper = new OnePlusNLayoutHelperEx();
        helper.setBgColor(0xff876384);
        helper.setMargin(0, 10, 0, 10);
        helper.setColWeights(new float[] { 20f, 80f, 0f, 60f, 20f });
        helper.setAspectRatio(4);
        adapters.add(new SubAdapter(this, helper, 5));
    }
    if (ONEN_LAYOUT) {
        OnePlusNLayoutHelperEx helper = new OnePlusNLayoutHelperEx();
        helper.setBgColor(0xff876384);
        helper.setMargin(0, 10, 0, 10);
        adapters.add(new SubAdapter(this, helper, 6));
    }
    if (ONEN_LAYOUT) {
        OnePlusNLayoutHelperEx helper = new OnePlusNLayoutHelperEx();
        helper.setBgColor(0xff876384);
        helper.setMargin(0, 10, 0, 10);
        adapters.add(new SubAdapter(this, helper, 7));
    }
    if (ONEN_LAYOUT) {
        OnePlusNLayoutHelperEx helper = new OnePlusNLayoutHelperEx();
        helper.setBgColor(0xff876384);
        helper.setMargin(0, 10, 0, 10);
        helper.setColWeights(new float[] { 40f, 45f, 15f, 60f, 0f, 30f, 30f });
        adapters.add(new SubAdapter(this, helper, 7));
    }
    if (ONEN_LAYOUT) {
        OnePlusNLayoutHelperEx helper = new OnePlusNLayoutHelperEx();
        helper.setBgColor(0xffed7612);
        //            helper.setMargin(10, 10, 10, 10);
        //            helper.setPadding(10, 10, 10, 10);
        helper.setColWeights(new float[] { 30f, 20f, 50f, 40f, 30f, 35f, 35f });
        adapters.add(new SubAdapter(this, helper, 7) {

            @Override
            public void onBindViewHolder(MainViewHolder holder, int position) {
                super.onBindViewHolder(holder, position);
            //                    LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 300);
            //                    layoutParams.leftMargin = 10;
            //                    layoutParams.topMargin = 10;
            //                    layoutParams.rightMargin = 10;
            //                    layoutParams.bottomMargin = 10;
            //                    holder.itemView.setLayoutParams(layoutParams);
            }
        });
    }
    if (STICKY_LAYOUT) {
        StickyLayoutHelper layoutHelper = new StickyLayoutHelper();
        layoutHelper.setAspectRatio(4);
        adapters.add(new SubAdapter(this, layoutHelper, 1, new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 100)));
    }
    if (SCROLL_FIX_LAYOUT) {
        ScrollFixLayoutHelper layoutHelper = new ScrollFixLayoutHelper(FixLayoutHelper.BOTTOM_RIGHT, 20, 20);
        layoutHelper.setShowType(ScrollFixLayoutHelper.SHOW_ON_LEAVE);
        adapters.add(new SubAdapter(this, layoutHelper, 1) {

            @Override
            public void onBindViewHolder(MainViewHolder holder, int position) {
                super.onBindViewHolder(holder, position);
                LayoutParams layoutParams = new LayoutParams(50, 50);
                holder.itemView.setLayoutParams(layoutParams);
            }
        });
    }
    if (LINEAR_LAYOUT)
        adapters.add(new SubAdapter(this, new LinearLayoutHelper(), 100));
    delegateAdapter.setAdapters(adapters);
    final Handler mainHandler = new Handler(Looper.getMainLooper());
    trigger = new Runnable() {

        @Override
        public void run() {
            // recyclerView.scrollToPosition(22);
            // recyclerView.getAdapter().notifyDataSetChanged();
            recyclerView.requestLayout();
        // mainHandler.postDelayed(trigger, 1000);
        }
    };
    mainHandler.postDelayed(trigger, 1000);
}
Also used : ScrollFixLayoutHelper(com.alibaba.android.vlayout.layout.ScrollFixLayoutHelper) DelegateAdapter(com.alibaba.android.vlayout.DelegateAdapter) RecyclablePagerAdapter(com.alibaba.android.vlayout.RecyclablePagerAdapter) GridLayoutHelper(com.alibaba.android.vlayout.layout.GridLayoutHelper) RecyclablePagerAdapter(com.alibaba.android.vlayout.RecyclablePagerAdapter) DelegateAdapter(com.alibaba.android.vlayout.DelegateAdapter) StickyLayoutHelper(com.alibaba.android.vlayout.layout.StickyLayoutHelper) OnePlusNLayoutHelperEx(com.alibaba.android.vlayout.layout.OnePlusNLayoutHelperEx) EditText(android.widget.EditText) Rect(android.graphics.Rect) LayoutParams(com.alibaba.android.vlayout.VirtualLayoutManager.LayoutParams) LinearLayoutHelper(com.alibaba.android.vlayout.layout.LinearLayoutHelper) ViewGroup(android.view.ViewGroup) Handler(android.os.Handler) OnePlusNLayoutHelper(com.alibaba.android.vlayout.layout.OnePlusNLayoutHelper) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) ViewPager(android.support.v4.view.ViewPager) LinkedList(java.util.LinkedList) RecyclerView(android.support.v7.widget.RecyclerView) VirtualLayoutManager(com.alibaba.android.vlayout.VirtualLayoutManager)

Example 77 with LayoutParams

use of android.support.v7.widget.RecyclerView.LayoutParams in project vlayout by alibaba.

the class VirtualLayoutManager method generateLayoutView.

@Override
public final View generateLayoutView() {
    if (mRecyclerView == null)
        return null;
    View layoutView = mLayoutViewFatory.generateLayoutView(mRecyclerView.getContext());
    LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    attachViewHolder(params, new LayoutViewHolder(layoutView));
    layoutView.setLayoutParams(params);
    return layoutView;
}
Also used : View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView)

Example 78 with LayoutParams

use of android.support.v7.widget.RecyclerView.LayoutParams in project vlayout by alibaba.

the class ColumnLayoutHelper method layoutViews.

@Override
public void layoutViews(RecyclerView.Recycler recycler, RecyclerView.State state, VirtualLayoutManager.LayoutStateWrapper layoutState, LayoutChunkResult result, LayoutManagerHelper helper) {
    // reach the end of this layout
    if (isOutOfRange(layoutState.getCurrentPosition())) {
        return;
    }
    final boolean layoutInVertical = helper.getOrientation() == VERTICAL;
    final OrientationHelper orientationHelper = helper.getMainOrientationHelper();
    final int itemCount = getItemCount();
    if (mViews == null || mViews.length != itemCount) {
        mViews = new View[itemCount];
    }
    if (mEqViews == null || mEqViews.length != itemCount) {
        mEqViews = new View[itemCount];
    } else {
        Arrays.fill(mEqViews, null);
    }
    final int count = getAllChildren(mViews, recycler, layoutState, result, helper);
    if (layoutInVertical) {
        // TODO: only handle vertical layout now
        int maxVMargin = 0;
        int lastHMargin = 0;
        int totalMargin = 0;
        for (int i = 0; i < count; i++) {
            View view = mViews[i];
            ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
            if (layoutParams instanceof RecyclerView.LayoutParams) {
                RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) layoutParams;
                params.leftMargin = Math.max(lastHMargin, params.leftMargin);
                totalMargin += params.leftMargin;
                if (i != count - 1) {
                    // not last item
                    lastHMargin = params.rightMargin;
                    params.rightMargin = 0;
                } else {
                    totalMargin += params.rightMargin;
                }
                maxVMargin = Math.max(maxVMargin, params.topMargin + params.bottomMargin);
            }
        }
        final int totalWidth = helper.getContentWidth() - helper.getPaddingLeft() - helper.getPaddingRight() - getHorizontalMargin() - getHorizontalPadding();
        final int availableWidth = totalWidth - totalMargin;
        int usedWidth = 0;
        int minHeight = Integer.MAX_VALUE;
        int uniformHeight = -1;
        if (!Float.isNaN(mAspectRatio)) {
            uniformHeight = (int) (totalWidth / mAspectRatio + 0.5f);
        }
        int eqSize = 0;
        for (int i = 0; i < count; i++) {
            View view = mViews[i];
            VirtualLayoutManager.LayoutParams params = (VirtualLayoutManager.LayoutParams) view.getLayoutParams();
            int heightSpec = helper.getChildMeasureSpec(helper.getContentHeight() - helper.getPaddingTop() - helper.getPaddingBottom(), uniformHeight > 0 ? uniformHeight : params.height, true);
            if (mWeights != null && i < mWeights.length && !Float.isNaN(mWeights[i]) && mWeights[i] >= 0) {
                // calculate width with weight in percentage
                int resizeWidth = (int) (mWeights[i] * 1.0f / 100 * availableWidth + 0.5f);
                if (!Float.isNaN(params.mAspectRatio)) {
                    int specialHeight = (int) (resizeWidth / params.mAspectRatio + 0.5f);
                    heightSpec = View.MeasureSpec.makeMeasureSpec(specialHeight, View.MeasureSpec.EXACTLY);
                }
                helper.measureChild(view, View.MeasureSpec.makeMeasureSpec(resizeWidth, View.MeasureSpec.EXACTLY), heightSpec);
                // add width into usedWidth
                usedWidth += resizeWidth;
                // find minHeight
                minHeight = Math.min(minHeight, view.getMeasuredHeight());
            } else {
                mEqViews[eqSize++] = view;
            }
        }
        for (int i = 0; i < eqSize; i++) {
            View view = mEqViews[i];
            VirtualLayoutManager.LayoutParams params = (VirtualLayoutManager.LayoutParams) view.getLayoutParams();
            int heightSpec;
            int resizeWidth = (int) ((availableWidth - usedWidth) * 1.0f / eqSize + 0.5f);
            if (!Float.isNaN(params.mAspectRatio)) {
                int specialHeight = (int) (resizeWidth / params.mAspectRatio + 0.5f);
                heightSpec = View.MeasureSpec.makeMeasureSpec(specialHeight, View.MeasureSpec.EXACTLY);
            } else {
                heightSpec = helper.getChildMeasureSpec(helper.getContentHeight() - helper.getPaddingTop() - helper.getPaddingBottom(), uniformHeight > 0 ? uniformHeight : params.height, true);
            }
            //if cols' length is less than view's count, then remainder views share the rest space
            helper.measureChild(view, View.MeasureSpec.makeMeasureSpec(resizeWidth, View.MeasureSpec.EXACTLY), heightSpec);
            // find minHeight
            minHeight = Math.min(minHeight, view.getMeasuredHeight());
        }
        // uniform all views into min height
        for (int i = 0; i < count; i++) {
            View view = mViews[i];
            if (view.getMeasuredHeight() != minHeight) {
                //noinspection ResourceType
                helper.measureChild(view, View.MeasureSpec.makeMeasureSpec(view.getMeasuredWidth(), View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(minHeight, View.MeasureSpec.EXACTLY));
            }
        }
        result.mConsumed = minHeight + maxVMargin + getVerticalMargin() + getVerticalPadding();
        calculateRect(minHeight + maxVMargin, mTempArea, layoutState, helper);
        // do layout
        int left = mTempArea.left;
        for (int i = 0; i < count; i++) {
            View view = mViews[i];
            int top = mTempArea.top, bottom = mTempArea.bottom;
            int right = left + orientationHelper.getDecoratedMeasurementInOther(view);
            layoutChild(view, left, top, right, bottom, helper);
            left = right;
        }
    }
    Arrays.fill(mViews, null);
    Arrays.fill(mEqViews, null);
}
Also used : OrientationHelper(android.support.v7.widget.OrientationHelper) ViewGroup(android.view.ViewGroup) RecyclerView(android.support.v7.widget.RecyclerView) VirtualLayoutManager(com.alibaba.android.vlayout.VirtualLayoutManager) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View)

Example 79 with LayoutParams

use of android.support.v7.widget.RecyclerView.LayoutParams in project vlayout by alibaba.

the class GridLayoutHelper method layoutViews.

@Override
public void layoutViews(RecyclerView.Recycler recycler, RecyclerView.State state, LayoutStateWrapper layoutState, LayoutChunkResult result, LayoutManagerHelper helper) {
    // reach the end of this layout
    if (isOutOfRange(layoutState.getCurrentPosition())) {
        return;
    }
    boolean isStartLine = false, isEndLine = false;
    final int currentPosition = layoutState.getCurrentPosition();
    final int itemDirection = layoutState.getItemDirection();
    final boolean layingOutInPrimaryDirection = itemDirection == LayoutStateWrapper.ITEM_DIRECTION_TAIL;
    OrientationHelper orientationHelper = helper.getMainOrientationHelper();
    final boolean layoutInVertical = helper.getOrientation() == VERTICAL;
    if (layoutInVertical) {
        mTotalSize = helper.getContentWidth() - helper.getPaddingRight() - helper.getPaddingLeft() - getHorizontalMargin() - getHorizontalPadding();
        mSizePerSpan = (int) ((mTotalSize - (mSpanCount - 1) * mHGap) * 1.0f / mSpanCount + 0.5f);
    } else {
        mTotalSize = helper.getContentHeight() - helper.getPaddingBottom() - helper.getPaddingTop() - getVerticalMargin() - getVerticalPadding();
        mSizePerSpan = (int) ((mTotalSize - (mSpanCount - 1) * mVGap) * 1.0f / mSpanCount + 0.5f);
    }
    int count = 0;
    int consumedSpanCount = 0;
    int remainingSpan = mSpanCount;
    ensureSpanCount();
    if (!layingOutInPrimaryDirection) {
        // fill the remaining spacing this row
        int itemSpanIndex = getSpanIndex(recycler, state, layoutState.getCurrentPosition());
        int itemSpanSize = getSpanSize(recycler, state, layoutState.getCurrentPosition());
        remainingSpan = itemSpanIndex + itemSpanSize;
        // should find the last element of this row
        if (itemSpanIndex != mSpanCount - 1) {
            int index = layoutState.getCurrentPosition();
            int revRemainingSpan = mSpanCount - remainingSpan;
            while (count < mSpanCount && revRemainingSpan > 0) {
                // go reverse direction to find views fill current row
                index -= itemDirection;
                if (isOutOfRange(index)) {
                    break;
                }
                final int spanSize = getSpanSize(recycler, state, index);
                if (spanSize > mSpanCount) {
                    throw new IllegalArgumentException("Item at position " + index + " requires " + spanSize + " spans but GridLayoutManager has only " + mSpanCount + " spans.");
                }
                View view = layoutState.retrieve(recycler, index);
                if (view == null)
                    break;
                if (!isStartLine) {
                    isStartLine = helper.getReverseLayout() ? index == getRange().getUpper() : index == getRange().getLower();
                }
                if (!isEndLine) {
                    isEndLine = helper.getReverseLayout() ? index == getRange().getLower() : index == getRange().getUpper();
                }
                revRemainingSpan -= spanSize;
                if (revRemainingSpan < 0)
                    break;
                consumedSpanCount += spanSize;
                mSet[count] = view;
                count++;
            }
            if (count > 0) {
                // reverse array
                int s = 0, e = count - 1;
                while (s < e) {
                    View temp = mSet[s];
                    mSet[s] = mSet[e];
                    mSet[e] = temp;
                    s++;
                    e--;
                }
            }
        }
    }
    while (count < mSpanCount && layoutState.hasMore(state) && remainingSpan > 0) {
        int pos = layoutState.getCurrentPosition();
        if (isOutOfRange(pos)) {
            if (DEBUG)
                Log.d(TAG, "pos [" + pos + "] is out of range");
            break;
        }
        final int spanSize = getSpanSize(recycler, state, pos);
        if (spanSize > mSpanCount) {
            throw new IllegalArgumentException("Item at position " + pos + " requires " + spanSize + " spans but GridLayoutManager has only " + mSpanCount + " spans.");
        }
        remainingSpan -= spanSize;
        if (remainingSpan < 0) {
            // item did not fit into this row or column
            break;
        }
        View view = layoutState.next(recycler);
        if (view == null) {
            break;
        }
        if (!isStartLine) {
            isStartLine = helper.getReverseLayout() ? pos == getRange().getUpper().intValue() : pos == getRange().getLower().intValue();
        }
        if (!isEndLine) {
            isEndLine = helper.getReverseLayout() ? pos == getRange().getLower().intValue() : pos == getRange().getUpper().intValue();
        }
        consumedSpanCount += spanSize;
        mSet[count] = view;
        count++;
    }
    if (count == 0) {
        return;
    }
    int maxSize = 0;
    // we should assign spans before item decor offsets are calculated
    assignSpans(recycler, state, count, consumedSpanCount, layingOutInPrimaryDirection, helper);
    if (remainingSpan > 0 && mIsAutoExpand) {
        if (layoutInVertical) {
            mSizePerSpan = (helper.getContentWidth() - helper.getPaddingRight() - getHorizontalMargin() - getHorizontalPadding() - helper.getPaddingLeft() - (count - 1) * mHGap) / count;
        } else {
            mSizePerSpan = (helper.getContentHeight() - helper.getPaddingBottom() - getVerticalMargin() - getVerticalPadding() - helper.getPaddingTop() - (count - 1) * mVGap) / count;
        }
    }
    boolean weighted = false;
    if (mWeights != null && mWeights.length > 0) {
        weighted = true;
        int totalSpace;
        if (layoutInVertical) {
            totalSpace = helper.getContentWidth() - helper.getPaddingLeft() - helper.getPaddingRight() - getHorizontalMargin() - getHorizontalPadding() - (count - 1) * mHGap;
        } else {
            totalSpace = helper.getContentHeight() - helper.getPaddingTop() - helper.getPaddingBottom() - getVerticalMargin() - getVerticalPadding() - (count - 1) * mVGap;
        }
        // calculate width with weight in percentage
        int eqCnt = 0, remainingSpace = totalSpace;
        int colCnt = (remainingSpan > 0 && mIsAutoExpand) ? count : mSpanCount;
        for (int i = 0; i < colCnt; i++) {
            if (i < mWeights.length && !Float.isNaN(mWeights[i]) && mWeights[i] >= 0) {
                float weight = mWeights[i];
                mSpanCols[i] = (int) (weight * 1.0f / 100 * totalSpace + 0.5f);
                remainingSpace -= mSpanCols[i];
            } else {
                eqCnt++;
                mSpanCols[i] = -1;
            }
        }
        if (eqCnt > 0) {
            int eqLength = remainingSpace / eqCnt;
            for (int i = 0; i < colCnt; i++) {
                if (mSpanCols[i] < 0) {
                    mSpanCols[i] = eqLength;
                }
            }
        }
    }
    for (int i = 0; i < count; i++) {
        View view = mSet[i];
        helper.addChildView(layoutState, view, layingOutInPrimaryDirection ? -1 : 0);
        int spanSize = getSpanSize(recycler, state, helper.getPosition(view)), spec;
        if (weighted) {
            final int index = mSpanIndices[i];
            int spanLength = 0;
            for (int j = 0; j < spanSize; j++) {
                spanLength += mSpanCols[j + index];
            }
            spec = View.MeasureSpec.makeMeasureSpec(Math.max(0, spanLength), View.MeasureSpec.EXACTLY);
        } else {
            spec = View.MeasureSpec.makeMeasureSpec(mSizePerSpan * spanSize + Math.max(0, spanSize - 1) * (layoutInVertical ? mHGap : mVGap), View.MeasureSpec.EXACTLY);
        }
        final VirtualLayoutManager.LayoutParams lp = (VirtualLayoutManager.LayoutParams) view.getLayoutParams();
        if (helper.getOrientation() == VERTICAL) {
            helper.measureChild(view, spec, getMainDirSpec(lp.height, mTotalSize, View.MeasureSpec.getSize(spec), lp.mAspectRatio));
        } else {
            helper.measureChild(view, getMainDirSpec(lp.width, mTotalSize, View.MeasureSpec.getSize(spec), lp.mAspectRatio), View.MeasureSpec.getSize(spec));
        }
        final int size = orientationHelper.getDecoratedMeasurement(view);
        if (size > maxSize) {
            maxSize = size;
        }
    }
    // views that did not measure the maxSize has to be re-measured
    final int maxMeasureSpec = getMainDirSpec(maxSize, mTotalSize, 0, Float.NaN);
    for (int i = 0; i < count; i++) {
        final View view = mSet[i];
        if (orientationHelper.getDecoratedMeasurement(view) != maxSize) {
            int spanSize = getSpanSize(recycler, state, helper.getPosition(view)), spec;
            if (weighted) {
                final int index = mSpanIndices[i];
                int spanLength = 0;
                for (int j = 0; j < spanSize; j++) {
                    spanLength += mSpanCols[j + index];
                }
                spec = View.MeasureSpec.makeMeasureSpec(Math.max(0, spanLength), View.MeasureSpec.EXACTLY);
            } else {
                spec = View.MeasureSpec.makeMeasureSpec(mSizePerSpan * spanSize + Math.max(0, spanSize - 1) * (layoutInVertical ? mHGap : mVGap), View.MeasureSpec.EXACTLY);
            }
            if (helper.getOrientation() == VERTICAL) {
                helper.measureChild(view, spec, maxMeasureSpec);
            } else {
                helper.measureChild(view, maxMeasureSpec, spec);
            }
        }
    }
    int startSpace = 0, endSpace = 0;
    if (isStartLine) {
        startSpace = layoutInVertical ? mMarginTop + mPaddingTop : mMarginLeft + mPaddingLeft;
    }
    if (isEndLine) {
        endSpace = layoutInVertical ? mMarginBottom + mPaddingBottom : mMarginRight + mPaddingRight;
    }
    result.mConsumed = maxSize + startSpace + endSpace;
    final boolean layoutStart = layoutState.getLayoutDirection() == LayoutStateWrapper.LAYOUT_START;
    if (!mLayoutWithAnchor && (!isEndLine || !layoutStart) && (!isStartLine || layoutStart)) {
        result.mConsumed += (layoutInVertical ? mVGap : mHGap);
    }
    int left = 0, right = 0, top = 0, bottom = 0;
    if (layoutInVertical) {
        if (layoutState.getLayoutDirection() == LayoutStateWrapper.LAYOUT_START) {
            bottom = layoutState.getOffset() - endSpace - ((mLayoutWithAnchor || isEndLine) ? 0 : mVGap);
            top = bottom - maxSize;
        } else {
            top = layoutState.getOffset() + startSpace + ((mLayoutWithAnchor || isStartLine) ? 0 : mVGap);
            bottom = top + maxSize;
        }
    } else {
        if (layoutState.getLayoutDirection() == LayoutStateWrapper.LAYOUT_START) {
            right = layoutState.getOffset() - endSpace - (mLayoutWithAnchor || isEndLine ? 0 : mHGap);
            left = right - maxSize;
        } else {
            left = layoutState.getOffset() + startSpace + (mLayoutWithAnchor || isStartLine ? 0 : mHGap);
            right = left + maxSize;
        }
    }
    for (int i = 0; i < count; i++) {
        View view = mSet[i];
        final int index = mSpanIndices[i];
        LayoutParams params = (LayoutParams) view.getLayoutParams();
        if (layoutInVertical) {
            if (weighted) {
                left = helper.getPaddingLeft() + mMarginLeft + mPaddingLeft;
                for (int j = 0; j < index; j++) left += mSpanCols[j] + mHGap;
            } else
                left = helper.getPaddingLeft() + mMarginLeft + mPaddingLeft + mSizePerSpan * index + index * mHGap;
            right = left + orientationHelper.getDecoratedMeasurementInOther(view);
        } else {
            if (weighted) {
                top = helper.getPaddingTop() + mMarginTop + mPaddingTop;
                for (int j = 0; j < index; j++) top += mSpanCols[j] + mVGap;
            } else
                top = helper.getPaddingTop() + mMarginTop + mPaddingTop + mSizePerSpan * index + index * mVGap;
            bottom = top + orientationHelper.getDecoratedMeasurementInOther(view);
        }
        if (DEBUG) {
            Log.d(TAG, "layout item in position: " + params.getViewPosition() + " with text " + ((TextView) view).getText() + " with SpanIndex: " + index + " into (" + left + ", " + top + ", " + right + ", " + bottom + " )");
        }
        // We calculate everything with View's bounding box (which includes decor and margins)
        // To calculate correct layout position, we subtract margins.
        // modified by huifeng at 20160907, margins are already subtracted
        layoutChild(view, left, top, right, bottom, helper);
        // Consume the available space if the view is not removed OR changed
        if (params.isItemRemoved() || params.isItemChanged()) {
            result.mIgnoreConsumed = true;
        }
        result.mFocusable |= view.isFocusable();
    }
    mLayoutWithAnchor = false;
    Arrays.fill(mSet, null);
    Arrays.fill(mSpanIndices, 0);
    Arrays.fill(mSpanCols, 0);
}
Also used : LayoutParams(com.alibaba.android.vlayout.VirtualLayoutManager.LayoutParams) OrientationHelper(android.support.v7.widget.OrientationHelper) LayoutParams(com.alibaba.android.vlayout.VirtualLayoutManager.LayoutParams) VirtualLayoutManager(com.alibaba.android.vlayout.VirtualLayoutManager) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) View(android.view.View)

Example 80 with LayoutParams

use of android.support.v7.widget.RecyclerView.LayoutParams in project vlayout by alibaba.

the class StaggeredGridLayoutHelper method recycleFromStart.

private void recycleFromStart(RecyclerView.Recycler recycler, int line, LayoutManagerHelper helper) {
    final OrientationHelper orientationHelper = helper.getMainOrientationHelper();
    boolean changed = true;
    while (helper.getChildCount() > 0 && changed) {
        View child = helper.getChildAt(0);
        if (child != null && orientationHelper.getDecoratedEnd(child) < line) {
            LayoutParams lp = (LayoutParams) child.getLayoutParams();
            int position = lp.getViewPosition();
            Span span = findSpan(position, child, true);
            if (span != null) {
                span.popStart(orientationHelper);
                helper.removeChildView(child);
                recycler.recycleView(child);
            } else {
                changed = false;
            }
        } else {
            // done
            return;
        }
    }
}
Also used : LayoutParams(android.support.v7.widget.RecyclerView.LayoutParams) OrientationHelper(android.support.v7.widget.OrientationHelper) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View)

Aggregations

View (android.view.View)77 RecyclerView (android.support.v7.widget.RecyclerView)67 ViewGroup (android.view.ViewGroup)21 TextView (android.widget.TextView)21 ImageView (android.widget.ImageView)13 LayoutParams (android.support.v7.widget.RecyclerView.LayoutParams)12 Toolbar (android.support.v7.widget.Toolbar)9 FrameLayout (android.widget.FrameLayout)9 Rect (android.graphics.Rect)8 StaggeredGridLayoutManager (android.support.v7.widget.StaggeredGridLayoutManager)8 BindView (butterknife.BindView)8 ActionMenuView (android.support.v7.widget.ActionMenuView)7 MarginLayoutParams (android.view.ViewGroup.MarginLayoutParams)6 LinearLayout (android.widget.LinearLayout)6 CollapsibleActionView (android.support.v7.view.CollapsibleActionView)5 MenuView (android.support.v7.view.menu.MenuView)5 EditText (android.widget.EditText)5 ImageView (carbon.widget.ImageView)5 TextView (carbon.widget.TextView)5 VirtualLayoutManager (com.alibaba.android.vlayout.VirtualLayoutManager)5