Example usage for android.view View getTop

List of usage examples for android.view View getTop

Introduction

In this page you can find the example usage for android.view View getTop.

Prototype

@ViewDebug.CapturedViewProperty
public final int getTop() 

Source Link

Document

Top position of this view relative to its parent.

Usage

From source file:com.aidy.bottomdrawerlayout.DrawerLayout.java

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    Log.i(TAG, "drawChild()");
    final int height = getHeight();
    final boolean drawingContent = isContentView(child);
    int clipLeft = 0, clipRight = getWidth();

    final int restoreCount = canvas.save();
    if (drawingContent) {
        final int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View v = getChildAt(i);
            if (v == child || v.getVisibility() != VISIBLE || !hasOpaqueBackground(v) || !isDrawerView(v)
                    || v.getHeight() < height) {
                Log.i(TAG, "drawChild() -- 0");
                continue;
            }/*w ww  .ja va2s . c o  m*/

            if (checkDrawerViewAbsoluteGravity(v, Gravity.LEFT)) {
                Log.i(TAG, "drawChild() -- 1");
                final int vright = v.getRight();
                if (vright > clipLeft)
                    clipLeft = vright;
            } else {
                Log.i(TAG, "drawChild() -- 2");
                final int vleft = v.getLeft();
                if (vleft < clipRight)
                    clipRight = vleft;
            }
        }
        canvas.clipRect(clipLeft, 0, clipRight, getHeight());
    }
    final boolean result = super.drawChild(canvas, child, drawingTime);
    canvas.restoreToCount(restoreCount);

    if (mScrimOpacity > 0 && drawingContent) {
        Log.i(TAG, "drawChild() -- drawingContent");
        final int baseAlpha = (mScrimColor & 0xff000000) >>> 24;
        final int imag = (int) (baseAlpha * mScrimOpacity);
        final int color = imag << 24 | (mScrimColor & 0xffffff);
        mScrimPaint.setColor(color);

        canvas.drawRect(clipLeft, 0, clipRight, getHeight(), mScrimPaint);
    } else if (mShadowLeft != null && checkDrawerViewAbsoluteGravity(child, Gravity.LEFT)) {
        Log.i(TAG, "drawChild() -- Gravity.LEFT");
        final int shadowWidth = mShadowLeft.getIntrinsicWidth();
        final int childRight = child.getRight();
        final int drawerPeekDistance = mLeftDragger.getEdgeSize();
        final float alpha = Math.max(0, Math.min((float) childRight / drawerPeekDistance, 1.f));
        mShadowLeft.setBounds(childRight, child.getTop(), childRight + shadowWidth, child.getBottom());
        mShadowLeft.setAlpha((int) (0xff * alpha));
        mShadowLeft.draw(canvas);
    } else if (mShadowRight != null && checkDrawerViewAbsoluteGravity(child, Gravity.RIGHT)) {
        Log.i(TAG, "drawChild() -- Gravity.RIGHT");
        final int shadowWidth = mShadowRight.getIntrinsicWidth();
        final int childLeft = child.getLeft();
        final int showing = getWidth() - childLeft;
        final int drawerPeekDistance = mRightDragger.getEdgeSize();
        final float alpha = Math.max(0, Math.min((float) showing / drawerPeekDistance, 1.f));
        mShadowRight.setBounds(childLeft - shadowWidth, child.getTop(), childLeft, child.getBottom());
        mShadowRight.setAlpha((int) (0xff * alpha));
        mShadowRight.draw(canvas);
    }
    return result;
}

From source file:com.appunite.list.ListView.java

@Override
protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
    super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);

    final ListAdapter adapter = mAdapter;
    int closetChildIndex = -1;
    int closestChildTop = 0;
    if (adapter != null && gainFocus && previouslyFocusedRect != null) {
        final int scrollX = getScrollX();
        final int scrollY = getScrollY();
        previouslyFocusedRect.offset(scrollX, scrollY);

        // Don't cache the result of getChildCount or mFirstPosition here,
        // it could change in layoutChildren.
        if (adapter.getCount() < getChildCount() + mFirstPosition) {
            mLayoutMode = LAYOUT_NORMAL;
            layoutChildren();/*from   w  w  w.  ja v a 2s.  c  o m*/
        }

        // figure out which item should be selected based on previously
        // focused rect
        Rect otherRect = mTempRect;
        int minDistance = Integer.MAX_VALUE;
        final int childCount = getChildCount();
        final int firstPosition = mFirstPosition;

        for (int i = 0; i < childCount; i++) {
            // only consider selectable views
            if (!adapter.isEnabled(firstPosition + i)) {
                continue;
            }

            View other = getChildAt(i);
            other.getDrawingRect(otherRect);
            offsetDescendantRectToMyCoords(other, otherRect);
            int distance = getDistance(previouslyFocusedRect, otherRect, direction);

            if (distance < minDistance) {
                minDistance = distance;
                closetChildIndex = i;
                closestChildTop = other.getTop();
            }
        }
    }

    if (closetChildIndex >= 0) {
        setSelectionFromTop(closetChildIndex + mFirstPosition, closestChildTop);
    } else {
        requestLayout();
    }
}

