Java Code Examples for android.view.View#isLayoutRequested()

The following examples show how to use android.view.View#isLayoutRequested() . 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: MultipleOrientationSlidingDrawer.java    From prayer-times-android with Apache License 2.0 6 votes vote down vote up
private void prepareContent() {

        if (mAnimating) {
            return;
        }

        // Something changed in the content, we need to honor the layout request
        // before creating the cached bitmap
        View content = mContent;
        if (content.isLayoutRequested()) {
            measureContent();
        }

        // Try only once... we should really loop but it's not a big deal
        // if the draw was cancelled, it will only be temporary anyway
        content.getViewTreeObserver().dispatchOnPreDraw();
        content.buildDrawingCache();

        content.setVisibility(View.GONE);
    }
 
Example 2
Source File: MultipleOrientationSlidingDrawer.java    From NYU-BusTracker-Android with Apache License 2.0 6 votes vote down vote up
private void prepareContent() {

        if (mAnimating) {
            return;
        }

        // Something changed in the content, we need to honor the layout request
        // before creating the cached bitmap
        final View content = mContent;
        if (content.isLayoutRequested()) {
            measureContent();
        }

        // Try only once... we should really loop but it's not a big deal
        // if the draw was cancelled, it will only be temporary anyway
        content.getViewTreeObserver().dispatchOnPreDraw();
        content.buildDrawingCache();

        content.setVisibility(View.GONE);
    }
 
Example 3
Source File: MonthTitleView.java    From MonthView with Apache License 2.0 5 votes vote down vote up
protected void makeAndAddCellView(int left, int top, int index) {
	final boolean recycled = !scrapTitleViews.isEmpty();
	final View child = recycled ? (scrapTitleViews.remove(0)) : (adapter.createTitleView(this));
	adapter.bindTitleView(child, index);
	
	LayoutParams lp = child.getLayoutParams();
	if(lp == null){
		lp = new LayoutParams(titleWidth, titleHeight);
	}
	
	if(recycled){
		attachViewToParent(child, index, lp);
	}else{
		addViewInLayout(child, index, lp, true);
	}
	
	final boolean needToMeasure = !recycled || child.isLayoutRequested();
	
	if (needToMeasure) {
	    final int childWidthSpec = getChildMeasureSpec(
	            MeasureSpec.makeMeasureSpec(titleWidth, MeasureSpec.EXACTLY), 0, lp.width);
	    final int childHeightSpec = getChildMeasureSpec(
	            MeasureSpec.makeMeasureSpec(titleHeight, MeasureSpec.EXACTLY), 0, lp.height);
	    child.measure(childWidthSpec, childHeightSpec);
	} else {
	    cleanupLayoutState(child);
	}
	
	if (needToMeasure) {
		child.layout(left, top,left + titleWidth, top + titleHeight);
	} else {
		child.offsetLeftAndRight(left - child.getLeft());
		child.offsetTopAndBottom(top - child.getTop());
	}
}
 
Example 4
Source File: DragSortItemView.java    From Overchan-Android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 
 */
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    
    int height = MeasureSpec.getSize(heightMeasureSpec);
    int width = MeasureSpec.getSize(widthMeasureSpec);

    int heightMode = MeasureSpec.getMode(heightMeasureSpec);

    final View child = getChildAt(0);
    if (child == null) {
        setMeasuredDimension(0, width);
        return;
    }

    if (child.isLayoutRequested()) {
        // Always let child be as tall as it wants.
        measureChild(child, widthMeasureSpec,
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    }

    if (heightMode == MeasureSpec.UNSPECIFIED) {
        ViewGroup.LayoutParams lp = getLayoutParams();

        if (lp.height > 0) {
            height = lp.height;
        } else {
            height = child.getMeasuredHeight();
        }
    }

    setMeasuredDimension(width, height);
}
 
