简体   繁体   中英

ScrollView alawys scrolling to the bottom

I defined a scrollview with a texteedit in my layout:

 <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>

I add text to this ScrollView with the following method:

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

As you can see i append the text and try to scroll to the bottom of the ScrollView. Unfortunately this doesn't worked correctly. It scrolls down, but not always, and not always to the bottom. Any hints?

Or after change of view size in scrollView use:

scroller.post(new Runnable() { 
    public void run() { 
        scroller.fullScroll(ScrollView.FOCUS_DOWN); 
    } 
}); 

After appending the text, try using the dimensions of your TextView to calculate where you should scroll to, instead of some constant, like this:

scroller.post(new Runnable() {

    public void run() {
        scroller.scrollTo(text.getMeasuredWidth(), text.getMeasuredHeight());
    }
});

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM