Java Code Examples for android.support.v7.widget.RecyclerView#SmoothScroller

The following examples show how to use android.support.v7.widget.RecyclerView#SmoothScroller . 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: PagerGridSnapHelper.java    From pager-layoutmanager with Apache License 2.0 6 votes vote down vote up
/**
 * 快速滚动的具体处理方案
 *
 * @param layoutManager 布局管理器
 * @param velocityX     X 轴滚动速率
 * @param velocityY     Y 轴滚动速率
 * @return 是否消费该事件
 */
private boolean snapFromFling(@NonNull RecyclerView.LayoutManager layoutManager, int velocityX,
                              int velocityY) {
    if (!(layoutManager instanceof RecyclerView.SmoothScroller.ScrollVectorProvider)) {
        return false;
    }

    RecyclerView.SmoothScroller smoothScroller = createSnapScroller(layoutManager);
    if (smoothScroller == null) {
        return false;
    }

    int targetPosition = findTargetSnapPosition(layoutManager, velocityX, velocityY);
    if (targetPosition == RecyclerView.NO_POSITION) {
        return false;
    }

    smoothScroller.setTargetPosition(targetPosition);
    layoutManager.startSmoothScroll(smoothScroller);
    return true;
}
 
Example 2
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 3
Source File: SnapHelper.java    From GridPagerSnapHelper with Apache License 2.0 6 votes vote down vote up
/**
 * Helper method to facilitate for snapping triggered by a fling.
 *
 * @param layoutManager The {@link RecyclerView.LayoutManager} associated with the attached
 *                      {@link RecyclerView}.
 * @param velocityX     Fling velocity on the horizontal axis.
 * @param velocityY     Fling velocity on the vertical axis.
 * @return true if it is handled, false otherwise.
 */
private boolean snapFromFling(@NonNull RecyclerView.LayoutManager layoutManager, int velocityX,
                              int velocityY) {
    if (!(layoutManager instanceof RecyclerView.SmoothScroller.ScrollVectorProvider)) {
        return false;
    }

    RecyclerView.SmoothScroller smoothScroller = createSnapScroller(layoutManager);
    if (smoothScroller == null) {
        return false;
    }

    int targetPosition = findTargetSnapPosition(layoutManager, velocityX, velocityY);
    if (targetPosition == RecyclerView.NO_POSITION) {
        return false;
    }

    smoothScroller.setTargetPosition(targetPosition);
    layoutManager.startSmoothScroll(smoothScroller);
    return true;
}
 
Example 4
Source File: SnappingLinearLayoutManager.java    From PlayTogether with Apache License 2.0 6 votes vote down vote up
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state,
                                   int position)
{
	RecyclerView.SmoothScroller smoothScroller = new TopSnappedSmoothScroller(recyclerView
			.getContext())
	{
		//This controls the direction in which smoothScroll looks for your view
		@Override
		public PointF computeScrollVectorForPosition(int targetPosition)
		{
			return new PointF(0, 1);
		}

		//This returns the milliseconds it takes to scroll one pixel.
		@Override
		protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics)
		{
			return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
		}
	};
	smoothScroller.setTargetPosition(position);
	startSmoothScroll(smoothScroller);
}
 
Example 5
Source File: SnappingLinearLayoutManager.java    From PowerSwitch_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) {
    RecyclerView.SmoothScroller smoothScroller = new TopSnappedSmoothScroller(recyclerView.getContext()) {
        //This controls the direction in which smoothScroll looks for your view
        @Override
        public PointF computeScrollVectorForPosition(int targetPosition) {
            return new PointF(0, 1);
        }

        //This returns the milliseconds it takes to scroll one pixel.
        @Override
        protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
            return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
        }
    };
    smoothScroller.setTargetPosition(position);
    startSmoothScroll(smoothScroller);
}
 
Example 6
Source File: GravitySnapHelper.java    From SuntimesWidget with GNU General Public License v3.0 6 votes vote down vote up
/**
 * @return true if the scroll will snap to a view, false otherwise
 */
private boolean scrollTo(int position, boolean smooth) {
    if (recyclerView.getLayoutManager() != null) {
        if (smooth) {
            RecyclerView.SmoothScroller smoothScroller
                    = createScroller(recyclerView.getLayoutManager());
            if (smoothScroller != null) {
                smoothScroller.setTargetPosition(position);
                recyclerView.getLayoutManager().startSmoothScroll(smoothScroller);
                return true;
            }
        } else {
            RecyclerView.ViewHolder viewHolder
                    = recyclerView.findViewHolderForAdapterPosition(position);
            if (viewHolder != null) {
                int[] distances = calculateDistanceToFinalSnap(recyclerView.getLayoutManager(),
                        viewHolder.itemView);
                recyclerView.scrollBy(distances[0], distances[1]);
                return true;
            }
        }
    }
    return false;
}
 
