0

I want to scroll to a specific string or index of text. i get search text position and i want to scroll or focus center of screen to my specific string in RecyclerView

I tried to many ways but not solved

now i scroll to like this

LinearLayoutManager llm = (LinearLayoutManager) recyclerView.getLayoutManager();
llm.scrollToPositionWithOffset(row, position);

but it scroll only row of RecyclerView. i need scroll to specific word can some one help me. thank you

Ashish
  • 6,005
  • 3
  • 17
  • 40

2 Answers2

1

layoutmanager.scrollToPosition(position);

or

recycler.getLayoutManager().scrollToPosition(position)

MIF50
  • 31
  • 1
  • 4
0

Create a method that returns the position of that TextView within your adapter. For this example, I created a very simple RecyclerView.Adapter that just holds a List items and is looking for the string "target".

private int getTargetPosition(RecyclerView recycler) {
    MyAdapter adapter = (MyAdapter) recycler.getAdapter();

for (int i = 0; i < adapter.items.size(); i++) {
        if (adapter.items.get(i).equals("target")) {
            return i;
        }
    }

    throw new AssertionError("target is guaranteed to be in the list");
}

After that, it's as easy as calling scrollToPosition():

int position = getTargetPosition(recycler);
recycler.scrollToPosition(position);
tushar
  • 184
  • 3
  • 14