android.support.v7.widget.LinearSmoothScroller Java Examples

The following examples show how to use android.support.v7.widget.LinearSmoothScroller. 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: WorkWorldFragment.java    From imsdk-android with MIT License 6 votes vote down vote up
@Override
    public void scrollTop() {
        if (work_world_rc != null) {
            if(workWorldAdapter.getItemCount()>5){
                work_world_rc.scrollToPosition(5);
            }
            if(getActivity()!=null){
                LinearSmoothScroller smoothScroller = new WorkWorldDetailsActivity.TopSmoothScroller(getActivity());
                smoothScroller.setTargetPosition(0);
                mRcManager.startSmoothScroll(smoothScroller);
            }else{
                work_world_rc.smoothScrollToPosition(0);
            }

//
        }

    }
 
Example #2
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 #3
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 #4
Source File: ActivityWithRecycler.java    From xDrip 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 #5
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 #6
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 #7
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 #8
Source File: SnapHelper.java    From GridPagerSnapHelper with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a scroller to be used in the snapping implementation.
 *
 * @param layoutManager The {@link RecyclerView.LayoutManager} associated with the attached
 *                      {@link RecyclerView}.
 * @return a {@link LinearSmoothScroller} which will handle the scrolling.
 */
@Nullable
protected LinearSmoothScroller createSnapScroller(RecyclerView.LayoutManager layoutManager) {
    if (!(layoutManager instanceof RecyclerView.SmoothScroller.ScrollVectorProvider)) {
        return null;
    }
    return new LinearSmoothScroller(mRecyclerView.getContext()) {
        @Override
        protected void onTargetFound(View targetView, RecyclerView.State state, Action action) {
            int[] snapDistances = calculateDistanceToFinalSnap(mRecyclerView.getLayoutManager(),
                    targetView);
            final int dx = snapDistances[0];
            final int dy = snapDistances[1];
            final int time = calculateTimeForDeceleration(Math.max(Math.abs(dx), Math.abs(dy)));
            if (time > 0) {
                action.update(dx, dy, time, mDecelerateInterpolator);
            }
        }

        @Override
        protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
            return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
        }
    };
}
 
Example #9
Source File: ToDayView.java    From ToDay with MIT License 6 votes vote down vote up
@Override
public void smoothScrollToPosition(RecyclerView recyclerView,
                                   RecyclerView.State state,
                                   int position) {
    RecyclerView.SmoothScroller smoothScroller =
            new LinearSmoothScroller(recyclerView.getContext()) {
                @Override
                public PointF computeScrollVectorForPosition(int targetPosition) {
                    return AgendaLinearLayoutManager.this
                            .computeScrollVectorForPosition(targetPosition);
                }

                @Override
                protected int getVerticalSnapPreference() {
                    return SNAP_TO_START; // override base class behavior
                }
            };
    smoothScroller.setTargetPosition(position);
    startSmoothScroll(smoothScroller);
}
 
Example #10
Source File: WheelPickerLayoutManager.java    From RecyclerWheelPicker with Apache License 2.0 6 votes vote down vote up
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, final int position) {
    LinearSmoothScroller smoothScroller = new LinearSmoothScroller(recyclerView.getContext()) {
        @Override
        protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
            return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
        }

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

    smoothScroller.setTargetPosition(position);
    startSmoothScroll(smoothScroller);
}
 
Example #11
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 #12
Source File: ScrollSpeedLinearLayoutManager.java    From tysq-android with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
    LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(recyclerView.getContext()){
        @Nullable
        @Override
        public PointF computeScrollVectorForPosition(int targetPosition) {
            return ScrollSpeedLinearLayoutManager.this.computeScrollVectorForPosition(targetPosition);
        }

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

    linearSmoothScroller.setTargetPosition(position);
    startSmoothScroll(linearSmoothScroller);

}
 
Example #13
Source File: PagerGridSnapHelper.java    From pager-layoutmanager with Apache License 2.0 5 votes vote down vote up
/**
 * 通过自定义 LinearSmoothScroller 来控制速度
 *
 * @param layoutManager 布局故哪里去
 * @return 自定义 LinearSmoothScroller
 */
protected LinearSmoothScroller createSnapScroller(RecyclerView.LayoutManager layoutManager) {
    if (!(layoutManager instanceof RecyclerView.SmoothScroller.ScrollVectorProvider)) {
        return null;
    }
    return new PagerGridSmoothScroller(mRecyclerView);
}
 
Example #14
Source File: BaseLayoutManager.java    From KinoCast with MIT License 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 BaseLayoutManager.this
                            .computeScrollVectorForPosition(targetPosition);
                }
            };
    linearSmoothScroller.setTargetPosition(position);
    startSmoothScroll(linearSmoothScroller);
}
 