Example 5
Source File: DragSortItemView.java    From google-authenticator-android with Apache License 2.0 5 votes vote down vote up
/**
 * 
 */
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    
    int height = MeasureSpec.getSize(heightMeasureSpec);
    int width = MeasureSpec.getSize(widthMeasureSpec);

    int heightMode = MeasureSpec.getMode(heightMeasureSpec);

    final View child = getChildAt(0);
    if (child == null) {
        setMeasuredDimension(0, width);
        return;
    }

    if (child.isLayoutRequested()) {
        // Always let child be as tall as it wants.
        measureChild(child, widthMeasureSpec,
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    }

    if (heightMode == MeasureSpec.UNSPECIFIED) {
        ViewGroup.LayoutParams lp = getLayoutParams();

        if (lp.height > 0) {
            height = lp.height;
        } else {
            height = child.getMeasuredHeight();
        }
    }

    setMeasuredDimension(width, height);
}
 
Example 6
Source File: CoordinatorLayout.java    From ticdesign with Apache License 2.0 5 votes vote down vote up
/**
 * Get the position rect for the given child. If the child has currently requested layout
 * or has a visibility of GONE.
 *
 * @param child child view to check
 * @param transform true to include transformation in the output rect, false to
 *                        only account for the base position
 * @param out rect to set to the output values
 */
void getChildRect(View child, boolean transform, Rect out) {
    if (child.isLayoutRequested() || child.getVisibility() == View.GONE) {
        out.set(0, 0, 0, 0);
        return;
    }
    if (transform) {
        getDescendantRect(child, out);
    } else {
        out.set(child.getLeft(), child.getTop(), child.getRight(), child.getBottom());
    }
}
 
Example 7
Source File: DragSortItemView.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
/**
 * 
 */
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    
    int height = MeasureSpec.getSize(heightMeasureSpec);
    int width = MeasureSpec.getSize(widthMeasureSpec);

    int heightMode = MeasureSpec.getMode(heightMeasureSpec);

    final View child = getChildAt(0);
    if (child == null) {
        setMeasuredDimension(0, width);
        return;
    }

    if (child.isLayoutRequested()) {
        // Always let child be as tall as it wants.
        measureChild(child, widthMeasureSpec,
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    }

    if (heightMode == MeasureSpec.UNSPECIFIED) {
        LayoutParams lp = getLayoutParams();

        if (lp.height > 0) {
            height = lp.height;
        } else {
            height = child.getMeasuredHeight();
        }
    }

    setMeasuredDimension(width, height);
}
 
Example 8
Source File: DragSortItemView.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
/**
 * 
 */
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    
    int height = MeasureSpec.getSize(heightMeasureSpec);
    int width = MeasureSpec.getSize(widthMeasureSpec);

    int heightMode = MeasureSpec.getMode(heightMeasureSpec);

    final View child = getChildAt(0);
    if (child == null) {
        setMeasuredDimension(0, width);
        return;
    }

    if (child.isLayoutRequested()) {
        // Always let child be as tall as it wants.
        measureChild(child, widthMeasureSpec,
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    }

    if (heightMode == MeasureSpec.UNSPECIFIED) {
        LayoutParams lp = getLayoutParams();

        if (lp.height > 0) {
            height = lp.height;
        } else {
            height = child.getMeasuredHeight();
        }
    }

    setMeasuredDimension(width, height);
}
 
Example 9
Source File: MonthView.java    From MonthView with Apache License 2.0 5 votes vote down vote up
/**
 * @param left
 * @param top
 * @param index
 */
