@@ -10,6 +10,7 @@ import android.content.res.ColorStateList
10
10
import android.graphics.Color
11
11
import android.view.View
12
12
import android.view.ViewGroup
13
+ import android.view.ViewTreeObserver.OnPreDrawListener
13
14
import android.widget.ImageView
14
15
import androidx.annotation.DrawableRes
15
16
import androidx.annotation.Size
@@ -28,6 +29,7 @@ import org.fcitx.fcitx5.android.daemon.FcitxConnection
28
29
import org.fcitx.fcitx5.android.data.theme.Theme
29
30
import org.fcitx.fcitx5.android.input.candidates.CandidateItemUi
30
31
import org.fcitx.fcitx5.android.input.preedit.PreeditUi
32
+ import org.fcitx.fcitx5.android.utils.styledFloat
31
33
import splitties.dimensions.dp
32
34
import splitties.resources.drawable
33
35
import splitties.views.backgroundColor
@@ -63,7 +65,9 @@ class CandidatesView(
63
65
field = value
64
66
visibility = View .GONE
65
67
if (field) {
66
- setupFcitxEventHandler()
68
+ if (eventHandlerJob == null ) {
69
+ setupFcitxEventHandler()
70
+ }
67
71
} else {
68
72
eventHandlerJob?.cancel()
69
73
eventHandlerJob = null
@@ -145,18 +149,20 @@ class CandidatesView(
145
149
}
146
150
}
147
151
152
+ private val updatePositionListener = OnPreDrawListener {
153
+ if (shouldUpdatePosition) {
154
+ updatePosition()
155
+ }
156
+ true
157
+ }
158
+
148
159
private val recyclerView = recyclerView {
149
160
isFocusable = false
150
161
horizontalPadding = dp(8 )
151
162
adapter = candidatesAdapter
152
163
layoutManager = candidatesLayoutManager
153
164
// update position after layout child views and before drawing to avoid flicker
154
- viewTreeObserver.addOnPreDrawListener {
155
- if (shouldUpdatePosition) {
156
- updatePosition()
157
- }
158
- true
159
- }
165
+ this @CandidatesView.viewTreeObserver.addOnPreDrawListener(updatePositionListener)
160
166
}
161
167
162
168
private fun createIcon (@DrawableRes icon : Int ) = imageView {
@@ -274,6 +280,7 @@ class CandidatesView(
274
280
275
281
override fun onDetachedFromWindow () {
276
282
handleEvents = false
283
+ viewTreeObserver.removeOnPreDrawListener(updatePositionListener)
277
284
super .onDetachedFromWindow()
278
285
}
279
286
}
0 commit comments