Example #15
Source File: FixedGridLayoutManager.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 #16
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 #17
Source File: FixedGridLayoutManager.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 #18
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 #19
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 #20
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 #21
Source File: AppDetailsRecyclerViewAdapter.java    From fdroidclient with GNU General Public License v3.0 5 votes vote down vote up
void setShowVersions(boolean showVersions, boolean scrollTo) {
    this.showVersions = showVersions;
    boolean itemsWereRemoved = items.removeAll(versions);
    int startIndex = items.indexOf(VIEWTYPE_VERSIONS) + 1;

    // When adding/removing items, be sure to only notifyItemInserted and notifyItemRemoved
    // rather than notifyDatasetChanged(). If we only notify about the entire thing, then
    // everything gets rebuilt, including the expandable "Versions" item. By rebuilding that
    // item it will interrupt the nice material-design-style-ripple from the background.
    if (showVersions) {
        items.addAll(startIndex, versions);
        notifyItemRangeInserted(startIndex, versions.size());
        if (recyclerView != null && scrollTo) {
            final LinearSmoothScroller smoothScroller = new LinearSmoothScroller(context) {
                @Override
                protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
                    // The default speed of smooth scrolling doesn't look good
                    // and it's too fast when it happens while inserting
                    // multiple recycler view items
                    return 75f / displayMetrics.densityDpi;
                }
            };
            // Expanding the version list reveals up to 5 items by default
            int visibleVersionLimit = Math.min(versions.size(), 5);
            smoothScroller.setTargetPosition(startIndex + visibleVersionLimit - 1);
            recyclerView.getLayoutManager().startSmoothScroll(smoothScroller);
        }
    } else if (itemsWereRemoved) {
        notifyItemRangeRemoved(startIndex, versions.size());
        if (recyclerView != null && scrollTo) {
            recyclerView.smoothScrollToPosition(startIndex - 1);
        }
    }
}
 
Example #22
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 #23
Source File: FlowLayoutManager.java    From AndroidLibs with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void smoothScrollToPosition(final RecyclerView recyclerView, final RecyclerView.State state, int position) {
	RecyclerView.SmoothScroller smoothScroller = new LinearSmoothScroller(recyclerView.getContext()) {
		@Override
		public PointF computeScrollVectorForPosition(int targetPosition) {
			return new PointF(0, getOffsetOfItemToFirstChild(targetPosition, recyclerRef));
		}
	};
	smoothScroller.setTargetPosition(position);
	startSmoothScroll(smoothScroller);
}
 
Example #24
Source File: GravitySnapHelper.java    From SuntimesWidget with GNU General Public License v3.0 5 votes vote down vote up
@Nullable
//@Override
public RecyclerView.SmoothScroller createScroller(RecyclerView.LayoutManager layoutManager) {
    if (!(layoutManager instanceof RecyclerView.SmoothScroller.ScrollVectorProvider)
            || recyclerView == null) {
        return null;
    }
    return new LinearSmoothScroller(recyclerView.getContext()) {
        @Override
        protected void onTargetFound(View targetView,
                                     RecyclerView.State state,
                                     RecyclerView.SmoothScroller.Action action) {
            if (recyclerView == null || recyclerView.getLayoutManager() == null) {
                // The associated RecyclerView has been removed so there is no action to take.
                return;
            }
            int[] snapDistances = calculateDistanceToFinalSnap(recyclerView.getLayoutManager(),
                    targetView);
            final int dx = snapDistances[0];
            final int dy = snapDistances[1];
            final int time = calculateTimeForDeceleration(Math.max(Math.abs(dx), Math.abs(dy)));
            if (time > 0) {
                action.update(dx, dy, time, mDecelerateInterpolator);
            }
        }

        @Override
        protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
            return scrollMsPerInch / displayMetrics.densityDpi;
        }
    };
}
 
