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.evandroid.musica.fragment.LocalLyricsFragment.java

public void animateUndo(Lyrics[] lyricsArray) {
    final HashMap<Long, Integer> itemIdTopMap = new HashMap<>();
    int firstVisiblePosition = megaListView.getFirstVisiblePosition();
    for (int i = 0; i < megaListView.getChildCount(); ++i) {
        View child = megaListView.getChildAt(i);
        int position = firstVisiblePosition + i;
        long itemId = megaListView.getAdapter().getItemId(position);
        itemIdTopMap.put(itemId, child.getTop());
    }//  www  .j a v  a2s.c  o  m
    final boolean[] firstAnimation = { true };
    // Delete the item from the adapter
    final int groupPosition = ((LocalAdapter) getExpandableListAdapter()).add(lyricsArray[0]);
    megaListView.setAdapter(getExpandableListAdapter());
    megaListView.post(new Runnable() {
        @Override
        public void run() {
            megaListView.expandGroupWithAnimation(groupPosition);
        }
    });
    new WriteToDatabaseTask(LocalLyricsFragment.this).execute(LocalLyricsFragment.this, null, lyricsArray);

    final ViewTreeObserver[] observer = { megaListView.getViewTreeObserver() };
    observer[0].addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        public boolean onPreDraw() {
            observer[0].removeOnPreDrawListener(this);
            firstAnimation[0] = true;
            int firstVisiblePosition = megaListView.getFirstVisiblePosition();
            for (int i = 0; i < megaListView.getChildCount(); ++i) {
                final View child = megaListView.getChildAt(i);
                int position = firstVisiblePosition + i;
                long itemId = getListView().getAdapter().getItemId(position);
                Integer formerTop = itemIdTopMap.get(itemId);
                int newTop = child.getTop();
                if (formerTop != null) {
                    if (formerTop != newTop) {
                        int delta = formerTop - newTop;
                        child.setTranslationY(delta);
                        int MOVE_DURATION = 500;
                        child.animate().setDuration(MOVE_DURATION).translationY(0);
                        if (firstAnimation[0]) {
                            child.animate().setListener(new AnimatorActionListener(new Runnable() {
                                public void run() {
                                    mBackgroundContainer.hideBackground();
                                    mSwiping = false;
                                    getListView().setEnabled(true);
                                }
                            }, AnimatorActionListener.ActionType.END));
                            firstAnimation[0] = false;
                        }
                    }
                } else {
                    // Animate new views along with the others. The catch is that they did not
                    // exist in the start state, so we must calculate their starting position
                    // based on neighboring views.
                    int childHeight = child.getHeight() + megaListView.getDividerHeight();
                    formerTop = newTop - childHeight;
                    int delta = formerTop - newTop;
                    final float z = ((CardView) child).getCardElevation();
                    ((CardView) child).setCardElevation(0f);
                    child.setTranslationY(delta);
                    final int MOVE_DURATION = 500;
                    child.animate().setDuration(MOVE_DURATION).translationY(0);
                    child.animate().setListener(new AnimatorActionListener(new Runnable() {
                        public void run() {
                            mBackgroundContainer.hideBackground();
                            mSwiping = false;
                            getListView().setEnabled(true);
                            ObjectAnimator anim = ObjectAnimator.ofFloat(child, "cardElevation", 0f, z);
                            anim.setDuration(200);
                            anim.setInterpolator(new AccelerateInterpolator());
                            anim.start();
                        }
                    }, AnimatorActionListener.ActionType.END));
                    firstAnimation[0] = false;
                }
            }
            if (firstAnimation[0]) {
                mBackgroundContainer.hideBackground();
                mSwiping = false;
                getListView().setEnabled(true);
                firstAnimation[0] = false;
            }
            itemIdTopMap.clear();
            return true;
        }
    });
}

From source file:android.support.v7.testutils.TestUtils.java

/**
 * This method takes a view and returns a single bitmap that is the layered combination
 * of background drawables of this view and all its ancestors. It can be used to abstract
 * away the specific implementation of a view hierarchy that is not exposed via class APIs
 * or a view hierarchy that depends on the platform version. Instead of hard-coded lookups
 * of particular inner implementations of such a view hierarchy that can break during
 * refactoring or on newer platform versions, calling this API returns a "combined" background
 * of the view./*from w w w.j a  v  a 2  s.c om*/
 *
 * For example, it is useful to get the combined background of a popup / dropdown without
 * delving into the inner implementation details of how that popup is implemented on a
 * particular platform version.
 */