protected void makeAndAddCellView(int left, int top, int index) {
	final boolean recycled = !scrapCellViews.isEmpty();
	final View child = recycled ? (scrapCellViews.remove(0)) : (adapter.createCellView(this, index));
	adapter.bindCellView(this, child, index, calendar);
	
	LayoutParams lp = child.getLayoutParams();
	if(lp == null){
		lp = new LayoutParams(cellWidth, cellHeight);
	}
	
	if(recycled){
		attachViewToParent(child, index, lp);
	}else{
		addViewInLayout(child, index, lp, true);
	}
	
	final boolean needToMeasure = !recycled || child.isLayoutRequested();
	
	if (needToMeasure) {
	    final int childWidthSpec = getChildMeasureSpec(
	            MeasureSpec.makeMeasureSpec(cellWidth, MeasureSpec.EXACTLY), 0, lp.width);
	    final int childHeightSpec = getChildMeasureSpec(
	            MeasureSpec.makeMeasureSpec(cellHeight, MeasureSpec.EXACTLY), 0, lp.height);
	    child.measure(childWidthSpec, childHeightSpec);
	} else {
	    cleanupLayoutState(child);
	}
	
	if (needToMeasure) {
		child.layout(left, top,left + cellWidth, top + cellHeight);
	} else {
		child.offsetLeftAndRight(left - child.getLeft());
		child.offsetTopAndBottom(top - child.getTop());
	}
}
 
Example 10
Source File: DragSortItemView.java    From chromadoze with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 
 */
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    int height = MeasureSpec.getSize(heightMeasureSpec);
    int width = MeasureSpec.getSize(widthMeasureSpec);

    int heightMode = MeasureSpec.getMode(heightMeasureSpec);

    final View child = getChildAt(0);
    if (child == null) {
        setMeasuredDimension(0, width);
        return;
    }

    if (child.isLayoutRequested()) {
        // Always let child be as tall as it wants.
        measureChild(child, widthMeasureSpec,
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    }

    if (heightMode == MeasureSpec.UNSPECIFIED) {
        ViewGroup.LayoutParams lp = getLayoutParams();

        if (lp.height > 0) {
            height = lp.height;
        } else {
            height = child.getMeasuredHeight();
        }
    }

    setMeasuredDimension(width, height);
}
 
Example 11
Source File: PinnedHeaderListView.java    From Yahala-Messenger with MIT License 5 votes vote down vote up
private void ensurePinnedHeaderLayout(View header, boolean forceLayout) {
    if (header.isLayoutRequested() || forceLayout) {
        int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), mWidthMode);

        int heightSpec;
        ViewGroup.LayoutParams layoutParams = header.getLayoutParams();
        if (layoutParams != null && layoutParams.height > 0) {
            heightSpec = MeasureSpec.makeMeasureSpec(layoutParams.height, MeasureSpec.EXACTLY);
        } else {
            heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        }
        header.measure(widthSpec, heightSpec);
        header.layout(0, 0, header.getMeasuredWidth(), header.getMeasuredHeight());
    }
}
 
Example 12
Source File: DragSortItemView.java    From android-kernel-tweaker with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 
 */
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    
    int height = MeasureSpec.getSize(heightMeasureSpec);
    int width = MeasureSpec.getSize(widthMeasureSpec);

    int heightMode = MeasureSpec.getMode(heightMeasureSpec);

    final View child = getChildAt(0);
    if (child == null) {
        setMeasuredDimension(0, width);
        return;
    }

    if (child.isLayoutRequested()) {
        // Always let child be as tall as it wants.
        measureChild(child, widthMeasureSpec,
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    }

    if (heightMode == MeasureSpec.UNSPECIFIED) {
        ViewGroup.LayoutParams lp = getLayoutParams();

        if (lp.height > 0) {
            height = lp.height;
        } else {
            height = child.getMeasuredHeight();
        }
    }

    setMeasuredDimension(width, height);
}
 
Example 13
Source File: PinnedHeaderListView.java    From FoodOrdering with Apache License 2.0 5 votes vote down vote up
private void ensurePinnedHeaderLayout(View header) {
    if (header.isLayoutRequested()) {
        int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), mWidthMode);

        int heightSpec;
        ViewGroup.LayoutParams layoutParams = header.getLayoutParams();
        if (layoutParams != null && layoutParams.height > 0) {
            heightSpec = MeasureSpec.makeMeasureSpec(layoutParams.height, MeasureSpec.EXACTLY);
        } else {
            heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        }
        header.measure(widthSpec, heightSpec);
        header.layout(0, 0, header.getMeasuredWidth(), header.getMeasuredHeight());
    }
}
 