Example #25
Source File: VerticalScrollingController.java    From ChipsLayoutManager with Apache License 2.0 5 votes vote down vote up
@Override
public RecyclerView.SmoothScroller createSmoothScroller(@NonNull Context context, final int position, final int timeMs, final AnchorViewState anchor) {
    return new LinearSmoothScroller(context) {
        /*
         * 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) {
            int visiblePosition = anchor.getPosition();
            //determine scroll up or scroll down needed
            return new PointF(0, position > visiblePosition ? 1 : -1);
        }

        @Override
        protected void onTargetFound(View targetView, RecyclerView.State state, Action action) {
            super.onTargetFound(targetView, state, action);
            int desiredTop = lm.getPaddingTop();
            int currentTop = lm.getDecoratedTop(targetView);

            int dy = currentTop - desiredTop;

            //perform fit animation to move target view at top of layout
            action.update(0, dy, timeMs, new LinearInterpolator());
        }
    };
}
 
Example #26
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 #27
Source File: HorizontalScrollingController.java    From ChipsLayoutManager with Apache License 2.0 5 votes vote down vote up
@Override
public RecyclerView.SmoothScroller createSmoothScroller(@NonNull Context context, final int position, final int timeMs, final AnchorViewState anchor) {
    return new LinearSmoothScroller(context) {
        /*
         * 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) {
            int visiblePosition = anchor.getPosition();
            //determine scroll up or scroll down needed
            return new PointF(position > visiblePosition ? 1 : -1, 0);
        }

        @Override
        protected void onTargetFound(View targetView, RecyclerView.State state, Action action) {
            super.onTargetFound(targetView, state, action);
            int currentLeft = layoutManager.getPaddingLeft();
            int desiredLeft = layoutManager.getDecoratedLeft(targetView);

            int dx = desiredLeft - currentLeft;

            //perform fit animation to move target view at top of layoutX
            action.update(dx, 0, timeMs, new LinearInterpolator());
        }
    };
}
 
Example #28
Source File: PagerGridLayoutManager.java    From pager-layoutmanager with Apache License 2.0 5 votes vote down vote up
/**
 * 平滑滚动到指定页面
 *
 * @param pageIndex 页面下标
 */
public void smoothScrollToPage(int pageIndex) {
    if (pageIndex < 0 || pageIndex >= mLastPageCount) {
        Log.e(TAG, "pageIndex is outOfIndex, must in [0, " + mLastPageCount + ").");
        return;
    }
    if (null == mRecyclerView) {
        Log.e(TAG, "RecyclerView Not Found!");
        return;
    }

    // 如果滚动到页面之间距离过大,先直接滚动到目标页面到临近页面,在使用 smoothScroll 最终滚动到目标
    // 否则在滚动距离很大时,会导致滚动耗费的时间非常长
    int currentPageIndex = getPageIndexByOffset();
    if (Math.abs(pageIndex - currentPageIndex) > 3) {
        if (pageIndex > currentPageIndex) {
            scrollToPage(pageIndex - 3);
        } else if (pageIndex < currentPageIndex) {
            scrollToPage(pageIndex + 3);
        }
    }

    // 具体执行滚动
    LinearSmoothScroller smoothScroller = new PagerGridSmoothScroller(mRecyclerView);
    int position = pageIndex * mOnePageSize;
    smoothScroller.setTargetPosition(position);
    startSmoothScroll(smoothScroller);
}
 
Example #29
Source File: ScrollSnapHelper.java    From YCBanner with Apache License 2.0 5 votes vote down vote up
@Nullable
protected LinearSmoothScroller createSnapScroller(final RecyclerView.LayoutManager layoutManager) {
    if (!(layoutManager instanceof RecyclerView.SmoothScroller.ScrollVectorProvider)) {
        return null;
    }
    return new LinearSmoothScroller(mRecyclerView.getContext()) {
        @Override
        protected void onTargetFound(View targetView, RecyclerView.State state, Action action) {
            int[] snapDistances = calculateDistanceToFinalSnap(mRecyclerView.getLayoutManager(), targetView);
            final int dx;
            final int dy;
            if (snapDistances != null) {
                dx = snapDistances[0];
                dy = snapDistances[1];
                final int time = calculateTimeForDeceleration(Math.max(Math.abs(dx), Math.abs(dy)));
                if (time > 0) {
                    action.update(dx, dy, time, mDecelerateInterpolator);
                }
            }
        }

        @Override
        protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
            return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
        }
    };
}
 
Example #30
Source File: ScrollLinearHelper.java    From YCBanner with Apache License 2.0 5 votes vote down vote up
@Nullable
protected LinearSmoothScroller createSnapScroller(final RecyclerView.LayoutManager layoutManager) {
    if (!(layoutManager instanceof RecyclerView.SmoothScroller.ScrollVectorProvider)) {
        return null;
    }
    return new LinearSmoothScroller(mRecyclerView.getContext()) {
        @Override
        protected void onTargetFound(View targetView, RecyclerView.State state, RecyclerView.SmoothScroller.Action action) {
            int[] snapDistances = calculateDistanceToFinalSnap(mRecyclerView.getLayoutManager(), targetView);
            final int dx;
            final int dy;
            if (snapDistances != null) {
                dx = snapDistances[0];
                dy = snapDistances[1];
                final int time = calculateTimeForDeceleration(Math.max(Math.abs(dx), Math.abs(dy)));
                if (time > 0) {
                    action.update(dx, dy, time, mDecelerateInterpolator);
                }
            }
        }

        @Override
        protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
            //这个地方可以自己设置
            return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
        }
    };
}