RecyclerView-滚动到每次都不工作的位置 [英] RecyclerView - Scroll To Position Not Working Every Time
问题描述
我实现了水平可滚动RecyclerView
.我的RecyclerView
使用的是LinearLayoutManager
,而我面临的问题是,当我尝试使用scrollToPosition(position)
或smoothScrollToPosition(position)
或从LinearLayoutManager
的scrollToPositionWithOffset(position)
使用时.都不适合我.滚动调用不会滚动到所需位置,或者不会调用OnScrollListener
.
I have implemented a horizontal scrollable RecyclerView
. My RecyclerView
uses a LinearLayoutManager
, and the problem I am facing is that when I try to use scrollToPosition(position)
or smoothScrollToPosition(position)
or from LinearLayoutManager
's scrollToPositionWithOffset(position)
. Neither works for me. Either a scroll call doesn't scroll to the desired location or it doesn't invoke the OnScrollListener
.
到目前为止,我已经尝试了很多不同的代码组合,以至于无法在此处全部发布.以下是对我有用的内容(但仅部分适用):
So far I have tried so many different combinations of code that I cannot post them all here. Following is the one that works for me (But only partially):
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()
,但也许是这样有所不同,因为我使用LinearLayoutManager而不是GridLayoutManager.但是该解决方案也确实对我有用,但是正如我前面所说的那样.
I opted for scrollToPositionWithOffset()
because of this but the case perhaps is different as I use a LinearLayoutManager instead of GridLayoutManager. But the solution does work for me too, but as I said earlier only partially.
- 当滚动调用从第0个位置到totalSize时-7个滚动就像一个超级按钮.
- 当滚动从totalSize-7到totalSize-3时,我第一次只滚动到列表中的第7个最后一项.但是第二次我可以滚动
- 从totalSize-3滚动到totalSize时,我开始出现意外行为.
如果有人找到了解决方案,我将不胜感激.这是我的自定义代码ReyclerView
的要点.
If anyone has found a work around I'd Appreciate it. Here's the gist to my code of custom ReyclerView
.
推荐答案
几周前,我遇到了同样的问题,但发现只有一个非常糟糕的解决方案可以解决.必须使用200-300ms的postDelayed
.
I had the same issue some weeks ago, and found only a really bad solution to solve it. Had to use a postDelayed
with 200-300ms.
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
yourList.scrollToPosition(position);
}
}, 200);
如果您找到了更好的解决方案,请告诉我!祝你好运!
If you found a better solution, please let me know! Good luck!
这篇关于RecyclerView-滚动到每次都不工作的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!