Java Code Examples for android.support.v7.widget.LinearSmoothScroller#setTargetPosition()

The following examples show how to use android.support.v7.widget.LinearSmoothScroller#setTargetPosition() . 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: SnappyLinearLayoutManager.java    From Ouroboros with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
    final LinearSmoothScroller linearSmoothScroller =
            new LinearSmoothScroller(recyclerView.getContext()) {

                // I want a behavior where the scrolling always snaps to the beginning of
                // the list. Snapping to end is also trivial given the default implementation.
                // If you need a different behavior, you may need to override more
                // of the LinearSmoothScrolling methods.
                protected int getHorizontalSnapPreference() {
                    return SNAP_TO_START;
                }

                protected int getVerticalSnapPreference() {
                    return SNAP_TO_START;
                }

                @Override
                public PointF computeScrollVectorForPosition(int targetPosition) {
                    return SnappyLinearLayoutManager.this
                            .computeScrollVectorForPosition(targetPosition);
                }
            };
    linearSmoothScroller.setTargetPosition(position);
    startSmoothScroll(linearSmoothScroller);
}
 
Example 2
Source File: ActivityWithRecycler.java    From xDrip-plus with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {

    final LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(recyclerView.getContext()) {

        @Override
        public PointF computeScrollVectorForPosition(int targetPosition) {
            return CustomLinearLayoutManager.this.computeScrollVectorForPosition(targetPosition);
        }

        @Override
        protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
            return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
        }
    };

    linearSmoothScroller.setTargetPosition(position);
    try {
        startSmoothScroll(linearSmoothScroller);
    } catch (IllegalArgumentException e) {
        // couldn't scroll for some reason, just ignore
    }
}
 
Example 3
Source File: StickyHeaderGridLayoutManager.java    From Sticky-Header-Grid with MIT License 6 votes vote down vote up
@Override
public void smoothScrollToPosition(final RecyclerView recyclerView, RecyclerView.State state, int position) {
   final LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(recyclerView.getContext()) {
      @Override
      public int calculateDyToMakeVisible(View view, int snapPreference) {
         final RecyclerView.LayoutManager layoutManager = getLayoutManager();
         if (layoutManager == null || !layoutManager.canScrollVertically()) {
            return 0;
         }

         final int adapterPosition = getPosition(view);
         final int topOffset = getPositionSectionHeaderHeight(adapterPosition);
         final int top = layoutManager.getDecoratedTop(view);
         final int bottom = layoutManager.getDecoratedBottom(view);
         final int start = layoutManager.getPaddingTop() + topOffset;
         final int end = layoutManager.getHeight() - layoutManager.getPaddingBottom();
         return calculateDtToFit(top, bottom, start, end, snapPreference);
      }
   };
   linearSmoothScroller.setTargetPosition(position);
   startSmoothScroll(linearSmoothScroller);
}
 
Example 4
Source File: GridLayoutManager.java    From TvRecyclerView with Apache License 2.0 6 votes vote down vote up
private int startPositionSmoothScroller(int position) {
    LinearSmoothScroller linearSmoothScroller = new GridLinearSmoothScroller() {
        @Override
        public PointF computeScrollVectorForPosition(int targetPosition) {
            if (getChildCount() == 0) {
                return null;
            }
            final int firstChildPos = getPosition(getChildAt(0));
            final boolean isStart = targetPosition < firstChildPos;
            final int direction = isStart ? -1 : 1;
            if (mOrientation == HORIZONTAL) {
                return new PointF(direction, 0);
            } else {
                return new PointF(0, direction);
            }
        }

        @Override
        protected void onStop() {
            super.onStop();
        }
    };
    linearSmoothScroller.setTargetPosition(position);
    startSmoothScroll(linearSmoothScroller);
    return linearSmoothScroller.getTargetPosition();
}
 
Example 5
Source File: SpeedyLinearLayoutManager.java    From diycode with Apache License 2.0 6 votes vote down vote up
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int
        position) {

    final LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(recyclerView
            .getContext()) {

        @Override
        public PointF computeScrollVectorForPosition(int targetPosition) {
            return SpeedyLinearLayoutManager.this.computeScrollVectorForPosition
                    (targetPosition);
        }

        @Override
        protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
            return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
        }
    };

    linearSmoothScroller.setTargetPosition(position);
    startSmoothScroll(linearSmoothScroller);
}
 
