Skip to content

Commit

Permalink
feat:增加长按事件
Browse files Browse the repository at this point in the history
  • Loading branch information
Petterpx committed Mar 21, 2024
1 parent 31b8673 commit 6e6b805
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ class CustomKtApplication : Application() {
setOnClickListener {
Toast.makeText(context, "浮窗被点击", Toast.LENGTH_SHORT).show()
}
setOnLongClickListener {
Toast.makeText(context, "浮窗被长按", Toast.LENGTH_SHORT).show()
true
}
// 设置tag-Activity生命周期回调时的触发
setTagActivityLifecycle(object : IFxProxyTagActivityLifecycle {
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ open class FxBasisHelper {
@JvmField
internal var iFxClickListener: View.OnClickListener? = null

@JvmField
internal var iFxLongClickListener: View.OnLongClickListener? = null

internal lateinit var fxLog: FxLog

@JvmField
Expand Down Expand Up @@ -131,6 +134,9 @@ open class FxBasisHelper {
val safeEdgeOffSet: Float
get() = if (enableEdgeRebound) edgeOffset else 0F

internal val hasClickStatus: Boolean
get() = enableClickListener && (iFxClickListener != null || iFxLongClickListener != null)

internal val hasDefaultXY: Boolean
get() = defaultX != 0f || defaultY != 0f

Expand Down Expand Up @@ -167,6 +173,7 @@ open class FxBasisHelper {
private var iFxViewLifecycle: IFxViewLifecycle? = null
private var iFxTouchListener: IFxTouchListener? = null
private var ifxClickListener: View.OnClickListener? = null
private var ifxLongClickListener: View.OnLongClickListener? = null

protected abstract fun buildHelper(): B

Expand Down Expand Up @@ -206,6 +213,7 @@ open class FxBasisHelper {
iFxViewLifecycle = this@Builder.iFxViewLifecycle
iFxConfigStorage = this@Builder.iFxConfigStorage
iFxClickListener = this@Builder.ifxClickListener
iFxLongClickListener = this@Builder.ifxLongClickListener
}

/** 设置悬浮窗view的layout */
Expand Down Expand Up @@ -286,6 +294,14 @@ open class FxBasisHelper {
return this as T
}

/** 设置悬浮窗长按事件 [ifxLongClickListener]
* */
fun setOnLongClickListener(listener: View.OnLongClickListener): T {
this.enableClickListener = true
this.ifxLongClickListener = listener
return this as T
}

/**
* 设置悬浮窗的layoutParams,即浮窗容器,非自己传递进去的用于显示的View
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,19 @@ abstract class FxBasisControlImp<F : FxBasisHelper, P : IFxPlatformProvider<F>>(
provider.apply(getViewHolder() ?: return)
}

override fun setClickListener(time: Long, clickListener: View.OnClickListener) {
override fun setClickListener(time: Long, listener: View.OnClickListener?) {
helper.clickTime = time
helper.enableClickListener = true
helper.iFxClickListener = clickListener
helper.iFxClickListener = listener
helper.enableClickListener = listener != null
}

override fun setClickListener(clickListener: View.OnClickListener) {
setClickListener(0, clickListener)
override fun setClickListener(listener: View.OnClickListener?) {
setClickListener(0, listener)
}

override fun setLongClickListener(listener: View.OnLongClickListener?) {
helper.iFxLongClickListener = listener
helper.enableClickListener = listener != null
}

override fun move(x: Float, y: Float) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,14 @@ interface IFxControl {
/** 更新当前View,如果要通过view更新视图,建议通过此方法,可以帮助选用合适的context,来避免因context所导致的内存泄漏 */
fun updateView(provider: IFxContextProvider)

/** 设置点击事件,同时增加防重 */
fun setClickListener(time: Long = 300L, clickListener: View.OnClickListener)

/** 设置点击事件 */
fun setClickListener(clickListener: View.OnClickListener)
fun setClickListener(listener: View.OnClickListener?)

/** 设置长按事件 */
fun setLongClickListener(listener: View.OnLongClickListener?)

/** 设置点击事件,同时增加防重 */
fun setClickListener(time: Long = 300L, listener: View.OnClickListener?)

/**
* 移动浮窗到指定位置,该方法会帮助你处理越界问题,默认带动画
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ internal const val INVALID_TOUCH_ID = -1
internal const val INVALID_LAYOUT_ID = 0
internal const val INVALID_TOUCH_IDX = -1
internal const val TOUCH_TIME_THRESHOLD = 150L
internal const val TOUCH_CLICK_LONG_TIME = 500L
internal const val DEFAULT_MOVE_ANIMATOR_DURATION = 200L
internal const val FX_DEFAULT_TAG = "FX_DEFAULT_TAG"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.petterp.floatingx.view.helper

import android.annotation.SuppressLint
import android.view.HapticFeedbackConstants
import android.view.MotionEvent
import android.view.ViewConfiguration
import com.petterp.floatingx.assist.FxDisplayMode
import com.petterp.floatingx.util.INVALID_TOUCH_ID
import com.petterp.floatingx.util.TOUCH_CLICK_LONG_TIME
import com.petterp.floatingx.util.TOUCH_TIME_THRESHOLD
import com.petterp.floatingx.util.pointerId
import com.petterp.floatingx.view.FxBasicContainerView
Expand All @@ -19,7 +21,7 @@ class FxViewTouchHelper : FxViewBasicHelper() {
private var initY = 0f
private var scaledTouchSlop = 0F
private var isClickEvent = false
private var clickEnable = true
private var isEnableClick = true
private var mLastTouchDownTime = 0L
private var touchDownId = INVALID_TOUCH_ID

Expand Down Expand Up @@ -123,12 +125,18 @@ class FxViewTouchHelper : FxViewBasicHelper() {
}

private fun performClickAction() {
if (isClickEffective()) {
clickEnable = false
config.iFxClickListener?.onClick(basicView)
basicView?.postDelayed({
clickEnable = true
}, config.clickTime)
if (isClickEvent && config.hasClickStatus) {
val diffTime = System.currentTimeMillis() - mLastTouchDownTime
if (diffTime < TOUCH_TIME_THRESHOLD && isEnableClick) {
if (config.clickTime > 0) {
isEnableClick = false
basicView?.postDelayed({ isEnableClick = true }, config.clickTime)
}
config.iFxClickListener?.onClick(basicView)
} else if (diffTime >= TOUCH_CLICK_LONG_TIME) {
val isHandle = config.iFxLongClickListener?.onLongClick(basicView) ?: false
if (isHandle) basicView?.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS)
}
}
reset()
}
Expand All @@ -153,11 +161,4 @@ class FxViewTouchHelper : FxViewBasicHelper() {
}

private fun hasMainPointerId() = touchDownId != INVALID_TOUCH_ID

private fun isClickEffective(): Boolean {
// 当前是点击事件&&点击事件目前可启用&&回调存在&&点击时间小于阈值
return isClickEvent && clickEnable && config.enableClickListener &&
config.iFxClickListener != null &&
System.currentTimeMillis() - mLastTouchDownTime < TOUCH_TIME_THRESHOLD
}
}

0 comments on commit 6e6b805

Please sign in to comment.