feat:完成反馈功能
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
package com.example.lib_utils
|
||||
|
||||
import android.view.MotionEvent
|
||||
import android.view.View
|
||||
import android.view.View.OnTouchListener
|
||||
import android.widget.EditText
|
||||
|
||||
class SolveEditTextScrollClash(private val editText: EditText) : OnTouchListener {
|
||||
override fun onTouch(view: View, event: MotionEvent): Boolean {
|
||||
//触摸的是EditText而且当前EditText能够滚动则将事件交给EditText处理。否则将事件交由其父类处理
|
||||
if (view.id == editText.id && canVerticalScroll(editText)) {
|
||||
view.parent.requestDisallowInterceptTouchEvent(true)
|
||||
if (event.action == MotionEvent.ACTION_UP) {
|
||||
view.parent.requestDisallowInterceptTouchEvent(false)
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* EditText竖直方向能否够滚动
|
||||
* @param editText 须要推断的EditText
|
||||
* @return true:能够滚动 false:不能够滚动
|
||||
*/
|
||||
private fun canVerticalScroll(editText: EditText): Boolean {
|
||||
//滚动的距离
|
||||
val scrollY = editText.scrollY
|
||||
//控件内容的总高度
|
||||
val scrollRange = editText.layout.height
|
||||
//控件实际显示的高度
|
||||
val scrollExtent =
|
||||
editText.height - editText.compoundPaddingTop - editText.compoundPaddingBottom
|
||||
//控件内容总高度与实际显示高度的差值
|
||||
val scrollDifference = scrollRange - scrollExtent
|
||||
return if (scrollDifference == 0) {
|
||||
false
|
||||
} else scrollY > 0 || scrollY < scrollDifference - 1
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user