From 10c9a6e8e76feca89a701e09016679be1e014efa Mon Sep 17 00:00:00 2001
From: 18034727885 <973609879@qq.com>
Date: 星期四, 18 六月 2026 18:07:58 +0800
Subject: [PATCH] Merge branch 'project_1' into project_2
---
common/src/main/java/com/xw/common/custom/SlideSwitchView.kt | 91 ++++++++++++++++++++++++++-------------------
1 files changed, 53 insertions(+), 38 deletions(-)
diff --git a/common/src/main/java/com/xw/common/custom/SlideSwitchView.kt b/common/src/main/java/com/xw/common/custom/SlideSwitchView.kt
index 4bd674e..bbd8b20 100644
--- a/common/src/main/java/com/xw/common/custom/SlideSwitchView.kt
+++ b/common/src/main/java/com/xw/common/custom/SlideSwitchView.kt
@@ -1,15 +1,18 @@
package com.xw.common.custom
import android.content.Context
-import android.graphics.*
+import android.graphics.Canvas
+import android.graphics.Color
+import android.graphics.Paint
+import android.graphics.RectF
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.view.animation.DecelerateInterpolator
import android.widget.Scroller
import androidx.core.content.withStyledAttributes
-import kotlin.math.abs
import com.xw.common.R
+import kotlin.math.abs
/**
* 自定义滑动开关 View
@@ -25,6 +28,13 @@
interface OnStateChangeListener {
fun onStateChanged(isChecked: Boolean)
}
+
+ //是否能手动点击
+ var switchEnable = true
+ set(value) {
+ field = value
+ invalidate()
+ }
// 尺寸配置
private var switchWidth = 0f
@@ -68,6 +78,7 @@
init {
context.withStyledAttributes(attrs, R.styleable.SlideSwitchView, defStyleAttr, 0) {
+ switchEnable = getBoolean(R.styleable.SlideSwitchView_switchEnable, switchEnable)
// 背景颜色
backgroundColorUnchecked = getColor(
R.styleable.SlideSwitchView_switchBackgroundColorUnchecked,
@@ -99,7 +110,8 @@
)
// 文字内容
- textUnchecked = getString(R.styleable.SlideSwitchView_switchTextUnchecked) ?: textUnchecked
+ textUnchecked =
+ getString(R.styleable.SlideSwitchView_switchTextUnchecked) ?: textUnchecked
textChecked = getString(R.styleable.SlideSwitchView_switchTextChecked) ?: textChecked
// 文字大小
@@ -107,7 +119,8 @@
// 圆角
cornerRadius = getDimension(R.styleable.SlideSwitchView_switchCornerRadius, 0f)
- thumbCornerRadius = getDimension(R.styleable.SlideSwitchView_switchThumbCornerRadius, 0f)
+ thumbCornerRadius =
+ getDimension(R.styleable.SlideSwitchView_switchThumbCornerRadius, 0f)
// 滑块间距(新增)
thumbMargin = getDimension(R.styleable.SlideSwitchView_switchThumbMargin, 0f)
@@ -222,44 +235,46 @@
}
override fun onTouchEvent(event: MotionEvent): Boolean {
- when (event.action) {
- MotionEvent.ACTION_DOWN -> {
- lastX = event.x
- isDragging = false
- parent?.requestDisallowInterceptTouchEvent(true)
- return true
- }
-
- MotionEvent.ACTION_MOVE -> {
- val deltaX = event.x - lastX
- if (!isDragging && abs(deltaX) > touchSlop) {
- isDragging = true
+ if (switchEnable) {
+ when (event.action) {
+ MotionEvent.ACTION_DOWN -> {
+ lastX = event.x
+ isDragging = false
+ parent?.requestDisallowInterceptTouchEvent(true)
+ return true
}
- if (isDragging) {
- thumbPosition += deltaX
- // 限制滑动范围
- thumbPosition = thumbPosition.coerceIn(0f, maxThumbPosition)
- invalidate()
- }
- lastX = event.x
- return true
- }
+ MotionEvent.ACTION_MOVE -> {
+ val deltaX = event.x - lastX
+ if (!isDragging && abs(deltaX) > touchSlop) {
+ isDragging = true
+ }
- MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
- parent?.requestDisallowInterceptTouchEvent(false)
-
- if (!isDragging) {
- // 点击切换
- toggle()
- } else {
- // 滑动释放,根据位置决定状态
- val threshold = maxThumbPosition / 2
- val targetChecked = thumbPosition > threshold
- animateToState(targetChecked)
+ if (isDragging) {
+ thumbPosition += deltaX
+ // 限制滑动范围
+ thumbPosition = thumbPosition.coerceIn(0f, maxThumbPosition)
+ invalidate()
+ }
+ lastX = event.x
+ return true
}
- isDragging = false
- return true
+
+ MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
+ parent?.requestDisallowInterceptTouchEvent(false)
+
+ if (!isDragging) {
+ // 点击切换
+ toggle()
+ } else {
+ // 滑动释放,根据位置决定状态
+ val threshold = maxThumbPosition / 2
+ val targetChecked = thumbPosition > threshold
+ animateToState(targetChecked)
+ }
+ isDragging = false
+ return true
+ }
}
}
return super.onTouchEvent(event)
--
Gitblit v1.7.1