scrollToPositionWithOffset from LinearLayoutManager on RecyclerView not working

Rafael Ruiz Muñoz picture Rafael Ruiz Muñoz · Nov 11, 2015 · Viewed 15.3k times · Source

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.

Answer

Rafael Ruiz Muñoz picture Rafael Ruiz Muñoz · Nov 11, 2015

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.