Mateusz Holak
07/18/2024, 8:13 PMStylianos Gakis
07/18/2024, 8:42 PMMateusz Holak
07/19/2024, 5:56 AMpackage com.example.jetpackcomposetraversewidgets
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.jetpackcomposetraversewidgets.ui.theme.JetpackComposeTraverseWidgetsTheme
@Composable
fun <T> ComposableWrapper(
composableFunction: @Composable () -> T
) {
// State to hold the latest root view
var rootView by remember { mutableStateOf<android.view.View?>(null) }
Column {
// Get the current root view
val currentView = LocalView.current
// LaunchedEffect to update the root view on recomposition
LaunchedEffect(currentView) {
rootView = currentView
}
// Call the composable function
composableFunction()
}
// Display the root view information
rootView?.let { view ->
val childViews = getAllChildViews(view)
Column(modifier = Modifier.padding(20.dp)) {
Text("Root view hash code: ${view.hashCode()}, ${view.id}")
childViews.forEach { child ->
val viewInfo = "ID: ${child.id}, Type: ${child::class.java.simpleName}, x: ${child.x}, y: ${child.y}"
Text(viewInfo, modifier = Modifier.padding(vertical = 4.dp))
}
}
}
}
fun getAllChildViews(view: View): List<View> {
val result = mutableListOf<View>()
if (view is ViewGroup) {
for (i in 0 until view.childCount) {
val child = view.getChildAt(i)
result.add(child)
result.addAll(getAllChildViews(child))
}
} else {
result.add(view)
}
return result
}
@Composable
fun SampleComposable() {
Column {
Text("Hello World", modifier = Modifier.padding(4.dp))
Text("Test", modifier = Modifier.padding(4.dp))
// Button(onClick = { /* Do something */ }) {
// Text("Click me")
// }
}
}
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
JetpackComposeTraverseWidgetsTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
ComposableWrapper {
SampleComposable()
}
}
}
}
}
}
@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
JetpackComposeTraverseWidgetsTheme {
ComposableWrapper {
SampleComposable()
}
}
}
Stylianos Gakis
07/19/2024, 8:31 AMthat data can be refreshed only every recomposition, sol can keep newest
What you're describing is just what compose does by default. Is this about interop with the view system? What are you trying to achieve?
Mateusz Holak
07/19/2024, 12:08 PMStylianos Gakis
07/19/2024, 12:11 PMMateusz Holak
07/19/2024, 7:10 PMMateusz Holak
07/19/2024, 7:14 PMMateusz Holak
07/19/2024, 7:23 PMMateusz Holak
07/19/2024, 7:26 PM