我正在使用
我正在思考如何让卡片更快地自动吸附。我尝试创建一个自定义的
我认为问题可能是我不太理解
LinearSnapHelper
来让我的RecyclerView
中的项目"自动吸附"到屏幕上(我的卡片占据了大部分屏幕,所以我希望它们在每次滑动/快速滑动/滚动时都能自动吸附并填充屏幕)。我正在思考如何让卡片更快地自动吸附。我尝试创建一个自定义的
LinearLayoutManager
(并编辑scrollToPosition
或smoothScrollToPosition
中的calculateSpeedPerPixel
方法),以及一个自定义的RecyclerView
(并编辑快速滑动方法)。但是这些都无法影响卡片自动吸附的速度。我认为问题可能是我不太理解
LinearSnapHelper
如何将卡片滚动到位。它似乎没有使用LinearLayoutManager
的scrollToPosition
或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);