From e8b40462779c153bf62885c2733f550fc1b95aaa Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 27 Sep 2022 22:51:56 +0800 Subject: [PATCH] =?UTF-8?q?=E9=99=90=E5=88=B6=E6=BA=90=E7=BC=96=E8=BE=91?= =?UTF-8?q?=E6=A1=86=E7=9A=84=E6=9C=80=E5=A4=A7=E8=A1=8C=E6=95=B0,?= =?UTF-8?q?=E6=BB=9A=E5=8A=A8=E5=88=B0=E5=85=B6=E4=BB=96=E8=BE=93=E5=85=A5?= =?UTF-8?q?=E6=A1=86=E6=9B=B4=E5=AE=B9=E6=98=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/ui/widget/code/CodeView.kt | 74 +--------------- .../text/ScrollMultiAutoCompleteTextView.kt | 84 +++++++++++++++++++ .../app/ui/widget/text/ScrollTextView.kt | 4 - 3 files changed, 86 insertions(+), 76 deletions(-) create mode 100644 app/src/main/java/io/legado/app/ui/widget/text/ScrollMultiAutoCompleteTextView.kt diff --git a/app/src/main/java/io/legado/app/ui/widget/code/CodeView.kt b/app/src/main/java/io/legado/app/ui/widget/code/CodeView.kt index fea3b0a2b..30f578436 100644 --- a/app/src/main/java/io/legado/app/ui/widget/code/CodeView.kt +++ b/app/src/main/java/io/legado/app/ui/widget/code/CodeView.kt @@ -1,6 +1,5 @@ package io.legado.app.ui.widget.code -import android.annotation.SuppressLint import android.content.Context import android.graphics.Canvas import android.graphics.Paint @@ -13,9 +12,8 @@ import android.text.style.BackgroundColorSpan import android.text.style.ForegroundColorSpan import android.text.style.ReplacementSpan import android.util.AttributeSet -import android.view.MotionEvent import androidx.annotation.ColorInt -import androidx.appcompat.widget.AppCompatMultiAutoCompleteTextView +import io.legado.app.ui.widget.text.ScrollMultiAutoCompleteTextView import java.util.* import java.util.regex.Matcher import java.util.regex.Pattern @@ -23,7 +21,7 @@ import kotlin.math.roundToInt @Suppress("unused") class CodeView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : - AppCompatMultiAutoCompleteTextView(context, attrs) { + ScrollMultiAutoCompleteTextView(context, attrs) { private var tabWidth = 0 private var tabWidthInCharacters = 0 @@ -39,12 +37,6 @@ class CodeView @JvmOverloads constructor(context: Context, attrs: AttributeSet? private val mSyntaxPatternMap: MutableMap = HashMap() private var mIndentCharacterList = mutableListOf('{', '+', '-', '*', '/', '=') - //滑动距离的最大边界 - private var mOffsetHeight = 0 - - //是否到顶或者到底的标志 - private var mBottomFlag = false - private val mUpdateRunnable = Runnable { val source = text highlightWithoutChange(source) @@ -110,46 +102,6 @@ class CodeView @JvmOverloads constructor(context: Context, attrs: AttributeSet? addTextChangedListener(mEditorTextWatcher) } - override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { - super.onMeasure(widthMeasureSpec, heightMeasureSpec) - initOffsetHeight() - } - - override fun onTextChanged( - text: CharSequence, - start: Int, - lengthBefore: Int, - lengthAfter: Int - ) { - super.onTextChanged(text, start, lengthBefore, lengthAfter) - initOffsetHeight() - } - - override fun dispatchTouchEvent(event: MotionEvent): Boolean { - if (event.action == MotionEvent.ACTION_DOWN) { - //如果是新的按下事件,则对mBottomFlag重新初始化 - mBottomFlag = mOffsetHeight <= 0 - } - return super.dispatchTouchEvent(event) - } - - @SuppressLint("ClickableViewAccessibility") - override fun onTouchEvent(event: MotionEvent): Boolean { - val result = super.onTouchEvent(event) - //如果是需要拦截,则再拦截,这个方法会在onScrollChanged方法之后再调用一次 - if (!mBottomFlag) 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 - } - } - override fun showDropDown() { val screenPoint = IntArray(2) getLocationOnScreen(screenPoint) @@ -165,28 +117,6 @@ class CodeView @JvmOverloads constructor(context: Context, attrs: AttributeSet? super.showDropDown() } - private fun initOffsetHeight() { - val mLayoutHeight: Int - - //获得内容面板 - val mLayout = layout ?: return - //获得内容面板的高度 - mLayoutHeight = mLayout.height - //获取上内边距 - val paddingTop: Int = totalPaddingTop - //获取下内边距 - val paddingBottom: Int = totalPaddingBottom - - //获得控件的实际高度 - val mHeight: Int = measuredHeight - - //计算滑动距离的边界 - mOffsetHeight = mLayoutHeight + paddingTop + paddingBottom - mHeight - if (mOffsetHeight <= 0) { - scrollTo(0, 0) - } - } - private fun autoIndent( source: CharSequence, dest: Spanned, diff --git a/app/src/main/java/io/legado/app/ui/widget/text/ScrollMultiAutoCompleteTextView.kt b/app/src/main/java/io/legado/app/ui/widget/text/ScrollMultiAutoCompleteTextView.kt new file mode 100644 index 000000000..849812a6b --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/widget/text/ScrollMultiAutoCompleteTextView.kt @@ -0,0 +1,84 @@ +package io.legado.app.ui.widget.text + +import android.annotation.SuppressLint +import android.content.Context +import android.util.AttributeSet +import android.view.MotionEvent +import androidx.appcompat.widget.AppCompatMultiAutoCompleteTextView + +open class ScrollMultiAutoCompleteTextView @JvmOverloads constructor( + context: Context, + attrs: AttributeSet? = null +) : AppCompatMultiAutoCompleteTextView(context, attrs) { + + + //滑动距离的最大边界 + private var mOffsetHeight = 0 + + //是否到顶或者到底的标志 + private var mBottomFlag = false + + override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { + super.onMeasure(widthMeasureSpec, heightMeasureSpec) + initOffsetHeight() + } + + override fun onTextChanged( + text: CharSequence, + start: Int, + lengthBefore: Int, + lengthAfter: Int + ) { + super.onTextChanged(text, start, lengthBefore, lengthAfter) + initOffsetHeight() + } + + override fun dispatchTouchEvent(event: MotionEvent): Boolean { + if (event.action == MotionEvent.ACTION_DOWN) { + //如果是新的按下事件,则对mBottomFlag重新初始化 + mBottomFlag = mOffsetHeight <= 0 + } + return super.dispatchTouchEvent(event) + } + + @SuppressLint("ClickableViewAccessibility") + override fun onTouchEvent(event: MotionEvent): Boolean { + val result = super.onTouchEvent(event) + //如果是需要拦截,则再拦截,这个方法会在onScrollChanged方法之后再调用一次 + if (!mBottomFlag) 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 + + //获得内容面板 + val mLayout = layout ?: return + //获得内容面板的高度 + mLayoutHeight = mLayout.height + //获取上内边距 + val paddingTop: Int = totalPaddingTop + //获取下内边距 + val paddingBottom: Int = totalPaddingBottom + + //获得控件的实际高度 + val mHeight: Int = measuredHeight + + //计算滑动距离的边界 + mOffsetHeight = mLayoutHeight + paddingTop + paddingBottom - mHeight + if (mOffsetHeight <= 0) { + scrollTo(0, 0) + } + } + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/widget/text/ScrollTextView.kt b/app/src/main/java/io/legado/app/ui/widget/text/ScrollTextView.kt index 4a9da6e0f..336dd8ff3 100644 --- a/app/src/main/java/io/legado/app/ui/widget/text/ScrollTextView.kt +++ b/app/src/main/java/io/legado/app/ui/widget/text/ScrollTextView.kt @@ -55,10 +55,6 @@ class ScrollTextView(context: Context, attrs: AttributeSet?) : AppCompatTextView //如果是新的按下事件,则对mBottomFlag重新初始化 mBottomFlag = mOffsetHeight <= 0 } - //如果已经不要这次事件,则传出取消的信号,这里的作用不大 - if (mBottomFlag) { - event.action = MotionEvent.ACTION_CANCEL - } return super.dispatchTouchEvent(event) }