From 92c2a78a198e1ccc25c3103c6b5d6d5f86b6ce03 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 1 Oct 2022 00:56:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NestScrollMultiAutoCompleteTextView.kt | 48 ++++++++++++------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/widget/text/NestScrollMultiAutoCompleteTextView.kt b/app/src/main/java/io/legado/app/ui/widget/text/NestScrollMultiAutoCompleteTextView.kt index 8c6f8d0cb..aed20bc8f 100644 --- a/app/src/main/java/io/legado/app/ui/widget/text/NestScrollMultiAutoCompleteTextView.kt +++ b/app/src/main/java/io/legado/app/ui/widget/text/NestScrollMultiAutoCompleteTextView.kt @@ -3,6 +3,7 @@ package io.legado.app.ui.widget.text import android.annotation.SuppressLint import android.content.Context import android.util.AttributeSet +import android.view.GestureDetector import android.view.MotionEvent import androidx.appcompat.widget.AppCompatMultiAutoCompleteTextView @@ -14,12 +15,38 @@ open class NestScrollMultiAutoCompleteTextView @JvmOverloads constructor( attrs: AttributeSet? = null ) : AppCompatMultiAutoCompleteTextView(context, attrs) { + //是否到顶或者到底的标志 + private var disallowIntercept = true //滑动距离的最大边界 private var mOffsetHeight = 0 - //是否到顶或者到底的标志 - private var mBottomFlag = false + private val gestureDetector = GestureDetector(context, + object : GestureDetector.SimpleOnGestureListener() { + + override fun onDown(e: MotionEvent): Boolean { + disallowIntercept = true + return super.onDown(e) + } + + override fun onScroll( + e1: MotionEvent, + e2: MotionEvent, + distanceX: Float, + distanceY: Float + ): Boolean { + val y = scrollY + distanceY + if (y < 0 || y > mOffsetHeight) { + disallowIntercept = false + //这里触发父布局或祖父布局的滑动事件 + parent.requestDisallowInterceptTouchEvent(false) + } else { + disallowIntercept = true + } + return true + } + + }) override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { super.onMeasure(widthMeasureSpec, heightMeasureSpec) @@ -37,9 +64,8 @@ open class NestScrollMultiAutoCompleteTextView @JvmOverloads constructor( } override fun dispatchTouchEvent(event: MotionEvent): Boolean { - if (event.action == MotionEvent.ACTION_DOWN) { - //如果是新的按下事件,则对mBottomFlag重新初始化 - mBottomFlag = lineCount <= maxLines + if (lineCount > maxLines) { + gestureDetector.onTouchEvent(event) } return super.dispatchTouchEvent(event) } @@ -48,22 +74,12 @@ open class NestScrollMultiAutoCompleteTextView @JvmOverloads constructor( override fun onTouchEvent(event: MotionEvent): Boolean { val result = super.onTouchEvent(event) //如果是需要拦截,则再拦截,这个方法会在onScrollChanged方法之后再调用一次 - if (!mBottomFlag) { + if (disallowIntercept && lineCount > maxLines) { parent.requestDisallowInterceptTouchEvent(true) } return result } - override fun onScrollChanged(horiz: Int, vert: Int, oldHoriz: Int, oldVert: Int) { - super.onScrollChanged(horiz, vert, oldHoriz, oldVert) - if (vert == mOffsetHeight || vert == 0) { - //这里触发父布局或祖父布局的滑动事件 - parent.requestDisallowInterceptTouchEvent(false) - mBottomFlag = true - } - } - - private fun initOffsetHeight() { val mLayoutHeight: Int