'Recycler view scroll to specific position

I have custom recyclerview with view Holder. I have one searchview. I want to move recycler view position as per searchview searched text. How can I achieve this task? so that search text filter will perform and according to filteration I will get position of filtered text. and using that I can move my focus (scroll) to that position.

Thank you



Solution 1:[1]

This will also work but but really slow and not effectively. Recycelerview do not load at same speed everytime that is why this one will be a bit laggy.

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
         myRecyclerview.scrollToPosition(position);
    }
 }, 200);

But after a long strugle i figure out a improved version of this which works really really well.

private void scrolltopos() {
        int index = 0;
        if (layoutManager.findFirstCompletelyVisibleItemPosition() != index) {
            recyclerview.getLayoutManager().scrollToPosition(index);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    scrolltopos();
                }
            }, 1000);
        } else {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    recyclerview.setVisibility(View.VISIBLE);
                }
            }, 1000);
        }
    }

This code is like a loop. Its gonna keep running untill it reaches your desired position. On top of that it will automatically make recyclerview visible after operation.

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 AJ Pro