Example 7
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 8
Source File: ChipsLayoutManager.java    From ChipsLayoutManager with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, final int position) {
    if (position >= getItemCount() || position < 0) {
        Log.e("span layout manager", "Cannot scroll to " + position + ", item count " + getItemCount());
        return;
    }

    RecyclerView.SmoothScroller scroller = scrollingController.createSmoothScroller(recyclerView.getContext(), position, 150, anchorView);
    scroller.setTargetPosition(position);
    startSmoothScroll(scroller);
}
 
Example 9
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 10
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 11
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 12
Source File: ScrollersFragment.java    From MultiView with Apache License 2.0 5 votes vote down vote up
private void startSmoothScroll(RecyclerView.SmoothScroller smoothScroller) {

        int targetPos;
        int pos = recyclerView.getChildAdapterPosition(recyclerView.getLayoutManager().getChildAt(0));
        if (pos >= 150) {
            targetPos = 100;
        } else {
            targetPos = 200;
        }
        Log.d("scrolling from/to: " + pos + "/" + targetPos);
        smoothScroller.setTargetPosition(targetPos);
        recyclerView.getLayoutManager().startSmoothScroll(smoothScroller);
    }
 
Example 13
Source File: RecyclerViewSmoothScrollerAssert.java    From assertj-android with Apache License 2.0 4 votes vote down vote up
public RecyclerViewSmoothScrollerAssert(RecyclerView.SmoothScroller actual) {
  super(actual, RecyclerViewSmoothScrollerAssert.class);
}
 
Example 14
Source File: RecyclerViewProxy.java    From XPlayer2 with Apache License 2.0 4 votes vote down vote up
public void startSmoothScroll(RecyclerView.SmoothScroller smoothScroller) {
    layoutManager.startSmoothScroll(smoothScroller);
}
 
Example 15
Source File: DuplexGridLayoutManager.java    From MultiView with Apache License 2.0 4 votes vote down vote up
@Override
public void startSmoothScroll(RecyclerView.SmoothScroller smoothScroller) {
    super.startSmoothScroll(smoothScroller);
}
 
Example 16
Source File: AbstractSnapperLLM.java    From MultiView with Apache License 2.0 4 votes vote down vote up
@Override
public RecyclerView.SmoothScroller getSmoothScroller() {

    return new LinearSmoothScroller(ctx) {
        protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
            return millisecondsPerInch / displayMetrics.densityDpi;
        }

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

        @Override
        public int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int
                boxEnd, int
                                            snapPreference) {
            Log.d("calculateDtToFit " + viewStart + " : " + viewEnd + " : " +
                    boxStart + " : " + boxEnd + " : ");
            switch (snapMethod) {
                case SNAP_START:
                    return boxStart - viewStart;
                case SNAP_END:
                    return boxEnd - viewEnd;
                case SNAP_CENTER:
                    int boxMid = boxStart + (boxEnd - boxStart) / 2;
                    int viewMid = viewStart + (viewEnd - viewStart) / 2;
                    final int dt1 = boxMid - viewMid;
                    Log.d("calculateDtToFit2 " + boxMid + " : " + viewMid + " : " +
                            dt1);
                    return dt1;

                case SNAP_NONE:
                    final int dtStart = boxStart - viewStart;
                    if (dtStart > 0) {
                        return dtStart;
                    }
                    final int dtEnd = boxEnd - viewEnd;
                    if (dtEnd < 0) {
                        return dtEnd;
                    }
                    break;
                default:
                    throw new IllegalArgumentException("snap preference should be one" +
                            " of the"
                            + " constants defined in SnapperLinearLayoutManager, " +
                            "starting with SNAP_");
            }
            return 0;
        }

    };
}
 
Example 17
Source File: CleverLinearLayoutManager.java    From CleverRecyclerView with Apache License 2.0 4 votes vote down vote up
@Override
public void startSmoothScroll(RecyclerView.SmoothScroller smoothScroller) {
    super.startSmoothScroll(smoothScroller);
}
 
Example 18
Source File: AbstractSnapperLLM.java    From MultiView with Apache License 2.0 votes vote down vote up
RecyclerView.SmoothScroller getSmoothScroller(); 
Example 19
Source File: IScrollingController.java    From ChipsLayoutManager with Apache License 2.0 votes vote down vote up
RecyclerView.SmoothScroller createSmoothScroller(@NonNull Context context, int position, int timeMs, AnchorViewState anchor);