public static Bitmap getCombinedBackgroundBitmap(View view) {
    final int bitmapWidth = view.getWidth();
    final int bitmapHeight = view.getHeight();

    // Create a bitmap
    final Bitmap bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888);
    // Create a canvas that wraps the bitmap
    final Canvas canvas = new Canvas(bitmap);

    // As the draw pass starts at the top of view hierarchy, our first step is to traverse
    // the ancestor hierarchy of our view and collect a list of all ancestors with non-null
    // and visible backgrounds. At each step we're keeping track of the combined offsets
    // so that we can properly combine all of the visuals together in the next pass.
    List<View> ancestorsWithBackgrounds = new ArrayList<>();
    List<Pair<Integer, Integer>> ancestorOffsets = new ArrayList<>();
    int offsetX = 0;
    int offsetY = 0;
    while (true) {
        final Drawable backgroundDrawable = view.getBackground();
        if ((backgroundDrawable != null) && backgroundDrawable.isVisible()) {
            ancestorsWithBackgrounds.add(view);
            ancestorOffsets.add(Pair.create(offsetX, offsetY));
        }
        // Go to the parent
        ViewParent parent = view.getParent();
        if (!(parent instanceof View)) {
            // We're done traversing the ancestor chain
            break;
        }

        // Update the offsets based on the location of current view in its parent's bounds
        offsetX += view.getLeft();
        offsetY += view.getTop();

        view = (View) parent;
    }

    // Now we're going to iterate over the collected ancestors in reverse order (starting from
    // the topmost ancestor) and draw their backgrounds into our combined bitmap. At each step
    // we are respecting the offsets of our original view in the coordinate system of the
    // currently drawn ancestor.
    final int layerCount = ancestorsWithBackgrounds.size();
    for (int i = layerCount - 1; i >= 0; i--) {
        View ancestor = ancestorsWithBackgrounds.get(i);
        Pair<Integer, Integer> offsets = ancestorOffsets.get(i);

        canvas.translate(offsets.first, offsets.second);
        ancestor.getBackground().draw(canvas);
        canvas.translate(-offsets.first, -offsets.second);
    }

    return bitmap;
}

From source file:com.amagi82.kerbalspaceapp.MissionPlanner.java

/**
 * This method animates all other views in the ListView container (not including ignoreView) into their final positions. It is called
 * after ignoreView has been removed from the adapter, but before layout has been run. The approach here is to figure out where
 * everything is now, then allow layout to run, then figure out where everything is after layout, and then to run animations between all
 * of those start/end positions./*from w ww  . j a  va2s  . co  m*/
 */
private void animateRemoval(final ListView listview, View viewToRemove) {
    int firstVisiblePosition = listview.getFirstVisiblePosition();
    for (int i = 0; i < listview.getChildCount(); ++i) {
        View child = listview.getChildAt(i);
        if (child != viewToRemove) {
            int position = firstVisiblePosition + i;
            long itemId = mAdapter.getItemId(position);
            mItemIdTopMap.put(itemId, child.getTop());
        }
    }
    // Delete the item from the adapter
    int position = mListView.getPositionForView(viewToRemove);
    mAdapter.remove(mAdapter.getItem(position));
    mAdapter.notifyDataSetChanged();
    refreshDeltaV();

    final ViewTreeObserver observer = listview.getViewTreeObserver();
    observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            observer.removeOnPreDrawListener(this);
            boolean firstAnimation = true;
            int firstVisiblePosition = listview.getFirstVisiblePosition();
            for (int i = 0; i < listview.getChildCount(); ++i) {
                final View child = listview.getChildAt(i);
                int position = firstVisiblePosition + i;
                long itemId = mAdapter.getItemId(position);
                Integer startTop = mItemIdTopMap.get(itemId);
                int top = child.getTop();
                if (startTop != null) {
                    if (startTop != top) {
                        int delta = startTop - top;
                        child.setTranslationY(delta);
                        child.animate().setDuration(MOVE_DURATION).translationY(0);
                        if (firstAnimation) {
                            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                                child.animate().setListener(new AnimatorListenerAdapter() {
                                    @Override
                                    public void onAnimationEnd(Animator animation) {
                                        mBackgroundContainer.hideBackground();
                                        mSwiping = false;
                                        mListView.setEnabled(true);

                                    }
                                });
                            } else {
                                child.animate().withEndAction(new Runnable() {
                                    @Override
                                    public void run() {
                                        mBackgroundContainer.hideBackground();
                                        mSwiping = false;
                                        mListView.setEnabled(true);
                                    }
                                });
                                firstAnimation = false;
                            }
                        }
                    }
                } else {
                    // Animate new views along with the others. The catch is that they did not
                    // exist in the start state, so we must calculate their starting position
                    // based on neighboring views.
                    int childHeight = child.getHeight() + listview.getDividerHeight();
                    startTop = top + (i > 0 ? childHeight : -childHeight);
                    int delta = startTop - top;
                    child.setTranslationY(delta);
                    child.animate().setDuration(MOVE_DURATION).translationY(0);
                    if (firstAnimation) {
                        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                            child.animate().setListener(new AnimatorListenerAdapter() {
                                @Override
                                public void onAnimationEnd(Animator animation) {
                                    mBackgroundContainer.hideBackground();
                                    mSwiping = false;
                                    mListView.setEnabled(true);

                                }
                            });
                        } else {
                            child.animate().withEndAction(new Runnable() {
                                @Override
                                public void run() {
                                    mBackgroundContainer.hideBackground();
                                    mSwiping = false;
                                    mListView.setEnabled(true);
                                }
                            });
                            firstAnimation = false;
                        }
                    }
                }
            }
            mItemIdTopMap.clear();
            return true;
        }
    });

}

