Опиши что делают все эти методы и расскажи о классе в целом : /* * Copyright 2018 The…
- User
Опиши что делают все эти методы и расскажи о классе в целом : /*
- Copyright 2018 The Android Open Source Project
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
http://www.apache.org/licenses/LICENSE-2.0 - Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License. */
package androidx.recyclerview.widget;
import android.graphics.PointF; import android.view.View;
import androidx.annotation.NonNull; import androidx.annotation.Nullable;
/**
-
Implementation of the {@link SnapHelper} supporting snapping in either vertical or horizontal
-
orientation.
-
The implementation will snap the center of the target child view to the center of
-
the attached {@link RecyclerView}. If you intend to change this behavior then override
-
{@link SnapHelper#calculateDistanceToFinalSnap}. */ public class LinearSnapHelper extends SnapHelper {
private static final float INVALID_DISTANCE = 1f;
// Orientation helpers are lazily created per LayoutManager. @Nullable private OrientationHelper mVerticalHelper; @Nullable private OrientationHelper mHorizontalHelper;
@Override public int[] calculateDistanceToFinalSnap( @NonNull RecyclerView.LayoutManager layoutManager, @NonNull View targetView) { int[] out = new int[2]; if (layoutManager.canScrollHorizontally()) { out[0] = distanceToCenter(layoutManager, targetView, getHorizontalHelper(layoutManager)); } else { out[0] = 0; }
if (layoutManager.canScrollVertically()) { out[1] = distanceToCenter(layoutManager, targetView, getVerticalHelper(layoutManager)); } else { out[1] = 0; } return out;}
@Override public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX, int velocityY) { if (!(layoutManager instanceof RecyclerView.SmoothScroller.ScrollVectorProvider)) { return RecyclerView.NO_POSITION; }
final int itemCount = layoutManager.getItemCount(); if (itemCount == 0) { return RecyclerView.NO_POSITION; } final View currentView = findSnapView(layoutManager); if (currentView == null) { return RecyclerView.NO_POSITION; } final int currentPosition = layoutManager.getPosition(currentView); if (currentPosition == RecyclerView.NO_POSITION) { return RecyclerView.NO_POSITION; } RecyclerView.SmoothScroller.ScrollVectorProvider vectorProvider = (RecyclerView.SmoothScroller.ScrollVectorProvider) layoutManager; // deltaJumps sign comes from the velocity which may not match the order of children in // the LayoutManager. To overcome this, we ask for a vector from the LayoutManager to // get the direction. PointF vectorForEnd = vectorProvider.computeScrollVectorForPosition(itemCount - 1); if (vectorForEnd == null) { // cannot get a vector for the given position. return RecyclerView.NO_POSITION; } int vDeltaJump, hDeltaJump; if (layoutManager.canScrollHorizontally()) { hDeltaJump = estimateNextPositionDiffForFling(layoutManager, getHorizontalHelper(layoutManager), velocityX, 0); if (vectorForEnd.x < 0) { hDeltaJump = -hDeltaJump; } } else { hDeltaJump = 0; } if (layoutManager.canScrollVertically()) { vDeltaJump = estimateNextPositionDiffForFling(layoutManager, getVerticalHelper(layoutManager), 0, velocityY); if (vectorForEnd.y < 0) { vDeltaJump = -vDeltaJump; } } else { vDeltaJump = 0; } int deltaJump = layoutManager.canScrollVertically() ? vDeltaJump : hDeltaJump; if (deltaJump == 0) { return RecyclerView.NO_POSITION; } int targetPos = currentPosition + deltaJump; if (targetPos < 0) { targetPos = 0; } if (targetPos >= itemCount) { targetPos = itemCount - 1; } return targetPos;
- Assistant
Класс LinearSnapHelper является реализацией SnapHelper и предоставляет функциональность для привязки элементов к центру RecyclerView. LinearSnapHelper поддерживает снимание как в вертикальной, так и в горизонтальной ориентациях.
Основные методы класса LinearSnapHelper:
calculateDistanceToFinalSnap: вычисляет расстояние до цести по диагонали в пикселях от текущего прокручиваемого элемента до центра RecyclerView.findTargetSnapPosition: находит позицию элемента, к которому требуется выполнить снимание, в ответ на прокрутку.findSnapView: возвращает элемент, который ближе всего к центру RecyclerView.
Данный класс также содержит вспомогательные методы:
distanceToCenter,estimateNextPositionDiffForFling, и . Эти методы помогают рассчитывать расстояния до центра, позиции элемента и другие значения, необходимые для выполнения снимания. - User
опиши все методы
- Assistant