From source file:com.brantapps.viewpagerindicator.vertical.VerticalViewPager.java

/**
 * This method will be invoked when the current page is scrolled, either as part
 * of a programmatically initiated smooth scroll or a user initiated touch scroll.
 * If you override this method you must call through to the superclass implementation
 * (e.g. super.onPageScrolled(position, offset, offsetPixels)) before onPageScrolled
 * returns.//from   ww  w .  j a va 2  s.  co  m
 *
 * @param position Position index of the first page currently being displayed.
 *                 Page position+1 will be visible if positionOffset is nonzero.
 * @param offset Value from [0, 1) indicating the offset from the page at position.
 * @param offsetPixels Value in pixels indicating the offset from position.
 */
protected void onPageScrolled(int position, float offset, int offsetPixels) {
    // Offset any decor views if needed - keep them on-screen at all times.
    if (mDecorChildCount > 0) {
        // BrantApps Change: Renamed scrollx to scrolly, paddingLeft to paddingTop, paddingRight to paddingBottom & width to height
        // BrantApps Change: Also changed method calls to get the four values
        final int scrollY = getScrollY();
        int paddingTop = getPaddingTop();
        int paddingBottom = getPaddingBottom();
        final int height = getHeight();
        final int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = getChildAt(i);
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            if (!lp.isDecor)
                continue;

            final int hgrav = lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
            // BrantApps Change: Renamed childLeft to childTop
            int childTop = 0;
            // BrantApps Change: In switch statement changed 3 calls to getMeasuredWidth() to getMeasuredHeight()
            switch (hgrav) {
            default:
                childTop = paddingTop;
                break;
            case Gravity.LEFT:
                childTop = paddingTop;
                // BrantApps Change: Was paddingLeft += child.getWidth();
                paddingTop += child.getHeight();
                break;
            case Gravity.CENTER_HORIZONTAL:
                childTop = Math.max((height - child.getMeasuredHeight()) / 2, paddingTop);
                break;
            case Gravity.RIGHT:
                childTop = height - paddingBottom - child.getMeasuredHeight();
                paddingBottom += child.getMeasuredHeight();
                break;
            }
            childTop += scrollY;

            // BrantApps Change: Was final int childOffset = childLeft - child.getLeft();
            final int childOffset = childTop - child.getTop();
            if (childOffset != 0) {
                child.offsetTopAndBottom(childOffset);
            }
        }
    }

    if (mOnPageChangeListener != null) {
        mOnPageChangeListener.onPageScrolled(position, offset, offsetPixels);
    }
    if (mInternalPageChangeListener != null) {
        mInternalPageChangeListener.onPageScrolled(position, offset, offsetPixels);
    }
    mCalledSuper = true;
}

From source file:app.umitems.greenclock.widget.sgv.StaggeredGridView.java

/**
 * NOTE: borrowed from {@link GridView}//from   w  w  w.j av a2s . c  om
 * Comments from {@link View}
 *
 * Compute the vertical extent of the vertical scrollbar's thumb within the vertical range.
 * This value is used to compute the length of the thumb within the scrollbar's track.
 * The range is expressed in arbitrary units that must be the same as the units used by
 * {@link #computeVerticalScrollRange} and {@link #computeVerticalScrollOffset}.
 *
 * The default extent is the drawing height of this view.
 *
 * @return the vertical extent of the scrollbar's thumb
 */
