'scrollToPositionWithOffset from LinearLayoutManager on RecyclerView not working
I'm trying to make an horizontal list of sticky images with RecyclerView and I'd like to move them by pixels' offset with scrollToPositionWithOffset. I thought passing 0 as position and the pixels I want to move to right / left as offset.
But it doesn't work, the list remains untouched, unscrolled, it doesn't move. This is my implementation:
final LargeImageAdapter mLargeImageAdapter = new LargeImageAdapter(this);
linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(mLargeImageAdapter);
seekBar = (SeekBar)findViewById(R.id.seekBar);
seekBar.setMax(7000);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int scrollToDX = progress;
((LinearLayoutManager)recyclerView.getLayoutManager()).scrollToPositionWithOffset(0, scrollToDX);
// tried invoking also linearLayoutManager instead getLayoutManager.
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
what am I doing wrong?
Thank you very much.
Regards.
Rafael.
Solution 1:[1]
I had a similar issue. My problem was that my recyclerview wasn't of the same size of its parent layout. I solved it by setting the recycler view width and height to match_parent. I don't know why this happens in this case.
Solution 2:[2]
A late answer to your first question, and an addition to your answer:
Your method works better for your personal needs, because scrollToPositionWithOffset is not intended to do what you want.
As the doc says here:
[...]Resolved layout start depends on [...] getLayoutDirection(android.view.View) [...]
Which means it would offset the scroll target position in the layout direction, vertically in your case.
I don't understand what's the utility of the function scrollToPositionWithOffset.
it allows to not only scroll to a given item in the list, but also position it at a more "visible" or otherwise convenient place.
Solution 3:[3]
recently I encountered this problem too, I invoke scrollToPositionWithOffset when onScrolled() directly, but nothing change, with that I turn to scrollToPosition() even scrollBy() but not help, finally I attempt to delay that so it work, first time I delay 50ms, but two weeks later I found that's not enough, so I increase to 100ms with no approachs in my hands, of course it work, just feel a little unsettled.
val layoutManager = LinearLayoutManager(hostActivity, VERTICAL, false)
fileRv.layoutManager = layoutManager
fileRv.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
if (dx == 0 && dy == 0) {
scrollToLastPosition()
}
}
private fun scrollToLastPosition() {
val lastScrollPosition = viewModel.consumeLastScrollPosition()
if (lastScrollPosition > 0) {
Handler().postDelayed({ layoutManager.scrollToPositionWithOffset(lastScrollPosition, 0) }, 100)
}
}
})
override fun onItemClick(position: Int) {
layoutManager.findFirstVisibleItemPosition().let {
if (it >= 0) viewModel.markLastScrollPosition(it)
}
}
fun markLastScrollPosition(position: Int) {
currentFolderListData.value?.lastOrNull()?.lastScrollPosition = position
}
fun consumeLastScrollPosition(): Int {
currentFolderListData.value?.lastOrNull()?.run {
return lastScrollPosition.apply { lastScrollPosition = -1 }
}
return 0
}
Solution 4:[4]
I find a solution. Coz I am the developer of DNA Launcher. When I use RecyclerView to display A-Z App List, I found that the function scrollToPositionWithOffset is not working. I track the problem for almost one day and I figured it out.
When the RecyclerView display again, just let the parent of RecyclerView do requestLayout.
It works for me.
And I know how to make the function scrollToPositionWithOffset not working. You just need to add a view on it and make it gone then.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Reynaldo Aguilar |
| Solution 2 | Community |
| Solution 3 | |
| Solution 4 | Atlantis Lee |
