【问题标题】:scrollVIew.fullScroll(HorizontalScrollView.FOCUS_RIGHT) doesn't scroll to endscrollVIew.fullScroll(Horizo​​ntalScrollView.FOCUS_RIGHT) 不会滚动到结束
【发布时间】:2018-06-22 13:21:26
【问题描述】:

我正在制作简单的计算器,其设计类似于 Google 的计算器。我在 Horizo​​ntalScrollView 中有带有计算的文本,我希望它在键入时滚动到计算的末尾。我已经用这段代码(Kotlin)做到了:

textView.addTextChangedListener(object : TextWatcher {

        override fun afterTextChanged(s: Editable) {
            scrollVIew.fullScroll(HorizontalScrollView.FOCUS_RIGHT)
        }

        override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
        override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
    })

这里是带有滚动视图的 .xml 片段:

<HorizontalScrollView
    android:id="@+id/scrollVIew"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="0"
        android:textAlignment="textEnd"
        android:textSize="75sp" />
</HorizontalScrollView>

而且这种方法很有效,当文本更新时,文本会滚动到最后一个聊天,但不是最后一个。有谁知道为什么?

【问题讨论】:

    标签: android kotlin horizontalscrollview kotlin-android-extensions


    【解决方案1】:

    只需使用post 即可完成全滚动:

    textView.post { scrollVIew.fullScroll(HorizontalScrollView.FOCUS_RIGHT) }
    

    这是因为在afterTextChanged回调中,textView的文本没有完成改变,所以你应该在下一个runloop中使用post进行滚动。

    【讨论】:

      猜你喜欢
      • 2014-10-17
      • 2012-08-27
      • 1970-01-01
      • 2012-11-23
      • 2011-06-10
      • 1970-01-01
      • 2015-11-17
      • 2021-09-05
      • 1970-01-01
      相关资源
      最近更新 更多