Skip to content

Commit 4496391

Browse files
authored
Merge pull request #66 from Petterpx/fix/fx_view_scope_reference
fix:view方式的浮窗取消弱引用
2 parents 33da079 + 8a96cff commit 4496391

3 files changed

Lines changed: 13 additions & 10 deletions

File tree

floatingx/src/main/java/com/petterp/floatingx/assist/helper/BasisHelper.kt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import com.petterp.floatingx.listener.IFxConfigStorage
99
import com.petterp.floatingx.listener.IFxScrollListener
1010
import com.petterp.floatingx.listener.IFxViewLifecycle
1111
import com.petterp.floatingx.util.FxLog
12-
import java.lang.ref.WeakReference
1312
import kotlin.math.abs
1413

1514
/** 通用构建器helper */
@@ -18,7 +17,7 @@ open class BasisHelper {
1817
internal var layoutId: Int = 0
1918

2019
@JvmField
21-
internal var layoutView: WeakReference<View>? = null
20+
internal var layoutView: View? = null
2221

2322
@JvmField
2423
internal var gravity: FxGravity = FxGravity.DEFAULT
@@ -104,12 +103,19 @@ open class BasisHelper {
104103
if (enableDebugLog) fxLog = FxLog.builder("$scope$fxLogTag")
105104
}
106105

106+
@JvmSynthetic
107+
internal fun clear() {
108+
layoutView = null
109+
enableFx = false
110+
fxAnimation?.cancelAnimation()
111+
}
112+
107113
abstract class Builder<T, B : BasisHelper> {
108114
private var context: Context? = null
109115

110116
@LayoutRes
111117
private var layoutId: Int = 0
112-
private var layoutView: WeakReference<View>? = null
118+
private var layoutView: View? = null
113119
private var gravity: FxGravity = FxGravity.DEFAULT
114120
private var clickTime: Long = 300L
115121
private var layoutParams: FrameLayout.LayoutParams? = null
@@ -190,7 +196,6 @@ open class BasisHelper {
190196

191197
/** 设置悬浮窗view的layout */
192198
fun setLayout(@LayoutRes layoutId: Int): T {
193-
this.layoutView?.clear()
194199
this.layoutView = null
195200
this.layoutId = layoutId
196201
return this as T
@@ -199,7 +204,7 @@ open class BasisHelper {
199204
/** 设置悬浮窗View */
200205
fun setLayoutView(view: View): T {
201206
layoutId = 0
202-
this.layoutView = WeakReference(view)
207+
this.layoutView = view
203208
return this as T
204209
}
205210

floatingx/src/main/java/com/petterp/floatingx/impl/control/FxBasisControlImpl.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,12 @@ open class FxBasisControlImpl(private val helper: BasisHelper) : IFxControl, IFx
7373

7474
override fun updateView(@LayoutRes resource: Int) {
7575
if (resource == 0) throw IllegalArgumentException("resource cannot be 0!")
76-
helper.layoutView?.clear()
7776
helper.layoutView = null
7877
updateMangerView(resource)
7978
}
8079

8180
override fun updateView(view: View) {
82-
helper.layoutView = WeakReference(view)
81+
helper.layoutView = view
8382
updateMangerView(0)
8483
}
8584

@@ -261,13 +260,12 @@ open class FxBasisControlImpl(private val helper: BasisHelper) : IFxControl, IFx
261260
}
262261

263262
protected open fun reset() {
264-
helper.enableFx = false
265-
helper.fxAnimation?.cancelAnimation()
266263
managerView?.removeCallbacks(hideAnimationRunnable)
267264
managerView?.removeCallbacks(cancelAnimationRunnable)
268265
detach(mContainer?.get())
269266
managerView = null
270267
viewHolder = null
268+
helper.clear()
271269
clearContainer()
272270
helper.fxLog?.d("fxView-lifecycle-> code->cancelFx")
273271
}

floatingx/src/main/java/com/petterp/floatingx/view/FxManagerView.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class FxManagerView @JvmOverloads constructor(
7070
}
7171

7272
private fun inflateLayoutView(): View? {
73-
val view = helper.layoutView?.get() ?: return null
73+
val view = helper.layoutView ?: return null
7474
helper.fxLog?.d("fxView-->init, way:[layoutView]")
7575
val lp = layoutParams ?: LayoutParams(
7676
ViewGroup.LayoutParams.WRAP_CONTENT,

0 commit comments

Comments
 (0)