golia-expo-utils 1.0.4 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -33,19 +33,30 @@ class ReactSegmentedControl(context: Context, appContext: AppContext) :
33
33
  private var isContentSet = false
34
34
 
35
35
  private val composeView = ComposeView(context).apply {
36
- layoutParams = ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
36
+ layoutParams = ViewGroup.LayoutParams(
37
+ LayoutParams.MATCH_PARENT,
38
+ LayoutParams.MATCH_PARENT
39
+ )
37
40
  setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnDetachedFromWindow)
38
41
  }
39
42
 
40
43
  init {
41
- addView(composeView)
44
+ clipChildren = false
45
+ clipToPadding = false
42
46
  }
43
47
 
44
48
  override fun onAttachedToWindow() {
45
49
  super.onAttachedToWindow()
46
50
 
51
+ // 1. 注入生命周期
47
52
  composeView.enforceLifecycle(context)
48
53
 
54
+ // 2. 动态挂载
55
+ if (composeView.parent == null) {
56
+ addView(composeView)
57
+ }
58
+
59
+ // 3. 设置内容
49
60
  if (!isContentSet) {
50
61
  composeView.setContent {
51
62
  SegmentedControl(
@@ -73,5 +84,31 @@ class ReactSegmentedControl(context: Context, appContext: AppContext) :
73
84
  }
74
85
  isContentSet = true
75
86
  }
87
+
88
+ requestLayout()
89
+ }
90
+
91
+ override fun onDetachedFromWindow() {
92
+ if (composeView.parent == this) {
93
+ removeView(composeView)
94
+ }
95
+ super.onDetachedFromWindow()
96
+ }
97
+
98
+ // ✅ 终极修正:手动接管布局
99
+ override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
100
+ super.onLayout(changed, left, top, right, bottom)
101
+
102
+ if (composeView.parent == this) {
103
+ val width = right - left
104
+ val height = bottom - top
105
+
106
+ // 强行设置 ComposeView 的大小
107
+ composeView.measure(
108
+ MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
109
+ MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)
110
+ )
111
+ composeView.layout(0, 0, width, height)
112
+ }
76
113
  }
77
114
  }
@@ -1,5 +1,6 @@
1
1
  package expo.modules.goliaexpoutils.waterView
2
2
 
3
+ // 引入扩展
3
4
  import android.annotation.SuppressLint
4
5
  import android.content.Context
5
6
  import android.view.View
@@ -68,15 +69,20 @@ class ReactWaterView(context: Context, appContext: AppContext) : ExpoView(contex
68
69
  init {
69
70
  clipChildren = false
70
71
  clipToPadding = false
71
-
72
- addView(composeView)
73
72
  }
74
73
 
75
74
  override fun onAttachedToWindow() {
76
75
  super.onAttachedToWindow()
77
76
 
77
+ // 1. 注入生命周期
78
78
  composeView.enforceLifecycle(context)
79
79
 
80
+ // 2. 动态挂载 View
81
+ if (composeView.parent == null) {
82
+ addView(composeView)
83
+ }
84
+
85
+ // 3. 设置内容
80
86
  if (!isContentSet) {
81
87
  composeView.setContent {
82
88
  WaterViewEntry(
@@ -97,16 +103,45 @@ class ReactWaterView(context: Context, appContext: AppContext) : ExpoView(contex
97
103
  }
98
104
  isContentSet = true
99
105
  }
106
+
107
+ // 4. 强制请求重新布局
108
+ requestLayout()
109
+ }
110
+
111
+ override fun onDetachedFromWindow() {
112
+ // 必须移除,防止 Fabric 对离屏 View 进行测量导致崩溃
113
+ if (composeView.parent == this) {
114
+ removeView(composeView)
115
+ }
116
+ super.onDetachedFromWindow()
117
+ }
118
+
119
+ // ✅ 终极修正:手动接管布局
120
+ // 解决“View加上去了但是宽高为0不显示”的问题
121
+ override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
122
+ super.onLayout(changed, left, top, right, bottom)
123
+
124
+ // 如果 composeView 已经挂载,强行设置它的尺寸等于父容器
125
+ if (composeView.parent == this) {
126
+ val width = right - left
127
+ val height = bottom - top
128
+
129
+ // 1. 手动测量
130
+ composeView.measure(
131
+ MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
132
+ MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)
133
+ )
134
+ // 2. 手动布局 (放在 (0,0) 位置)
135
+ composeView.layout(0, 0, width, height)
136
+ }
100
137
  }
101
138
 
102
139
  override fun addView(child: View?, index: Int) {
103
140
  if (child == composeView) {
104
141
  super.addView(child, index)
105
- } else {
106
- if (child != null) {
107
- (child.parent as? ViewGroup)?.removeView(child)
108
- childContainer.addView(child)
109
- }
142
+ } else if (child != null) {
143
+ (child.parent as? ViewGroup)?.removeView(child)
144
+ childContainer.addView(child)
110
145
  }
111
146
  }
112
147
 
@@ -122,7 +157,7 @@ class ReactWaterView(context: Context, appContext: AppContext) : ExpoView(contex
122
157
  if (childContainer.isNotEmpty()) {
123
158
  try {
124
159
  childContainer.removeViewAt(0)
125
- } catch (_: Exception) {
160
+ } catch (_: Throwable) {
126
161
  }
127
162
  }
128
163
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "golia-expo-utils",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "expo utils provided by golia.jp",
5
5
  "main": "build/index.js",
6
6
  "homepage": "https://expo.golia.jp/golia-expo-utils",