Search in sources :

Example 11 with LinearSmoothScroller

use of android.support.v7.widget.LinearSmoothScroller in project RecyclerViewPager by lsjwzh.

the class RecyclerViewPager method smoothScrollToPosition.

@Override
public void smoothScrollToPosition(int position) {
    if (DEBUG) {
        Log.d("@", "smoothScrollToPosition:" + position);
    }
    if (mPositionBeforeScroll < 0) {
        mPositionBeforeScroll = getCurrentPosition();
    }
    mSmoothScrollTargetPosition = position;
    if (getLayoutManager() != null && getLayoutManager() instanceof LinearLayoutManager) {
        // exclude item decoration
        LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(getContext()) {

            @Override
            public PointF computeScrollVectorForPosition(int targetPosition) {
                if (getLayoutManager() == null) {
                    return null;
                }
                return ((LinearLayoutManager) getLayoutManager()).computeScrollVectorForPosition(targetPosition);
            }

            @Override
            protected void onTargetFound(View targetView, RecyclerView.State state, Action action) {
                if (getLayoutManager() == null) {
                    return;
                }
                int dx = calculateDxToMakeVisible(targetView, getHorizontalSnapPreference());
                int dy = calculateDyToMakeVisible(targetView, getVerticalSnapPreference());
                if (dx > 0) {
                    dx = dx - getLayoutManager().getLeftDecorationWidth(targetView);
                } else {
                    dx = dx + getLayoutManager().getRightDecorationWidth(targetView);
                }
                if (dy > 0) {
                    dy = dy - getLayoutManager().getTopDecorationHeight(targetView);
                } else {
                    dy = dy + getLayoutManager().getBottomDecorationHeight(targetView);
                }
                final int distance = (int) Math.sqrt(dx * dx + dy * dy);
                final int time = calculateTimeForDeceleration(distance);
                if (time > 0) {
                    action.update(-dx, -dy, time, mDecelerateInterpolator);
                }
            }

            @Override
            protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
                return mMillisecondsPerInch / displayMetrics.densityDpi;
            }
        };
        linearSmoothScroller.setTargetPosition(position);
        if (position == RecyclerView.NO_POSITION) {
            return;
        }
        getLayoutManager().startSmoothScroll(linearSmoothScroller);
    } else {
        super.smoothScrollToPosition(position);
    }
}
Also used : LinearSmoothScroller(android.support.v7.widget.LinearSmoothScroller) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View) DisplayMetrics(android.util.DisplayMetrics)

Example 12 with LinearSmoothScroller

use of android.support.v7.widget.LinearSmoothScroller in project SuperSLiM by TonicArtos.

the class LayoutManager method smoothScrollToPosition.

@Override
public void smoothScrollToPosition(final RecyclerView recyclerView, RecyclerView.State state, final int position) {
    if (position < 0 || getItemCount() <= position) {
        Log.e("SuperSLiM.LayoutManager", "Ignored smooth scroll to " + position + " as it is not within the item range 0 - " + getItemCount());
        return;
    }
    // Temporarily disable sticky headers.
    requestLayout();
    recyclerView.getHandler().post(new Runnable() {

        @Override
        public void run() {
            LinearSmoothScroller smoothScroller = new LinearSmoothScroller(recyclerView.getContext()) {

                @Override
                protected void onChildAttachedToWindow(View child) {
                    super.onChildAttachedToWindow(child);
                }

                @Override
                protected void onStop() {
                    super.onStop();
                    // Turn sticky headers back on.
                    requestLayout();
                }

                @Override
                protected int getVerticalSnapPreference() {
                    return LinearSmoothScroller.SNAP_TO_START;
                }

                @Override
                public int calculateDyToMakeVisible(View view, int snapPreference) {
                    final RecyclerView.LayoutManager layoutManager = getLayoutManager();
                    if (!layoutManager.canScrollVertically()) {
                        return 0;
                    }
                    final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams();
                    final int top = layoutManager.getDecoratedTop(view) - params.topMargin;
                    final int bottom = layoutManager.getDecoratedBottom(view) + params.bottomMargin;
                    final int start = getPosition(view) == 0 ? layoutManager.getPaddingTop() : 0;
                    final int end = layoutManager.getHeight() - layoutManager.getPaddingBottom();
                    int dy = calculateDtToFit(top, bottom, start, end, snapPreference);
                    return dy == 0 ? 1 : dy;
                }

                @Override
                public PointF computeScrollVectorForPosition(int targetPosition) {
                    if (getChildCount() == 0) {
                        return null;
                    }
                    return new PointF(0, getDirectionToPosition(targetPosition));
                }
            };
            smoothScroller.setTargetPosition(position);
            startSmoothScroll(smoothScroller);
        }
    });
}
Also used : PointF(android.graphics.PointF) LinearSmoothScroller(android.support.v7.widget.LinearSmoothScroller) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView)

