Skip to content

Commit 1d381ff

Browse files
committed
Add AndroidView reusable snippet.
1 parent 3bbac16 commit 1d381ff

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

compose/snippets/build.gradle

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,14 @@ dependencies {
5252
implementation 'androidx.paging:paging-common-ktx:3.1.1'
5353
def composeBom = platform('androidx.compose:compose-bom:2023.01.00')
5454
implementation(composeBom)
55-
implementation "androidx.compose.ui:ui"
56-
implementation "androidx.compose.ui:ui-graphics"
57-
implementation "androidx.compose.ui:ui-tooling-preview"
58-
implementation "androidx.compose.ui:ui-viewbinding"
55+
def composeVersion = '1.4.0-rc01'
56+
implementation "androidx.compose.ui:ui:$composeVersion"
57+
implementation "androidx.compose.ui:ui-graphics:$composeVersion"
58+
implementation "androidx.compose.ui:ui-tooling-preview:$composeVersion"
59+
implementation "androidx.compose.ui:ui-viewbinding:$composeVersion"
5960
implementation "androidx.paging:paging-compose:1.0.0-alpha18"
6061
implementation 'androidx.compose.animation:animation-graphics'
61-
implementation 'androidx.compose.material3:material3:1.1.0-alpha06'
62+
implementation 'androidx.compose.material3:material3:1.1.0-alpha08'
6263
implementation 'androidx.compose.material:material'
6364
implementation 'androidx.compose.runtime:runtime-livedata'
6465

compose/snippets/src/main/java/com/example/compose/snippets/interop/InteroperabilityAPIsSnippets.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import androidx.activity.ComponentActivity
3232
import androidx.activity.compose.setContent
3333
import androidx.compose.foundation.layout.Column
3434
import androidx.compose.foundation.layout.fillMaxSize
35+
import androidx.compose.foundation.lazy.LazyColumn
3536
import androidx.compose.material3.Button
3637
import androidx.compose.material3.MaterialTheme
3738
import androidx.compose.material3.Text
@@ -42,6 +43,7 @@ import androidx.compose.runtime.mutableStateOf
4243
import androidx.compose.runtime.remember
4344
import androidx.compose.runtime.rememberUpdatedState
4445
import androidx.compose.runtime.setValue
46+
import androidx.compose.ui.ExperimentalComposeUiApi
4547
import androidx.compose.ui.Modifier
4648
import androidx.compose.ui.platform.ComposeView
4749
import 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
169194
fun CustomView() {
@@ -204,6 +229,8 @@ fun ContentExample() {
204229
// [START_EXCLUDE silent]
205230
class 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

Comments
 (0)