Android kotlin NestedScrollView判断是否滚动到了TextView控件的底部
在Android中,可以通过监听NestedScrollView的滚动事件来判断是否滚动到了TextView控件的底部。以下是一种实现方法:
- 在XML布局文件中,将NestedScrollView和TextView控件进行嵌套:
<androidx.core.widget.NestedScrollView
android:id="@+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"/>
</androidx.core.widget.NestedScrollView>
- 在Activity(或Fragment)中,获取NestedScrollView和TextView控件,并添加滚动监听:
val nestedScrollView: NestedScrollView = findViewById(R.id.nestedScrollView)
val textView: TextView = findViewById(R.id.textView)
nestedScrollView.setOnScrollChangeListener { _, _, scrollY, _, oldScrollY ->
val isScrolledToBottom = scrollY == nestedScrollView.getChildAt(0).measuredHeight - nestedScrollView.measuredHeight
if (isScrolledToBottom) {
// 已滚动到底部
// 在此处执行相关操作
textView.text = "已滚动到底部"
}
}
在滚动监听的回调方法中,通过比较滚动的距离(scrollY)和NestedScrollView的内容高度(getChildAt(0).measuredHeight)减去NestedScrollView的高度(measuredHeight),来判断是否滚动到了底部。
如果滚动到了底部,可以在回调方法中执行相关操作,例如修改TextView的文本
原文地址: http://www.cveoy.top/t/topic/iWX7 著作权归作者所有。请勿转载和采集!