@Override
protected int computeVerticalScrollExtent() {

    final int count = getChildCount();
    if (count > 0) {
        if (mSmoothScrollbarEnabled) {
            final int rowCount = (count + mColCount - 1) / mColCount;
            int extent = rowCount * SCROLLING_ESTIMATED_ITEM_HEIGHT;

            View view = getChildAt(0);
            final int top = view.getTop();
            int height = view.getHeight();
            if (height > 0) {
                extent += (top * SCROLLING_ESTIMATED_ITEM_HEIGHT) / height;
            }

            view = getChildAt(count - 1);
            final int bottom = view.getBottom();
            height = view.getHeight();
            if (height > 0) {
                extent -= ((bottom - getHeight()) * SCROLLING_ESTIMATED_ITEM_HEIGHT) / height;
            }

            return extent;
        } else {
            return 1;
        }
    }
    return 0;
}

From source file:com.appunite.list.ListView.java

/**
 * Determine how much we need to scroll in order to get the next selected view
 * visible, with a fading edge showing below as applicable.  The amount is
 * capped at {@link #getMaxScrollAmount()} .
 *
 * @param direction either {@link android.view.View#FOCUS_UP} or
 *        {@link android.view.View#FOCUS_DOWN}.
 * @param nextSelectedPosition The position of the next selection, or
 *        {@link #INVALID_POSITION} if there is no next selectable position
 * @return The amount to scroll. Note: this is always positive!  Direction
 *         needs to be taken into account when actually scrolling.
 *///  www  . j  ava2s  . c  om
private int amountToScroll(int direction, int nextSelectedPosition) {
    final int listBottom = getHeight() - mListPadding.bottom;
    final int listTop = mListPadding.top;

    int numChildren = getChildCount();

    if (direction == View.FOCUS_DOWN) {
        int indexToMakeVisible = numChildren - 1;
        if (nextSelectedPosition != INVALID_POSITION) {
            indexToMakeVisible = nextSelectedPosition - mFirstPosition;
        }
        while (numChildren <= indexToMakeVisible) {
            // Child to view is not attached yet.
            addViewBelow(getChildAt(numChildren - 1), mFirstPosition + numChildren - 1);
            numChildren++;
        }
        final int positionToMakeVisible = mFirstPosition + indexToMakeVisible;
        final View viewToMakeVisible = getChildAt(indexToMakeVisible);

        int goalBottom = listBottom;
        if (positionToMakeVisible < mItemCount - 1) {
            goalBottom -= getArrowScrollPreviewLength();
        }

        if (viewToMakeVisible.getBottom() <= goalBottom) {
            // item is fully visible.
            return 0;
        }

        if (nextSelectedPosition != INVALID_POSITION
                && (goalBottom - viewToMakeVisible.getTop()) >= getMaxScrollAmount()) {
            // item already has enough of it visible, changing selection is good enough
            return 0;
        }

        int amountToScroll = (viewToMakeVisible.getBottom() - goalBottom);

        if ((mFirstPosition + numChildren) == mItemCount) {
            // last is last in list -> make sure we don't scroll past it
            final int max = getChildAt(numChildren - 1).getBottom() - listBottom;
            amountToScroll = Math.min(amountToScroll, max);
        }

        return Math.min(amountToScroll, getMaxScrollAmount());
    } else {
        int indexToMakeVisible = 0;
        if (nextSelectedPosition != INVALID_POSITION) {
            indexToMakeVisible = nextSelectedPosition - mFirstPosition;
        }
        while (indexToMakeVisible < 0) {
            // Child to view is not attached yet.
            addViewAbove(getChildAt(0), mFirstPosition);
            mFirstPosition--;
            indexToMakeVisible = nextSelectedPosition - mFirstPosition;
        }
        final int positionToMakeVisible = mFirstPosition + indexToMakeVisible;
        final View viewToMakeVisible = getChildAt(indexToMakeVisible);
        int goalTop = listTop;
        if (positionToMakeVisible > 0) {
            goalTop += getArrowScrollPreviewLength();
        }
        if (viewToMakeVisible.getTop() >= goalTop) {
            // item is fully visible.
            return 0;
        }

        if (nextSelectedPosition != INVALID_POSITION
                && (viewToMakeVisible.getBottom() - goalTop) >= getMaxScrollAmount()) {
            // item already has enough of it visible, changing selection is good enough
            return 0;
        }

        int amountToScroll = (goalTop - viewToMakeVisible.getTop());
        if (mFirstPosition == 0) {
            // first is first in list -> make sure we don't scroll past it
            final int max = listTop - getChildAt(0).getTop();
            amountToScroll = Math.min(amountToScroll, max);
        }
        return Math.min(amountToScroll, getMaxScrollAmount());
    }
}

