Android scrollview (textview) long text is positioned to the specified line

When doing a project, you need to hollow out a certain line of a long text to do multiple-choice questions, and you need to position the scroll bar to a certain line (fill in the blank line)

I checked it for a long time and didn't solve this problem. Later I found that it was mainly caused by two reasons: positioning offset and scrollTo() failure.

Suppose we want to achieve

 

Display a long text in a scrolling text box like this, and then need to locate a certain line

scrollView.scrollTo(0, textView.getLineHeight() * lineY);

However, when the text is very long, it often fails. This is because the scrollTo fails, and the reason for the failure is probably because the component is not initialized properly.

On the post, I saw mScroller.startScroll(mScroller.getFinalX(), mScroller.getFinalY(), (int) distanceX, 0,200); here is a 200ms delay reason

To solve this problem can be solved by the following code

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
    scrollView.scrollTo(0, textView.getLineHeight() * lineY);
//在该run方法中执行滚动操作
    }
 }, 2000); // 延迟2秒,再运行splashhandler的run()

 

Guess you like

Origin blog.csdn.net/qhlpptdyfn/article/details/89817346