9

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

Nik
  • 1,991
  • 2
  • 13
  • 30

3 Answers3

14

Try this:

myRecyclerview.scrollToPosition(position);

if not works in some cases(Keyboard opening etc.), try using delay.

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
         myRecyclerview.scrollToPosition(position);
    }
 }, 200);
Oğuzhan Döngül
  • 7,856
  • 4
  • 38
  • 52
  • Thank you for your reply. But I have one search view using that When I search any text and I need to go that position which I have searched. so How can I get searched position? – Nik May 07 '17 at 12:13
  • @Nik you need to write a search or filter algorithm for your list. I don't know your codes or what do you want to achieve but you need to use written keyword on your searchview and search it in your list. Then get your found item's position. Check this: http://stackoverflow.com/a/12496197/3669559 – Oğuzhan Döngül May 07 '17 at 13:11
0

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.

AJ Pro
  • 1
  • 1
0

try myRecyclerview.smoothScrollToPosition(position);