14

レイアウトに texteedit を使用してスクロールビューを定義しました。

 <ScrollView android:fillViewport="true"
     android:layout_marginBottom="50dip"
     android:id="@+id/start_scroller"
     android:layout_height="fill_parent"
     android:layout_width="fill_parent"
     android:fadingEdge="none">
 <TextView  
    android:id="@+id/text"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" >
 </TextView>
 </ScrollView>

次のメソッドを使用して、この ScrollView にテキストを追加します。

public void writeToLogView(String textMsg) {
   if (text.getText().equals("")) {
       text.append(textMsg);
   } else {
    text.append("\n" + textMsg);
    scroller.scrollBy(0, 1000000);
   }
}

ご覧のとおり、テキストを追加して、ScrollView の一番下までスクロールしようとしています。残念ながら、これは正しく機能しません。下にスクロールしますが、常にではなく、常に一番下までスクロールするとは限りません。ヒントはありますか?

4

3 に答える 3

30

または、scrollView の使用でビュー サイズを変更した後:

scroller.post(new Runnable() { 
    public void run() { 
        scroller.fullScroll(ScrollView.FOCUS_DOWN); 
    } 
}); 
于 2010-07-31T23:10:31.903 に答える
7

テキストを追加した後、TextViewのサイズを使用して、次のように定数ではなくスクロール先を計算してみてください。

scroller.post(new Runnable() {

    public void run() {
        scroller.scrollTo(text.getMeasuredWidth(), text.getMeasuredHeight());
    }
});
于 2010-05-19T11:41:56.130 に答える
0

For those who are using Android Annotations, you can do it like this:

@ViewById
ScrollView myScrollView;

@AfterViews
protected void init() {
    scrollToEnd();
}

@UiThread(delay=200)
void scrollToEnd() {
    myScrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
于 2014-05-18T11:58:51.510 に答える