谢谢网上的资源,结合自己的 kotlin 项目。特意做了项目需求的变更,但只是语言的转换,根本实现思想与网上一致。这个已是普遍的继承扩展操作方法。只做个人的 使用记录:
1、继承
class ProtectScrollView : ScrollView {
private var scrollChangeListener: ScrollViewListener? = null
//实现单参数context的构造方法
constructor(context: Context) : super(context)
//实现双参数的构造方法
constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet)
//int x, int y, int oldx, int oldy
//继承scrollView的 onScrollChanged方法,并外部传入数据接口实例监听回调
override fun onScrollChanged(x: Int, y: Int, oldx: Int, oldy: Int) {
super.onScrollChanged(x, y, oldx, oldy)
scrollChangeListener?.let { it.onScrollChanged(this, x, y, oldx, oldy) }
}
fun setScrollChangedListener(scroviewListener: ScrollViewListener) {
this.scrollChangeListener = scroviewListener
}
interface ScrollViewListener {
fun onScrollChanged(
@Nonnull scrollView: ProtectScrollView,
x: Int,
y: Int,
oldx: Int,
oldy: Int
)
}
}
2、使用:
//scrollview 为上例中的自定义对象,设置于 xml中,可通过 findViewById或 binding获取
scrollview.setScrollChangedListener(object:ProtectScrollView.ScrollViewListener{
override fun onScrollChanged(
view: ProtectScrollView,
x: Int,
y: Int,
oldx: Int,
oldy: Int
) {
val scrollY = view.scrollY
val height = view.height
val scrollViewMeasuredHeight: Int = binding.svProtectText.getChildAt(0).measuredHeight
if (scrollY == 0) {
println(“滑动到了顶端 view.getScrollY()=KaTeX parse error: Expected 'EOF', got '}' at position 31: … }̲ …scrollY”)
println(“滑动到了底部 height=
h
e
i
g
h
t
"
)
p
r
i
n
t
l
n
(
"
滑
动
到
了
底
部
s
c
r
o
l
l
V
i
e
w
M
e
a
s
u
r
e
d
H
e
i
g
h
t
=
height") println("滑动到了底部 scrollViewMeasuredHeight=
height")println("滑动到了底部scrollViewMeasuredHeight=scrollViewMeasuredHeight”)
showProtectCompleted()
}else{
showReadProtect()
}
}
}
总结: 使用 这种方法,主要解决 直接使用 ScrollViewr的 onScrollChange方法的 API 23或以上的问题。