Example 14
Source File: DragSortItemView.java    From simpletask-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 
 */
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    
    int height = MeasureSpec.getSize(heightMeasureSpec);
    int width = MeasureSpec.getSize(widthMeasureSpec);

    int heightMode = MeasureSpec.getMode(heightMeasureSpec);

    final View child = getChildAt(0);
    if (child == null) {
        setMeasuredDimension(width, 0);
        return;
    }

    if (child.isLayoutRequested()) {
        // Always let child be as tall as it wants.
        measureChild(child, widthMeasureSpec,
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    }

    if (heightMode == MeasureSpec.UNSPECIFIED) {
        ViewGroup.LayoutParams lp = getLayoutParams();

        if (lp.height > 0) {
            height = lp.height;
        } else {
            height = child.getMeasuredHeight();
        }
    }

    setMeasuredDimension(width, height);
}
 
Example 15
Source File: DragSortItemView.java    From biermacht with Apache License 2.0 5 votes vote down vote up
/**
 *
 */
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

  int height = MeasureSpec.getSize(heightMeasureSpec);
  int width = MeasureSpec.getSize(widthMeasureSpec);

  int heightMode = MeasureSpec.getMode(heightMeasureSpec);

  final View child = getChildAt(0);
  if (child == null) {
    setMeasuredDimension(0, width);
    return;
  }

  if (child.isLayoutRequested()) {
    // Always let child be as tall as it wants.
    measureChild(child, widthMeasureSpec,
                 MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
  }

  if (heightMode == MeasureSpec.UNSPECIFIED) {
    LayoutParams lp = getLayoutParams();

    if (lp.height > 0) {
      height = lp.height;
    }
    else {
      height = child.getMeasuredHeight();
    }
  }

  setMeasuredDimension(width, height);
}
 
