- 此view是自定義view,用戶能夠修改其繼承的父類
package com.koolearn.android.view
import android.content.Context
import android.support.constraint.ConstraintLayout
import android.util.AttributeSet
import android.view.MotionEvent
class SlideTopView(context: Context,attr: AttributeSet):ConstraintLayout(context,attr){
// private var lastX = 0
private var lastY = 0;
private lateinit var onSlideTopListener:(Int)->Unit
override fun onTouchEvent(event: MotionEvent): Boolean {
// val x = event.rawX.toInt()
val y = event.rawY.toInt()
when (event.action) {
MotionEvent.ACTION_DOWN -> {
// lastX = x
lastY = y
}
MotionEvent.ACTION_MOVE -> {
// val offsetX = x - lastX
var offsetY = y - lastY
if(top+offsetY>0){//只能向上滑動
offsetY=0
}
layout(left,top + offsetY,right,bottom + offsetY)
onSlideTopListener(top+offsetY)
// lastX = x
lastY = y
}
MotionEvent.ACTION_UP -> {
}
}
return true
}
fun setOnSlideTop(onSlideTopListener:(currentTop:Int)->Unit){
this.onSlideTopListener = onSlideTopListener;
}
}
複製代碼