Example 6
Source File: PreCachingLayoutManager.java    From iGap-Android with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {

    final LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(recyclerView.getContext()) {

        @Override
        public PointF computeScrollVectorForPosition(int targetPosition) {
            return PreCachingLayoutManager.this.computeScrollVectorForPosition(targetPosition);
        }

        @Override
        protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
            return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
        }
    };

    linearSmoothScroller.setTargetPosition(position);
    startSmoothScroll(linearSmoothScroller);
}
 
Example 7
Source File: StaticGridLayoutManager.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 8
Source File: TwoWayLayoutManager.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, State state, int position) {
    final LinearSmoothScroller scroller = new LinearSmoothScroller(recyclerView.getContext()) {
        @Override
        public PointF computeScrollVectorForPosition(int targetPosition) {
            if (getChildCount() == 0) {
                return null;
            }

            final int direction = targetPosition < getFirstVisiblePosition() ? -1 : 1;
            if (mIsVertical) {
                return new PointF(0, direction);
            } else {
                return new PointF(direction, 0);
            }
        }

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

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

    scroller.setTargetPosition(position);
    startSmoothScroll(scroller);
}
 
Example 9
Source File: ScrollableLinearLayoutManager.java    From nono-android with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
    LinearSmoothScroller scroller = new LinearSmoothScroller(_context) {
        @Override
        public PointF computeScrollVectorForPosition(int targetPosition) {
            return ScrollableLinearLayoutManager.this.computeScrollVectorForPosition(targetPosition);
        }
    };
    scroller.setTargetPosition(position);
    startSmoothScroll(scroller);

}
 
Example 10
Source File: SpannedGridLayoutManager.java    From AvatarTinderView with Apache License 2.0 5 votes vote down vote up
@Override
public void smoothScrollToPosition(
        RecyclerView recyclerView, RecyclerView.State state, int position) {
    if (position >= getItemCount()) position = getItemCount() - 1;

    LinearSmoothScroller scroller = new LinearSmoothScroller(recyclerView.getContext()) {
        @Override
        public PointF computeScrollVectorForPosition(int targetPosition) {
            final int rowOffset = getRowIndex(targetPosition) - firstVisibleRow;
            return new PointF(0, rowOffset * cellHeight);
        }
    };
    scroller.setTargetPosition(position);
    startSmoothScroll(scroller);
}
 
Example 11
Source File: HeaderLayoutManagerFixed.java    From android-parallax-recyclerview with Apache License 2.0 5 votes vote down vote up
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state,
                                   int position) {
    LinearSmoothScroller linearSmoothScroller =
            new LinearSmoothScroller(recyclerView.getContext()) {
                @Override
                public PointF computeScrollVectorForPosition(int targetPosition) {
                    return HeaderLayoutManagerFixed.this
                            .computeScrollVectorForPosition(targetPosition);
                }
            };
    linearSmoothScroller.setTargetPosition(position);
    startSmoothScroll(linearSmoothScroller);
}
 
Example 12
Source File: HorizontalLayoutManager.java    From Horizontal-Calendar with Apache License 2.0 5 votes vote down vote up
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
    LinearSmoothScroller smoothScroller = new LinearSmoothScroller(recyclerView.getContext()) {

        @Override
        protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
            return smoothScrollSpeed / displayMetrics.densityDpi;
        }

    };
    smoothScroller.setTargetPosition(position);
    startSmoothScroll(smoothScroller);
}
 
Example 13
Source File: FixedGridLayoutManager.java    From recyclerview-playground with MIT License 5 votes vote down vote up
@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);
}
 
Example 14
Source File: StaticGridLayoutManager.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 15
Source File: OverFlyingLayoutManager.java    From RecyclerBanner with Apache License 2.0 4 votes vote down vote up
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
    LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(recyclerView.getContext());
    linearSmoothScroller.setTargetPosition(position);
    startSmoothScroll(linearSmoothScroller);
}
 
