2011-03-13 26 views
2

TextViewをスクロールして、テキスト内の特定の位置を表示したいと考えています。どうやってやるの?私はbringPointIntoView(int offset)を試みましたが、成功しませんでした。TextViewをテキスト位置にスクロール

ソースコード:同じ問題を抱えている人のために

public class TextScrollActivity extends Activity { 
    public void onCreate (final Bundle savedInstanceState) { 
    super.onCreate (savedInstanceState); 
    final int position = 500; 
    final TextView textView = new TextView (this); 
    final ScrollView scrollView = new ScrollView (this); 
    scrollView.addView (textView); 
    Button button = new Button (this); 
    button.setText ("Scroll to " + position); 
    LinearLayout layout = new LinearLayout (this); 
    layout.setOrientation (LinearLayout.VERTICAL); 
    layout.addView (scrollView, 
    new LayoutParams (LayoutParams.FILL_PARENT, 200)); 
    layout.addView (button, new LayoutParams (LayoutParams.FILL_PARENT, 
    LayoutParams.WRAP_CONTENT)); 
    StringBuilder builder = new StringBuilder(); 
    for (int i = 0; i < 1000; i++) 
     builder.append (String.format ("[ %05d ] ", i)); 
    textView.setText (builder); 
    setContentView (layout); 
    button.setOnClickListener (new OnClickListener() { 
     public void onClick (View v) { 
     System.out.println (textView.bringPointIntoView (position * 10)); 
     // scrollView.scrollTo (0, position * 10); // no 
     } 
    }); 
    } 
} 
+0

ScrollViewを削除すると、メソッド 'bringPointIntoView'が動作するように見えますが、今はTextViewをスクロールできません...どうすればこの問題を解決できますか? – Patrick

答えて

3

、私は最終的に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); 
    } 

は躊躇しないでください。

1

テキストビューに移動方法を追加すると問題は解決しますか?

textView.setMovementMethod(new ScrollingMovementMethod()); 
+0

再描画の問題があり、flingアニメーションがなく、スクロールバーもないため、部分的にしか使用できません。とにかくありがとう :) – Patrick

1

ちょうどFYI同じ問題で他の誰のために、大きなlistItemslistViewに、オーバーロードさbringPointIntoViewではなくScrollViewListViewを通過して、代わりにsmoothScrollToScrollTo方法を使用することができます。

関連する問題