Scroll TextView to text position

后端 未结 3 1348
灰色年华
灰色年华 2020-12-20 03:51

I want to scroll my TextView to make visible a specific position in the text. How can I do that? I tried bringPointIntoView (int offset) but without success.

Source

相关标签:
3条回答
  • 2020-12-20 04:12

    Does adding a movement method to the text view solve the problem?

    textView.setMovementMethod(new ScrollingMovementMethod());
    
    0 讨论(0)
  • 2020-12-20 04:14

    Just FYI for anyone else with the same problem, on a listView with large listItems, the overloaded bringPointIntoView can be passed a ListView instead of a ScrollView and use the ScrollTo method instead of smoothScrollTo.

    0 讨论(0)
  • 2020-12-20 04:18

    For those who have the same problem, I finally made my own implementation of bringPointIntoView:

      public static void bringPointIntoView (TextView textView,
      ScrollView scrollView, int offset)
      {
        int line = textView.getLayout ().getLineForOffset (offset);
        int y = (int) ((line + 0.5) * textView.getLineHeight ());
        scrollView.smoothScrollTo (0, y - scrollView.getHeight () / 2);
      }
    

    Don't hesitate if you have a better solution.

    0 讨论(0)
提交回复
热议问题