Example 16
Source File: SwipeFlingAdapterView.java    From Social with Apache License 2.0 4 votes vote down vote up
/**
 * 绘制子View
 *
 * @param index
 * @param child
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private void makeAndAddView(int index, View child) {

    FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) child.getLayoutParams();
    addViewInLayout(child, 0, lp, true);

    final boolean needToMeasure = child.isLayoutRequested();
    if (needToMeasure) {
        int childWidthSpec = getChildMeasureSpec(getWidthMeasureSpec(), getPaddingLeft() + getPaddingRight() + lp.leftMargin + lp.rightMargin, lp.width);
        int childHeightSpec = getChildMeasureSpec(getHeightMeasureSpec(), getPaddingTop() + getPaddingBottom() + lp.topMargin + lp.bottomMargin, lp.height);
        child.measure(childWidthSpec, childHeightSpec);
    } else {
        cleanupLayoutState(child);
    }


    int w = child.getMeasuredWidth();
    int h = child.getMeasuredHeight();
    int gravity = lp.gravity;
    if (gravity == -1) {
        gravity = Gravity.TOP | Gravity.START;
    }
    int layoutDirection = getLayoutDirection();
    final int absoluteGravity = Gravity.getAbsoluteGravity(gravity, layoutDirection);
    final int verticalGravity = gravity & Gravity.VERTICAL_GRAVITY_MASK;

    int childLeft;
    int childTop;
    switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
        case Gravity.CENTER_HORIZONTAL:
            childLeft = (getWidth() + getPaddingLeft() - getPaddingRight() - w) / 2 + lp.leftMargin - lp.rightMargin;
            break;
        case Gravity.END:
            childLeft = getWidth() + getPaddingRight() - w - lp.rightMargin;
            break;
        case Gravity.START:
        default:
            int l = 0;
            childLeft = getPaddingLeft() + lp.leftMargin + l;
            break;
    }
    switch (verticalGravity) {
        case Gravity.CENTER_VERTICAL:
            childTop = (getHeight() + getPaddingTop() - getPaddingBottom() - h) / 2 +
                    lp.topMargin - lp.bottomMargin;
            break;
        case Gravity.BOTTOM:
            childTop = getHeight() - getPaddingBottom() - h - lp.bottomMargin;
            break;
        case Gravity.TOP:
        default:
            int top = 0;
            childTop = getPaddingTop() + lp.topMargin + top;
            break;
    }

    child.layout(childLeft, childTop, childLeft + w, childTop + h);
    adjustChildView(child, index);
}
 
Example 17
Source File: HListView.java    From letv with Apache License 2.0 4 votes vote down vote up
@TargetApi(11)
private void setupChild(View child, int position, int x, boolean flowDown, int childrenTop, boolean selected, boolean recycled) {
    boolean isSelected = selected && shouldShowSelector();
    boolean updateChildSelected = isSelected != child.isSelected();
    int mode = this.mTouchMode;
    boolean isPressed = mode > 0 && mode < 3 && this.mMotionPosition == position;
    boolean updateChildPressed = isPressed != child.isPressed();
    boolean needToMeasure = !recycled || updateChildSelected || child.isLayoutRequested();
    LayoutParams p = (LayoutParams) child.getLayoutParams();
    if (p == null) {
        p = (LayoutParams) generateDefaultLayoutParams();
    }
    p.viewType = this.mAdapter.getItemViewType(position);
    if ((!recycled || p.forceAdd) && !(p.recycledHeaderFooter && p.viewType == -2)) {
        p.forceAdd = false;
        if (p.viewType == -2) {
            p.recycledHeaderFooter = true;
        }
        addViewInLayout(child, flowDown ? -1 : 0, p, true);
    } else {
        attachViewToParent(child, flowDown ? -1 : 0, p);
    }
    if (updateChildSelected) {
        child.setSelected(isSelected);
    }
    if (updateChildPressed) {
        child.setPressed(isPressed);
    }
    if (!(this.mChoiceMode == 0 || this.mCheckStates == null)) {
        if (child instanceof Checkable) {
            ((Checkable) child).setChecked(((Boolean) this.mCheckStates.get(position, Boolean.valueOf(false))).booleanValue());
        } else if (VERSION.SDK_INT >= 11) {
            child.setActivated(((Boolean) this.mCheckStates.get(position, Boolean.valueOf(false))).booleanValue());
        }
    }
    if (needToMeasure) {
        int childWidthSpec;
        int childHeightSpec = ViewGroup.getChildMeasureSpec(this.mHeightMeasureSpec, this.mListPadding.top + this.mListPadding.bottom, p.height);
        int lpWidth = p.width;
        if (lpWidth > 0) {
            childWidthSpec = MeasureSpec.makeMeasureSpec(lpWidth, 1073741824);
        } else {
            childWidthSpec = MeasureSpec.makeMeasureSpec(0, 0);
        }
        child.measure(childWidthSpec, childHeightSpec);
    } else {
        cleanupLayoutState(child);
    }
    int w = child.getMeasuredWidth();
    int h = child.getMeasuredHeight();
    int childLeft = flowDown ? x : x - w;
    if (needToMeasure) {
        child.layout(childLeft, childrenTop, childLeft + w, childrenTop + h);
    } else {
        child.offsetLeftAndRight(childLeft - child.getLeft());
        child.offsetTopAndBottom(childrenTop - child.getTop());
    }
    if (this.mCachingStarted && !child.isDrawingCacheEnabled()) {
        child.setDrawingCacheEnabled(true);
    }
    if (VERSION.SDK_INT >= 11 && recycled && ((LayoutParams) child.getLayoutParams()).scrappedFromPosition != position) {
        child.jumpDrawablesToCurrentState();
    }
}
 
Example 18
Source File: CyclePager.java    From MonthView with Apache License 2.0 4 votes vote down vote up
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
	int widthSize = MeasureSpec.getSize(widthMeasureSpec);
	int heightSize = MeasureSpec.getSize(heightMeasureSpec);
	
	if(adapter != null){
		final boolean existed = getChildCount() > 0;
		if(existed){
			setMeasuredDimension(cellWidth, cellHeight);
			return;
		}
		
		final boolean recycled = ! scrapViews.isEmpty();
		final View child = recycled ? (scrapViews.get(0)) : (adapter.createView(this, 0));
		LayoutParams lp = child.getLayoutParams();
		if(lp == null){
			lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		}
		if(recycled){
			attachViewToParent(child, 0, lp);
		}else{
			addViewInLayout(child, 0, lp, true);
		}
		
		final boolean needToMeasure = !recycled || child.isLayoutRequested();
		if (needToMeasure) {
		    final int childWidthSpec = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.AT_MOST);
		    final int childHeightSpec = MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.AT_MOST);
		    child.measure(childWidthSpec, childHeightSpec);
		}
		
		cellWidth = child.getMeasuredWidth();
		cellHeight = child.getMeasuredHeight();
		
		cellWidthWithPadding = cellWidth + cellPadding;
		
		widthSize = cellWidth;
		heightSize = cellHeight;
		
		detachViewFromParent(child);
		if(! recycled){
			scrapViews.add(child);
		}
		setMeasuredDimension(widthSize, heightSize);
	}else{
		setMeasuredDimension(widthSize, (int) (widthSize * (6f / 7)));
	}
}
 
Example 19
Source File: SwipeAdapterView.java    From SwipeAdapterView with Apache License 2.0 4 votes vote down vote up
private void makeAndAddView(View child, int index) {
    child.setAlpha(1F);
    child.setTranslationX(0);
    FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) child.getLayoutParams();
    addViewInLayout(child, 0, lp, true);

    final boolean needToMeasure = child.isLayoutRequested();
    if (needToMeasure) {
        int childWidthSpec = getChildMeasureSpec(widthMeasureSpec,
                getPaddingLeft() + getPaddingRight() + lp.leftMargin + lp.rightMargin,
                lp.width);
        int childHeightSpec = getChildMeasureSpec(heightMeasureSpec,
                getPaddingTop() + getPaddingBottom() + lp.topMargin + lp.bottomMargin,
                lp.height);
        child.measure(childWidthSpec, childHeightSpec);
    } else {
        cleanupLayoutState(child);
    }

    int w = child.getMeasuredWidth();
    int h = child.getMeasuredHeight();

    int gravity = lp.gravity;
    if (gravity == -1) {
        gravity = Gravity.TOP | Gravity.START;
    }

    int layoutDirection = 0;
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN)
        layoutDirection = getLayoutDirection();
    final int absoluteGravity = Gravity.getAbsoluteGravity(gravity, layoutDirection);
    final int verticalGravity = gravity & Gravity.VERTICAL_GRAVITY_MASK;

    int childLeft;
    int childTop;
    switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
        case Gravity.CENTER_HORIZONTAL:
            childLeft = (getWidth() + getPaddingLeft() - getPaddingRight()  - w) / 2 +
                    lp.leftMargin - lp.rightMargin;
            break;
        case Gravity.END:
            childLeft = getWidth() + getPaddingRight() - w - lp.rightMargin;
            break;
        case Gravity.START:
        default:
            childLeft = getPaddingLeft() + lp.leftMargin;
            break;
    }
    switch (verticalGravity) {
        case Gravity.CENTER_VERTICAL:
            childTop = (getHeight() + getPaddingTop() - getPaddingBottom()  - h) / 2 +
                    lp.topMargin - lp.bottomMargin;
            break;
        case Gravity.BOTTOM:
            childTop = getHeight() - getPaddingBottom() - h - lp.bottomMargin;
            break;
        case Gravity.TOP:
        default:
            childTop = getPaddingTop() + lp.topMargin;
            break;
    }
    child.layout(childLeft, childTop, childLeft + w, childTop + h);
    // 缩放层叠效果
    adjustChildView(child, index);
}
 
Example 20
Source File: TwoWayGridView.java    From recent-images with MIT License 4 votes vote down vote up
/**
 * Add a view as a child and make sure it is measured (if necessary) and
 * positioned properly.
 *
 * @param child The view to add
 * @param position The position of the view
 * @param y The y position relative to which this view will be positioned
 * @param flow if true, align top edge to y. If false, align bottom edge
 *        to y.
 * @param childrenLeft Left edge where children should be positioned
 * @param selected Is this position selected?
 * @param recycled Has this view been pulled from the recycle bin? If so it
 *        does not need to be remeasured.
 * @param where Where to add the item in the list
 *
 */