From source file:com.android.launcher2.Workspace.java

void mapPointFromSelfToChild(View v, float[] xy, Matrix cachedInverseMatrix) {
    if (cachedInverseMatrix == null) {
        v.getMatrix().invert(mTempInverseMatrix);
        cachedInverseMatrix = mTempInverseMatrix;
    }/*  w  w w .  j  ava2 s .  co m*/
    int scrollX = getScrollX();
    if (mNextPage != INVALID_PAGE) {
        scrollX = mScroller.getFinalX();
    }
    xy[0] = xy[0] + scrollX - v.getLeft();
    xy[1] = xy[1] + getScrollY() - v.getTop();
    cachedInverseMatrix.mapPoints(xy);
}

From source file:app.umitems.greenclock.widget.sgv.StaggeredGridView.java

/**
 * NOTE: borrowed from {@link GridView} and altered as appropriate to accommodate for
 * {@link StaggeredGridView}//  www  .  j a va  2s  . c om
 *
 * Comments from {@link View}
 *
 * Compute the vertical offset of the vertical scrollbar's thumb within the horizontal range.
 * This value is used to compute the position of the thumb within the scrollbar's track.
 * The range is expressed in arbitrary units that must be the same as the units used by
 * {@link #computeVerticalScrollRange()} and {@link #computeVerticalScrollExtent()}.
 *
 * The default offset is the scroll offset of this view.
 *
 * @return the vertical offset of the scrollbar's thumb
 */
@Override
protected int computeVerticalScrollOffset() {
    final int firstPosition = mFirstPosition;
    final int childCount = getChildCount();
    final int paddingTop = getPaddingTop();

    if (firstPosition >= 0 && childCount > 0) {
        if (mSmoothScrollbarEnabled) {
            final View view = getChildAt(0);
            final int top = view.getTop();
            final int currentTopViewHeight = view.getHeight();
            if (currentTopViewHeight > 0) {
                // In an ideal world, all items would have a fixed height that we would know
                // a priori, calculating the scroll offset would simply be:
                //     [A] (mFirstPosition * fixedHeight) - childView[0].top
                //         where childView[0] is the first view on screen.
                //
                // However, given that we do not know the height ahead of time, and that each
                // item in this grid can have varying heights, we'd need to assign an arbitrary
                // item height (SCROLLING_ESTIMATED_ITEM_HEIGHT) in order to estimate the scroll
                // offset.  The previous equation thus transforms to:
                //     [B] (mFirstPosition * SCROLLING_ESTIMATED_ITEM_HEIGHT) -
                //         ((childView[0].top * SCROLLING_ESTIMATED_ITEM_HEIGHT) /
                //          childView[0].height)
                //
                // Equation [B] gives a pretty good calculation of the offset if this were a
                // single column grid view, for a multi-column grid, one slight modification is
                // needed:
                //     [C] ((mFirstPosition * SCROLLING_ESTIMATED_ITEM_HEIGHT) / mColCount) -
                //         ((childView[0].top * SCROLLING_ESTIMATED_ITEM_HEIGHT) /
                //          childView[0].height)
                final int estimatedScrollOffset = ((firstPosition * SCROLLING_ESTIMATED_ITEM_HEIGHT)
                        / mColCount) - ((top * SCROLLING_ESTIMATED_ITEM_HEIGHT) / currentTopViewHeight);

                final int rowCount = (mItemCount + mColCount - 1) / mColCount;
                final int overScrollCompensation = (int) ((float) getScrollY() / getHeight() * rowCount
                        * SCROLLING_ESTIMATED_ITEM_HEIGHT);

                int val = Math.max(estimatedScrollOffset + overScrollCompensation, 0);
                // If mFirstPosition is currently the very first item in the adapter, check to
                // see if we need to take into account any top padding.  This is so that we
                // don't return 0 when in fact the user may still be scrolling through some
                // top padding.
                if (firstPosition == 0 && paddingTop > 0) {
                    val += paddingTop - top + mItemMargin;
                }
                return val;
            }
        } else {
            int index;
            final int count = mItemCount;
            if (firstPosition == 0) {
                index = 0;
            } else if (firstPosition + childCount == count) {
                index = count;
            } else {
                index = firstPosition + childCount / 2;
            }
            return (int) (firstPosition + childCount * (index / (float) count));
        }
    }

    return paddingTop;
}

