我想从我的回调中打开WebView.但我收到了以下错误
@Composable invocations can only happen from the context of a @Composable function
个
这个代码片段就是问题所在
onNewsLinkedClicked = { newsLink ->
WebViewScreen(webLink = newsLink)
}
我确实try 过在NewsScreen函数中声明我的lambda,如下所示.
onNewsLinkedClicked: @Composable (newsLink: String) -> Unit
个
这解决了下面的问题.但后来我遇到了另一个问题,当我从Button onClick事件启动回调时,开始给我一个错误.
对于我下面的代码,有什么方法可以做到这一点吗?
@AndroidEntryPoint
class MainActivity : ComponentActivity() {
@OptIn(ExperimentalMaterial3Api::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
val newsViewModel: NewsViewModel = hiltViewModel()
val newsHeadLines = newsViewModel.newsPager.collectAsLazyPagingItems()
BeerPagingTheme {
// A surface container using the 'background' color from the theme
val scrollBehavior = enterAlwaysScrollBehavior()
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
NewsScreen(
newsPagingData = newsHeadLines,
modifier = Modifier
.fillMaxSize()
.nestedScroll(connection = scrollBehavior.nestedScrollConnection),
topAppBarScrollBehavior = scrollBehavior,
onNewsLinkedClicked = { newsLink ->
WebViewScreen(webLink = newsLink)
}
)
}
}
}
}
}
@Composable
fun WebViewScreen(webLink: String) {
AndroidView(
factory = { context ->
WebView(context).apply {
this.layoutParams = ViewGroup.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT)
loadUrl(webLink)
} },
update = { webView ->
webView.loadUrl(webLink)
},
modifier = Modifier.fillMaxSize())
}