From source file:com.dgnt.dominionCardPicker.view.DynamicListView.java

/**
 * This method determines whether the hover cell has been shifted far enough
 * to invoke a cell swap. If so, then the respective cell swap candidate is
 * determined and the data set is changed. Upon posting a notification of the
 * data set change, a layout is invoked to place the cells in the right place.
 * Using a ViewTreeObserver and a corresponding OnPreDrawListener, we can
 * offset the cell being swapped to where it previously was and then animate it to
 * its new position./*  w  w  w .java 2 s  .  c o m*/
 */
private void handleCellSwitch() {
    final int deltaY = mLastEventY - mDownY;
    int deltaYTotal = mHoverCellOriginalBounds.top + mTotalOffset + deltaY;

    View belowView = getViewForID(mBelowItemId);
    View mobileView = getViewForID(mMobileItemId);
    View aboveView = getViewForID(mAboveItemId);

    boolean isBelow = (belowView != null) && (deltaYTotal > belowView.getTop());
    boolean isAbove = (aboveView != null) && (deltaYTotal < aboveView.getTop());

    if (isBelow || isAbove) {

        final long switchItemID = isBelow ? mBelowItemId : mAboveItemId;
        View switchView = isBelow ? belowView : aboveView;
        final int originalItem = getPositionForView(mobileView);

        if (switchView == null) {
            updateNeighborViewsForID(mMobileItemId);
            return;
        }

        swapElements(list, originalItem, getPositionForView(switchView));

        ((BaseAdapter) getAdapter()).notifyDataSetChanged();

        mDownY = mLastEventY;

        final int switchViewStartTop = switchView.getTop();

        mobileView.setVisibility(View.VISIBLE);
        switchView.setVisibility(View.VISIBLE);

        updateNeighborViewsForID(mMobileItemId);

        final ViewTreeObserver observer = getViewTreeObserver();
        observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            public boolean onPreDraw() {
                observer.removeOnPreDrawListener(this);

                View switchView = getViewForID(switchItemID);

                mTotalOffset += deltaY;

                int switchViewNewTop = switchView.getTop();
                int delta = switchViewStartTop - switchViewNewTop;

                switchView.setTranslationY(delta);

                ObjectAnimator animator = ObjectAnimator.ofFloat(switchView, View.TRANSLATION_Y, 0);
                animator.setDuration(MOVE_DURATION);
                animator.start();

                return true;
            }
        });
    }
}

From source file:ac.robinson.mediaphone.activity.NarrativeBrowserActivity.java

@Override
protected void onPause() {
    super.onPause();
    hidePopup();//from   ww  w . ja  va  2  s .  c o  m
    mScrollHandler.removeMessages(R.id.msg_update_narrative_icons);

    // need to do this manually because the default implementation resets when we change the adapters
    int listTop = mNarratives.getFirstVisiblePosition();
    int listPosition = 0;
    View firstView = mNarratives.getChildAt(0);
    if (firstView != null) {
        listPosition = firstView.getTop();
    }
    updateListPositions(listTop, listPosition);
}

From source file:com.example.customview.DynamicListView.java

/**
 * This method determines whether the hover cell has been shifted far enough
 * to invoke a cell swap. If so, then the respective cell swap candidate is
 * determined and the data set is changed. Upon posting a notification of the
 * data set change, a layout is invoked to place the cells in the right place.
 * Using a ViewTreeObserver and a corresponding OnPreDrawListener, we can
 * offset the cell being swapped to where it previously was and then animate it to
 * its new position.// w  w w  .j a  v  a 2  s .c om
 */