private void setupChild(View child, int position, int y, boolean flow, int childrenLeft,
		boolean selected, boolean recycled, int where) {
	boolean isSelected = selected && shouldShowSelector();
	final boolean updateChildSelected = isSelected != child.isSelected();
	final int mode = mTouchMode;
	final boolean isPressed = mode > TOUCH_MODE_DOWN && mode < TOUCH_MODE_SCROLL &&
	mMotionPosition == position;
	final boolean updateChildPressed = isPressed != child.isPressed();

	boolean needToMeasure = !recycled || updateChildSelected || child.isLayoutRequested();

	// Respect layout params that are already in the view. Otherwise make
	// some up...
	TwoWayAbsListView.LayoutParams p = (TwoWayAbsListView.LayoutParams)child.getLayoutParams();
	if (p == null) {
		p = new TwoWayAbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
				ViewGroup.LayoutParams.WRAP_CONTENT, 0);
	}
	p.viewType = mAdapter.getItemViewType(position);

	if (recycled && !p.forceAdd) {
		attachViewToParent(child, where, p);
	} else {
		p.forceAdd = false;
		addViewInLayout(child, where, p, true);
	}

	if (updateChildSelected) {
		child.setSelected(isSelected);
		if (isSelected) {
			requestFocus();
		}
	}

	if (updateChildPressed) {
		child.setPressed(isPressed);
	}

	if (needToMeasure) {
		int childHeightSpec = ViewGroup.getChildMeasureSpec(
				MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 0, p.height);

		int childWidthSpec = ViewGroup.getChildMeasureSpec(
				MeasureSpec.makeMeasureSpec(mColumnWidth, MeasureSpec.EXACTLY), 0, p.width);
		child.measure(childWidthSpec, childHeightSpec);
	} else {
		cleanupLayoutState(child);
	}

	final int w = child.getMeasuredWidth();
	final int h = child.getMeasuredHeight();

	int childLeft;
	final int childTop = flow ? y : y - h;

	switch (mGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
	case Gravity.LEFT:
		childLeft = childrenLeft;
		break;
	case Gravity.CENTER_HORIZONTAL:
		childLeft = childrenLeft + ((mColumnWidth - w) / 2);
		break;
	case Gravity.RIGHT:
		childLeft = childrenLeft + mColumnWidth - w;
		break;
	default:
		childLeft = childrenLeft;
		break;
	}

	if (needToMeasure) {
		final int childRight = childLeft + w;
		final int childBottom = childTop + h;
		child.layout(childLeft, childTop, childRight, childBottom);
	} else {
		child.offsetLeftAndRight(childLeft - child.getLeft());
		child.offsetTopAndBottom(childTop - child.getTop());
	}

	if (mCachingStarted) {
		child.setDrawingCacheEnabled(true);
	}
}