水平スクロール可能な を実装しましたRecyclerView
。私RecyclerView
は aLinearLayoutManager
を使用していますが、私が直面している問題は、scrollToPosition(position)
orsmoothScrollToPosition(position)
または fromを使用しようとしたときLinearLayoutManager
ですscrollToPositionWithOffset(position)
。どちらも私にはうまくいきません。スクロール呼び出しが目的の場所にスクロールしないか、OnScrollListener
.
これまで、さまざまなコードの組み合わせを試してきたので、ここにすべてを掲載することはできません。以下は私のために働くものです(しかし部分的にのみ):
public void smoothUserScrollTo(final int position) {
if (position < 0 || position > getAdapter().getItemCount()) {
Log.e(TAG, "An attempt to scroll out of adapter size has been stopped.");
return;
}
if (getLayoutManager() == null) {
Log.e(TAG, "Cannot scroll to position a LayoutManager is not set. " +
"Call setLayoutManager with a non-null layout.");
return;
}
if (getChildAdapterPosition(getCenterView()) == position) {
return;
}
stopScroll();
scrollToPosition(position);
if (lastScrollPosition == position) {
addOnLayoutChangeListener(new OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
if (left == oldLeft && right == oldRight && top == oldTop && bottom == oldBottom) {
removeOnLayoutChangeListener(this);
updateViews();
// removing the following line causes a position - 3 effect.
scrollToView(getChildAt(0));
}
}
});
}
lastScrollPosition = position;
}
@Override
public void scrollToPosition(int position) {
if (position < 0 || position > getAdapter().getItemCount()) {
Log.e(TAG, "An attempt to scroll out of adapter size has been stopped.");
return;
}
if (getLayoutManager() == null) {
Log.e(TAG, "Cannot scroll to position a LayoutManager is not set. " +
"Call setLayoutManager with a non-null layout.");
return;
}
// stopScroll();
((LinearLayoutManager) getLayoutManager()).scrollToPositionWithOffset(position, 0);
// getLayoutManager().scrollToPosition(position);
}
このscrollToPositionWithOffset()
ため選択しましたが、GridLayoutManager の代わりに LinearLayoutManager を使用しているため、おそらくケースが異なります。しかし、解決策は私にも有効ですが、先ほど述べたように、部分的にしか機能しません。
- スクロールの呼び出しが 0 番目の位置から totalSize - 7 までの場合、スクロールは魅力的に機能します。
- スクロールが totalSize - 7 から totalSize - 3 の場合、初めてリストの最後の 7 番目の項目までスクロールします。2回目ですが、うまくスクロールできます
- totalSize - 3 から totalSize までスクロールすると、予期しない動作が発生し始めます。
誰かが回避策を見つけた場合は、感謝します。私のカスタムコードの要点は次ReyclerView
のとおりです。