I'm assuming there's no horizontal scrolling. If there is, you can extrapolate from this answer. Seeing as you're highlighting the matches, I'm also going to assume you have the finding part down already. This means you should have at least a start position for the search string in the CharSequence
.
Use Layout.getLineForOffset
to find the line for the string, then Layout.getLineTop
to get the position of the top of the line, then View.scrollTo
to scroll to your desired position. It should look something like this:
Layout layout = textView.getLayout();
scrollView.scrollTo(0, layout.getLineTop(layout.getLineForOffset(startPos)));
I haven't tried this out, so you might have to do something to deal with the padding that you've added, etc. but I would think the general idea should work.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…