From source file:app.umitems.greenclock.widget.sgv.StaggeredGridView.java

/**
 * Important: this method will leave offscreen views attached if they
 * are required to maintain the invariant that child view with index i
 * is always the view corresponding to position mFirstPosition + i.
 *//*from www .  j  a  va2s .co m*/
private void recycleOffscreenViews() {
    if (getChildCount() == 0) {
        return;
    }

    final int height = getHeight();
    final int clearAbove = -mItemMargin;
    final int clearBelow = height + mItemMargin;
    for (int i = getChildCount() - 1; i >= 0; i--) {
        final View child = getChildAt(i);
        if (child.getTop() <= clearBelow) {
            // There may be other offscreen views, but we need to maintain
            // the invariant documented above.
            break;
        }

        child.clearFocus();
        if (mInLayout) {
            removeViewsInLayout(i, 1);
        } else {
            removeViewAt(i);
        }

        mRecycler.addScrap(child);
    }

    while (getChildCount() > 0) {
        final View child = getChildAt(0);
        if (child.getBottom() >= clearAbove) {
            // There may be other offscreen views, but we need to maintain
            // the invariant documented above.
            break;
        }

        child.clearFocus();
        if (mInLayout) {
            removeViewsInLayout(0, 1);
        } else {
            removeViewAt(0);
        }

        mRecycler.addScrap(child);
        mFirstPosition++;
    }

    final int childCount = getChildCount();
    if (childCount > 0) {
        // Repair the top and bottom column boundaries from the views we still have
        Arrays.fill(mItemTops, Integer.MAX_VALUE);
        Arrays.fill(mItemBottoms, Integer.MIN_VALUE);
        for (int i = 0; i < childCount; i++) {
            final View child = getChildAt(i);
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            final int top = child.getTop() - mItemMargin;
            final int bottom = child.getBottom();
            LayoutRecord rec = mLayoutRecords.get(mFirstPosition + i);

            // It's possible the layout record could be null for visible views because
            // they are cleared between adapter data set changes, but the views are left
            // attached for the purpose of animations. Hence, populate the layout record again.
            if (rec == null) {
                rec = recreateLayoutRecord(mFirstPosition + i, child, lp);
            }

            // In LTR layout, iterate across each column that this child is laid out in,
            // starting from the child's first column (lp.column).  For each column, update
            // mItemTops and mItemBottoms appropriately to take into account this child's
            // dimension.  In RTL layout, iterate in reverse, where the child's starting
            // column would start from the right-most.
            final int span = Math.min(mColCount, lp.span);
            for (int spanIndex = 0; spanIndex < span; spanIndex++) {
                final int col = mIsRtlLayout ? lp.column - spanIndex : lp.column + spanIndex;
                final int colTop = top - rec.getMarginAbove(spanIndex);
                final int colBottom = bottom + rec.getMarginBelow(spanIndex);
                if (colTop < mItemTops[col]) {
                    mItemTops[col] = colTop;
                }
                if (colBottom > mItemBottoms[col]) {
                    mItemBottoms[col] = colBottom;
                }
            }
        }

        for (int col = 0; col < mColCount; col++) {
            if (mItemTops[col] == Integer.MAX_VALUE) {
                // If one was untouched, both were.
                final int top = getPaddingTop();
                mItemTops[col] = top;
                mItemBottoms[col] = top;
            }
        }
    }

    mCurrentScrollState = getScrollState();
}

From source file:app.umitems.greenclock.widget.sgv.StaggeredGridView.java

/**
 * NOTE This method is borrowed from {@link ScrollView}.
 *///from  w  w  w.  jav a2 s .c  o  m
@Override
public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
    // offset into coordinate space of this scroll view
    rectangle.offset(child.getLeft() - child.getScrollX(), child.getTop() - child.getScrollY());

    return scrollToChildRect(rectangle, immediate);
}

From source file:com.nttec.everychan.ui.presentation.BoardFragment.java

private void scroll(boolean up) {
    if (listLoaded) {
        int step = (int) (50 * resources.getDisplayMetrics().density + 0.5f);
        View v = listView.getChildAt(0);
        int position = listView.getPositionForView(v);
        int top = v.getTop();
        listView.setSelectionFromTop(position, top + step * (up ? 1 : -1));
    }//from  w w  w. j  av  a  2s. c o  m
}