Example 16
Source File: RecyclerViewPager.java    From oneHookLibraryAndroid with Apache License 2.0 4 votes vote down vote up
@Override
    public void smoothScrollToPosition(int position) {
        if (position >= getAdapter().getItemCount() || position < 0) {
            return;
        }
        mSmoothScrollTargetPosition = position;
        if (getLayoutManager() != null && getLayoutManager() instanceof LinearLayoutManager) {
            /*
             * exclude item decoration
             */
            final 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;
                            }
                            final int dx;
                            final int dy;
                            if (getLayoutManager().canScrollHorizontally()) {
                                /* horizontal */
                                dx = 0;
                                dy = 0;
                            } else {
                                dx = 0;
                                int targetTop = getLayoutManager().getDecoratedTop(targetView);
                                final int targetBottom = getLayoutManager().getDecoratedBottom(targetView);
                                final int targetHeight = targetBottom - targetTop;
//                                System.out.println("Target top : " + targetTop + " Target bottom : " + targetBottom + " , height " + targetHeight);
                                if (targetTop < 0 && Math.abs(targetTop) / 2 > targetHeight / 2) {
                                    while (targetTop < 0) {
                                        targetTop += targetHeight;
                                    }
                                }
                                if (targetTop > 0) {
                                    while (targetTop > targetHeight) {
                                        targetTop -= targetHeight;
                                    }
                                }
                                dy = -targetTop;
                            }

                            final int distance = (int) Math.sqrt(dx * dx + dy * dy);
                            final int time = calculateTimeForDeceleration(distance);
                            if (time > 0) {
                                action.update(-dx, -dy, time, mDecelerateInterpolator);
                            }
                        }
                    };
            linearSmoothScroller.setTargetPosition(position);
            getLayoutManager().startSmoothScroll(linearSmoothScroller);
        } else {
            super.smoothScrollToPosition(position);
        }
    }
 
Example 17
Source File: PagerRecyclerView.java    From DragBoardView with Apache License 2.0 4 votes vote down vote up
@Override
public void smoothScrollToPosition(int position) {
    mSmoothScrollTargetPosition = position;
    if (getLayoutManager() != null && getLayoutManager() instanceof LinearLayoutManager) {
        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, 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);
                }
            }
        };
        linearSmoothScroller.setTargetPosition(position);
        if (position == RecyclerView.NO_POSITION) {
            return;
        }
        getLayoutManager().startSmoothScroll(linearSmoothScroller);

    } else {
        super.smoothScrollToPosition(position);
    }
}
 
Example 18
Source File: DiscreteScrollLayoutManager.java    From XPlayer2 with Apache License 2.0 4 votes vote down vote up
private void startSmoothPendingScroll() {
    LinearSmoothScroller scroller = new DiscreteLinearSmoothScroller(context);
    scroller.setTargetPosition(currentPosition);
    recyclerViewProxy.startSmoothScroll(scroller);
}
 
Example 19
Source File: PagerRecyclerView.java    From timecat with Apache License 2.0 4 votes vote down vote up
@Override
public void smoothScrollToPosition(int position) {
    mSmoothScrollTargetPosition = position;
    if (getLayoutManager() != null && getLayoutManager() instanceof LinearLayoutManager) {
        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, 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);
                }
            }
        };
        linearSmoothScroller.setTargetPosition(position);
        if (position == RecyclerView.NO_POSITION) {
            return;
        }
        getLayoutManager().startSmoothScroll(linearSmoothScroller);

    } else {
        super.smoothScrollToPosition(position);
    }
}
 
Example 20
Source File: RecyclerViewPager.java    From RecyclerViewPager with Apache License 2.0 4 votes vote down vote up
@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;
                    }

                    @Override
                    protected void onStop() {
                        super.onStop();
                        if (mOnPageChangedListeners != null) {
                            for (OnPageChangedListener onPageChangedListener : mOnPageChangedListeners) {
                                if (onPageChangedListener != null) {
                                    onPageChangedListener.OnPageChanged(mPositionBeforeScroll, mSmoothScrollTargetPosition);
                                }
                            }
                        }
                        mHasCalledOnPageChanged = true;
                    }
                };

        linearSmoothScroller.setTargetPosition(position);
        if (position == RecyclerView.NO_POSITION) {
            return;
        }
        getLayoutManager().startSmoothScroll(linearSmoothScroller);
    } else {
        super.smoothScrollToPosition(position);
    }
}