【问题标题】:ScrollView alawys scrolling to the bottomScrollView 总是滚动到底部
【发布时间】:2011-02-19 10:26:23
【问题描述】:

我在布局中定义了一个带有 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 的底部。不幸的是,这不能正常工作。它向下滚动,但并非总是如此,也不总是向下滚动。有什么提示吗?

【问题讨论】:

    标签: java android scrollview


    【解决方案1】:

    添加文本后,尝试使用 TextView 的尺寸来计算应该滚动到的位置,而不是一些常量,如下所示:

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

    【讨论】:

      【解决方案2】:

      或者在scrollView中改变视图大小后使用:

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

      【讨论】:

      • 这也适用于我,但我使用 postDelayed() 代替,延迟低,如 20 毫秒。
      • 很好的提示!我正在使用 scrollTo(0,0) 它什么也没做
      【解决方案3】:

      使用Android Annotations的朋友可以这样操作:

      @ViewById
      ScrollView myScrollView;
      
      @AfterViews
      protected void init() {
          scrollToEnd();
      }
      
      @UiThread(delay=200)
      void scrollToEnd() {
          myScrollView.fullScroll(ScrollView.FOCUS_DOWN);
      }
      

      【讨论】:

        猜你喜欢
        • 2013-02-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-01
        • 2020-03-26
        • 1970-01-01
        • 2020-02-11
        • 1970-01-01
        相关资源
        最近更新 更多