示例是LinearLayout布局,这里可灵活变换
/**
* @CreateDate: 2021/1/11 14:59
* @Author: ZZJ
* @Description: 可以拖拽高度的View
*/
class DragHeightView : LinearLayout {
private var parentHeight = 0
private var parentWidth = 0
private var lastX = 0
private var lastY = 0
private var button: AppCompatImageView? = null
constructor(context: Context) : super(context) {
initView(context)
}
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
initView(context)
}
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
initView(context)
}
@SuppressLint("ClickableViewAccessibility")
private fun initView(context: Context) {
button = AppCompatImageView(context)
addView(button)
val layoutParams2 = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
button?.layoutParams = layoutParams2
button?.setImageDrawable(context.getDrawable(R.drawable.ic_practice_top))
button?.scaleType = ImageView.ScaleType.FIT_XY
button?.setOnTouchListener { _, event ->
val rawX = event.rawX.toInt()
val rawY = event.rawY.toInt()
when (event.action and MotionEvent.ACTION_MASK) {
MotionEvent.ACTION_DOWN -> {
isPressed = true
parent.requestDisallowInterceptTouchEvent(true)
lastX = rawX
lastY = rawY
val parent: ViewGroup
if (getParent() != null) {
parent = getParent() as ViewGroup
parentHeight = parent.height
parentWidth = parent.width
}
}
MotionEvent.ACTION_MOVE -> {
//计算手指移动了多少
val dx: Int = rawX - lastX
val dy: Int = rawY - lastY
val mLayoutParams = layoutParams
when {
mLayoutParams.height - dy <= 300 -> {
mLayoutParams.height = 300
}
mLayoutParams.height - dy >= parentHeight -> {
mLayoutParams.height = parentHeight
}
else -> {
mLayoutParams.height = mLayoutParams.height - dy
}
}
layoutParams = mLayoutParams
lastX = rawX
lastY = rawY
}
MotionEvent.ACTION_UP -> {
//恢复按压效果
isPressed = false
}
}
//如果是拖拽则消s耗事件,否则正常传递即可。
true
}
//监听软键盘 解决材料题评分遮挡问题
SoftKeyBoardListener.setListener(context as Activity,
object : SoftKeyBoardListener.OnSoftKeyBoardChangeListener {
override fun keyBoardShow(height: Int) {
//软键盘显示
val mLayoutParams = layoutParams
if (parent != null) {
parentHeight = (parent as ViewGroup).height
mLayoutParams.height = parentHeight
} else {
mLayoutParams.height = height + 300
}
layoutParams = mLayoutParams
}
override fun keyBoardHide(height: Int) {
}
})
}
}
软件盘监听类--这个是项目特殊需求
/**
* @CreateDate: 2020/4/24 17:47
* @Author: ZZJ
* @Description: 监听软键盘
*/
class SoftKeyBoardListener(activity: Activity) {
private val rootView: View = activity.window.decorView//activity的根视图
private var rootViewVisibleHeight: Int = 0//纪录根视图的显示高度
private var onSoftKeyBoardChangeListener: OnSoftKeyBoardChangeListener? = null
init {
//获取activity的根视图
//监听视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变
rootView.viewTreeObserver.addOnGlobalLayoutListener(ViewTreeObserver.OnGlobalLayoutListener {
//获取当前根视图在屏幕上显示的大小
val r = Rect()
rootView.getWindowVisibleDisplayFrame(r)
val visibleHeight = r.height()
println("" + visibleHeight)
if (rootViewVisibleHeight == 0) {
rootViewVisibleHeight = visibleHeight
return@OnGlobalLayoutListener
}
//根视图显示高度没有变化,可以看作软键盘显示/隐藏状态没有改变
if (rootViewVisibleHeight == visibleHeight) {
return@OnGlobalLayoutListener
}
//根视图显示高度变小超过300,可以看作软键盘显示了,该数值可根据需要自行调整
if (rootViewVisibleHeight - visibleHeight > 200) {
if (onSoftKeyBoardChangeListener != null) {
onSoftKeyBoardChangeListener?.keyBoardShow(rootViewVisibleHeight - visibleHeight)
}
rootViewVisibleHeight = visibleHeight
return@OnGlobalLayoutListener
}
//根视图显示高度变大超过300,可以看作软键盘隐藏了,该数值可根据需要自行调整
if (visibleHeight - rootViewVisibleHeight > 200) {
if (onSoftKeyBoardChangeListener != null) {
onSoftKeyBoardChangeListener!!.keyBoardHide(visibleHeight - rootViewVisibleHeight)
}
rootViewVisibleHeight = visibleHeight
return@OnGlobalLayoutListener
}
})
}
private fun setOnSoftKeyBoardChangeListener(onSoftKeyBoardChangeListener: OnSoftKeyBoardChangeListener) {
this.onSoftKeyBoardChangeListener = onSoftKeyBoardChangeListener
}
interface OnSoftKeyBoardChangeListener {
fun keyBoardShow(height: Int)
fun keyBoardHide(height: Int)
}
companion object {
@JvmStatic
fun setListener(activity: Activity, onSoftKeyBoardChangeListener: OnSoftKeyBoardChangeListener) {
val softKeyBoardListener = SoftKeyBoardListener(activity)
softKeyBoardListener.setOnSoftKeyBoardChangeListener(onSoftKeyBoardChangeListener)
}
}
}