我正在使用一个
LinearSnapHelper
来使我的
RecyclerView
中的项目在屏幕上 "卡 "住。我正在使用一个
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);