Example 13 with LinearSmoothScroller

use of android.support.v7.widget.LinearSmoothScroller in project UltimateAndroid by cymcsg.

the class FixedGridLayoutManager method smoothScrollToPosition.

/*
     * You must override this method if you would like to support external calls
     * to animate a change to a new adapter position. The framework provides a
     * helper scroller implementation (LinearSmoothScroller), which we leverage
     * to do the animation calculations.
     */
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, final int position) {
    if (position >= getItemCount()) {
        Log.e(TAG, "Cannot scroll to " + position + ", item count is " + getItemCount());
        return;
    }
    /*
         * LinearSmoothScroller's default behavior is to scroll the contents until
         * the child is fully visible. It will snap to the top-left or bottom-right
         * of the parent depending on whether the direction of travel was positive
         * or negative.
         */
    LinearSmoothScroller scroller = new LinearSmoothScroller(recyclerView.getContext()) {

        /*
             * LinearSmoothScroller, at a minimum, just need to know the vector
             * (x/y distance) to travel in order to get from the current positioning
             * to the target.
             */
        @Override
        public PointF computeScrollVectorForPosition(int targetPosition) {
            final int rowOffset = getGlobalRowOfPosition(targetPosition) - getGlobalRowOfPosition(mFirstVisiblePosition);
            final int columnOffset = getGlobalColumnOfPosition(targetPosition) - getGlobalColumnOfPosition(mFirstVisiblePosition);
            return new PointF(columnOffset * mDecoratedChildWidth, rowOffset * mDecoratedChildHeight);
        }
    };
    scroller.setTargetPosition(position);
    startSmoothScroll(scroller);
}
Also used : PointF(android.graphics.PointF) LinearSmoothScroller(android.support.v7.widget.LinearSmoothScroller)

Example 14 with LinearSmoothScroller

use of android.support.v7.widget.LinearSmoothScroller in project UltimateAndroid by cymcsg.

the class StaticGridLayoutManager method smoothScrollToPosition.

/*
     * You must override this method if you would like to support external calls
     * to animate a change to a new adapter position. The framework provides a
     * helper scroller implementation (LinearSmoothScroller), which we leverage
     * to do the animation calculations.
     */
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, final int position) {
    if (position >= getItemCount()) {
        Log.e(TAG, "Cannot scroll to " + position + ", item count is " + getItemCount());
        return;
    }
    /*
         * LinearSmoothScroller's default behavior is to scroll the contents until
         * the child is fully visible. It will snap to the top-left or bottom-right
         * of the parent depending on whether the direction of travel was positive
         * or negative.
         */
    LinearSmoothScroller scroller = new LinearSmoothScroller(recyclerView.getContext()) {

        /*
             * LinearSmoothScroller, at a minimum, just need to know the vector
             * (x/y distance) to travel in order to get from the current positioning
             * to the target.
             */
        @Override
        public PointF computeScrollVectorForPosition(int targetPosition) {
            final int rowOffset = getGlobalRowOfPosition(targetPosition) - getGlobalRowOfPosition(mFirstVisiblePosition);
            final int columnOffset = getGlobalColumnOfPosition(targetPosition) - getGlobalColumnOfPosition(mFirstVisiblePosition);
            return new PointF(columnOffset * mDecoratedChildWidth, rowOffset * mDecoratedChildHeight);
        }
    };
    scroller.setTargetPosition(position);
    startSmoothScroll(scroller);
}
Also used : PointF(android.graphics.PointF) LinearSmoothScroller(android.support.v7.widget.LinearSmoothScroller)

Aggregations

LinearSmoothScroller (android.support.v7.widget.LinearSmoothScroller)14 PointF (android.graphics.PointF)9 RecyclerView (android.support.v7.widget.RecyclerView)4 View (android.view.View)4 DisplayMetrics (android.util.DisplayMetrics)3 LinearInterpolator (android.view.animation.LinearInterpolator)2 AnchorViewState (com.beloo.widget.chipslayoutmanager.anchor.AnchorViewState)2 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1