@@ -32,6 +32,7 @@ import androidx.activity.ComponentActivity
3232import androidx.activity.compose.setContent
3333import androidx.compose.foundation.layout.Column
3434import androidx.compose.foundation.layout.fillMaxSize
35+ import androidx.compose.foundation.lazy.LazyColumn
3536import androidx.compose.material3.Button
3637import androidx.compose.material3.MaterialTheme
3738import androidx.compose.material3.Text
@@ -42,6 +43,7 @@ import androidx.compose.runtime.mutableStateOf
4243import androidx.compose.runtime.remember
4344import androidx.compose.runtime.rememberUpdatedState
4445import androidx.compose.runtime.setValue
46+ import androidx.compose.ui.ExperimentalComposeUiApi
4547import androidx.compose.ui.Modifier
4648import androidx.compose.ui.platform.ComposeView
4749import androidx.compose.ui.platform.LocalContext
@@ -164,6 +166,29 @@ class ExampleFragmentMultipleComposeView : Fragment() {
164166}
165167// [END android_compose_interop_apis_compose_in_fragment_multiple]
166168
169+ // [START android_compose_interop_apis_android_view_reuse]
170+ @OptIn(ExperimentalComposeUiApi ::class )
171+ @Composable
172+ fun AndroidViewInLazyList () {
173+ LazyColumn {
174+ items(100 ) { index ->
175+ AndroidView (
176+ modifier = Modifier .fillMaxSize(), // Occupy the max size in the Compose UI tree
177+ factory = { context ->
178+ MyView (context)
179+ },
180+ update = { view ->
181+ view.selectedItem = index
182+ },
183+ onReset = { view ->
184+ view.clear()
185+ }
186+ )
187+ }
188+ }
189+ }
190+ // [END android_compose_interop_apis_android_view_reuse]
191+
167192// [START android_compose_interop_apis_views_in_compose]
168193@Composable
169194fun CustomView () {
@@ -204,6 +229,8 @@ fun ContentExample() {
204229// [START_EXCLUDE silent]
205230class MyView (context : Context ) : View(context) {
206231 var selectedItem: Int = 0
232+
233+ fun clear () { }
207234}
208235// [END_EXCLUDE silent]
209236// [END android_compose_interop_apis_views_in_compose]
0 commit comments