我正在使用 aLinearSnapHelper
将我的RecyclerView
“卡”中的项目在屏幕上放置到位(我的卡片占据了屏幕的大部分,所以我希望它们卡到位并在每次滑动/甩动/滚动时填满屏幕)。
我正在为如何让卡片更快地卡入到位而苦苦挣扎。我尝试过创建自定义LinearLayoutManager
(并calculateSpeedPerPixel
在scrollToPosition
or中编辑方法smoothScrollToPosition
)以及自定义RecyclerView
(并编辑 fling 方法)。但没有什么影响卡片“卡入”到位的速度。
我想问题是我真的不明白如何LinearSnapHelper
将卡片“滚动”到位。它似乎没有使用LinearLayoutManager
'sscrollToPosition
或smoothScrollToPosition
方法。
snapHelper = new LinearSnapHelper() {
@Override
public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX, int velocityY) {
View centerView = findSnapView(layoutManager);
if (centerView == null) {
return RecyclerView.NO_POSITION;
}
int position = layoutManager.getPosition(centerView);
int targetPosition = -1;
if (layoutManager.canScrollHorizontally()) {
if (velocityX < 0) {
targetPosition = position - 1;
} else {
targetPosition = position + 1;
}
}
if (layoutManager.canScrollVertically()) {
if (velocityY > 0) {
targetPosition = position + 1;
} else {
targetPosition = position - 1;
}
}
final int firstItem = 0;
final int lastItem = layoutManager.getItemCount() - 1;
targetPosition = Math.min(lastItem, Math.max(targetPosition, firstItem));
return targetPosition;
}
};
snapHelper.attachToRecyclerView(mRecyclerView);