2

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.

Maximilian Riegler
  • 22,720
  • 4
  • 62
  • 71
Rafael Ruiz Muñoz
  • 5,333
  • 6
  • 46
  • 92

5 Answers5

6

I finally used:

recyclerView.scrollBy(int offsetX, int offsetY); setting offsetY = 0 and it works now.

I don't understand what's the utility of the function scrollToPositionWithOffset.

Rafael Ruiz Muñoz
  • 5,333
  • 6
  • 46
  • 92
  • 1
    Where do you call scrollToPositionWithOffset? In onCreate or after the onLayoutMeasure? – box Sep 15 '16 at 11:53
3

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.

Reynaldo Aguilar
  • 1,906
  • 1
  • 15
  • 29
0

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.

Community
  • 1
  • 1
Deliriom
  • 545
  • 4
  • 7
0

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
}
VinceStyling
  • 3,707
  • 3
  • 29
  • 44
0

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.

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 16 '22 at 01:39
  • Why are you telling that you are a developer of bla bla? If you think your answer will be helpful for others just write it clearly and if possible obviously with example code. – Md. Yamin Mollah Jun 01 '23 at 17:44