private void handleCellSwitch() {
    final int deltaY = mLastEventY - mDownY;
    int deltaYTotal = mHoverCellOriginalBounds.top + mTotalOffset + deltaY;

    View belowView = getViewForID(mBelowItemId);
    View mobileView = getViewForID(mMobileItemId);
    View aboveView = getViewForID(mAboveItemId);

    boolean isBelow = (belowView != null) && (deltaYTotal > belowView.getTop());
    boolean isAbove = (aboveView != null) && (deltaYTotal < aboveView.getTop());

    if (isBelow || isAbove) {

        final long switchItemID = isBelow ? mBelowItemId : mAboveItemId;
        View switchView = isBelow ? belowView : aboveView;
        final int originalItem = getPositionForView(mobileView);

        if (switchView == null) {
            updateNeighborViewsForID(mMobileItemId);
            return;
        }

        if (getPositionForView(switchView) == mPanelList.size() - 1) {
            return;
        }
        swapElements(mPanelList, originalItem, getPositionForView(switchView));

        ((BaseAdapter) getAdapter()).notifyDataSetChanged();
        mDownY = mLastEventY;

        final int switchViewStartTop = switchView.getTop();

        mobileView.setVisibility(View.VISIBLE);
        switchView.setVisibility(View.INVISIBLE);

        updateNeighborViewsForID(mMobileItemId);

        final ViewTreeObserver observer = getViewTreeObserver();
        observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            public boolean onPreDraw() {
                observer.removeOnPreDrawListener(this);

                View switchView = getViewForID(switchItemID);

                mTotalOffset += deltaY;

                int switchViewNewTop = switchView.getTop();
                int delta = switchViewStartTop - switchViewNewTop;

                switchView.setTranslationY(delta);

                ObjectAnimator animator = ObjectAnimator.ofFloat(switchView, View.TRANSLATION_Y, 0);
                animator.setDuration(MOVE_DURATION);
                animator.start();

                return true;
            }
        });
    }
}

From source file:android.support.design.widget.BottomSheetBehavior.java

boolean shouldHide(View child, float yvel) {
    if (mSkipCollapsed) {
        return true;
    }/* w  w w. j ava  2  s .c om*/
    if (child.getTop() < mMaxOffset) {
        // It should not hide, but collapse.
        return false;
    }
    final float newTop = child.getTop() + yvel * HIDE_FRICTION;
    return Math.abs(newTop - mMaxOffset) / (float) mPeekHeight > HIDE_THRESHOLD;
}

From source file:com.chauthai.overscroll.BouncyAdapter.java

/**
 * Get the visible size of a view to the bottom of the RecyclerView.
 * @param view to be checked//from   w w  w  . j a va  2s  . c o m
 * @return visible size in pixels, 0 if not visible.
 */
private int getBottomVisible(View view) {
    return Math.max(0, mRecyclerView.getHeight() - view.getTop() - mRecyclerView.getPaddingBottom());
}

From source file:com.devbrackets.android.recyclerext.decoration.ReorderDecoration.java

/**
 * Updates the stored position for the start of the view.  This will be the
 * top when Vertical and left when Horizontal.
 *
 * @param childPosition The position of the view in the RecyclerView
 * @param draggedUp True if the view has been moved up or to the left
 *//*from w w  w.jav a 2  s. c om*/
private void updateNewViewStart(int childPosition, boolean draggedUp) {
    View view = recyclerView.getLayoutManager().getChildAt(childPosition);
    if (view == null) {
        return;
    }

    int start = orientation == LayoutOrientation.VERTICAL ? view.getTop() : view.getLeft();
    int viewDimen = orientation == LayoutOrientation.VERTICAL ? view.getHeight() : view.getWidth();
    viewDimen *= draggedUp ? -1 : 1;

    newViewStart = start + (view.getVisibility() == View.VISIBLE ? viewDimen : 0);
}

From source file:com.github.wakhub.monodict.activity.FlashcardActivity.java

@UiThread
void onLoadContents(Cursor cursor) {
    Log.d(TAG, "onLoadContents: " + cursor.getCount());

    // how to maintain scroll position of listview when it updates
    // http://stackoverflow.com/questions/10196079
    ListView listView = getListView();
    int lastPosition = listView.getFirstVisiblePosition();
    int lastTopOffset = 0;
    if (listView.getCount() > 0) {
        View view = listView.getChildAt(0);
        if (view != null) {
            lastTopOffset = view.getTop();
        }//ww w .j  a v a 2  s  .  c o  m
    }

    listAdapter = new ListAdapter(this, cursor);
    setListAdapter(listAdapter);

    if (lastPosition > listView.getCount()) {
        listView.setSelection(listView.getCount() - 1);
    } else {
        listView.setSelectionFromTop(lastPosition, lastTopOffset);
    }

    reloadTabs();
    activityHelper.hideProgressDialog();
}